Arduino C++
From Aino Wiki
Contents
Differenze fondamentali
- La istruzione
printfnon esiste ma si usa laprint - C'è differenza tra C++ (e C++ di Arduino) e C#, si usano le parentesi quadarate ma in C++ si mettono dopo il nome della variabile mentre in C# si mettono dopo il nome del tipo.
Tips
Output x Debug
Serial Monitor
A titolo di esempio con Arduino non funziona il "Console.WriteLn()" di C# ma per stampare dei testi che poi comunque finiranno a "video" si usa il "Serial Monitor" che è una applicazione fruibile dall'IDE di Arduino e che dallo scketc si può usare per inviare del testo, esempio messaggi per debuggare.
Per usarlo occorrerà inserire nella funzione setup() l'inializzazione, attendere che sia disponibile e pi nella funzione loop() si potrà produrre output. A scopo dei esempio ecco come suarlo:
void setup() { Serial.begin(9600); // Inizializzazione con 9600 baud while (Serial); // Da Arduino 4 in poi occorre attendere che l'inizializzazione seriale sia davvero operativa } void loop() { Serial.println("Testo con accapo!"); Serial.print("Solo testo (NON con accapo)!"); }
Leggere da seriale
//??? String stringaLetta = Serial.readStringUntil('\n'); while(Serial.available()) //all the time there is something to read { //read the input delay(50); //waste some time to allow characters to arrive char c=Serial.read(); //read a character readString+=c; //add it to a String } //keep reading until there is nothing more to read if(readString.length()>0){ //if something was actually read and added to the String Serial.println(readString); //print the String if (readString =="ON"){ //if the String is "ON" digitalWrite(led, HIGH); //make the led pin HIGH } readString=""; //reset the value of the String to nothing }
Stringhe
Ecco come concatenare un numero ad una stringa:
String displayMsg = ""; int stationCount = 0; //Concatenazione dispMsg = "Trovate " + (String)stationCount + " staz. "; Serial.println(dispMsg);
Cloud
Memo
| Istruzione/Oggetto | Descrizione | Esempio |
|---|---|---|
| "thingProperties.h" | Header da aggiungere negli sketch che si riferiscono al cloud. Le variabili dichiarate nelle Things sono in questo file. |
#include "thingProperties.h" |
| ArduinoCloud | Oggetto che ha i metodi per interagire col cloud | // Connect to Arduino IoT Cloud ArduinoCloud.begin(ArduinoIoTPreferredConnection); |
| ArduinoCloud.begin | Inizializza l'ogetto "ArduinoCloud" |
Sketch di default
Non è proprio generico ma è il mio primo, ha per nome che inizia col nome dato alle "Thinks" (ovvero oggetti elettronici o variabili pulliche sul cloud)./* Sketch generated by the Arduino IoT Cloud Thing "Untitled" https://create.arduino.cc/cloud/things/8cb07275-7f5c-414f-86cc-7b7e9c80389a Arduino IoT Cloud Variables description The following variables are automatically generated and updated when changes are made to the Thing String dispositivoParlante; String messaggio; Variables which are marked as READ/WRITE in the Cloud Thing will also have functions which are called when their values are changed from the Dashboard. These functions are generated with the Thing and added at the end of this sketch. */ #include "thingProperties.h" void setup() { // Initialize serial and wait for port to open: Serial.begin(9600); // This delay gives the chance to wait for a Serial Monitor without blocking if none is found delay(1500); // Defined in thingProperties.h initProperties(); // Connect to Arduino IoT Cloud ArduinoCloud.begin(ArduinoIoTPreferredConnection); /* The following function allows you to obtain more information related to the state of network and IoT Cloud connection and errors the higher number the more granular information you’ll get. The default is 0 (only errors). Maximum is 4 */ setDebugMessageLevel(2); ArduinoCloud.printDebugInfo(); } void loop() { ArduinoCloud.update(); // Your code here } /* Since Messaggio is READ_WRITE variable, onMessaggioChange() is executed every time a new value is received from IoT Cloud. */ void onMessaggioChange() { // Add your code here to act upon Messaggio change } /* Since DispositivoParlante is READ_WRITE variable, onDispositivoParlanteChange() is executed every time a new value is received from IoT Cloud. */ void onDispositivoParlanteChange() { // Add your code here to act upon DispositivoParlante change }
Mappa e Link
Parole chiave:
