Découpage du programme (split) :
Pour répartir le code dans plusieurs fichiers, à partir du programme principal. Via la flèche sous la loupe sélectionnez nouvel onglet et coller le code dans la nouvelle fenêtre après avoir nommé le fichier nouvellement créé.
http://www.robot-maker.com/index.php?/tutorials/article/30-creer-une-bibliotheque-arduino/
Conversion d’un nombre de type Float en String :
#include dtostrf(FLOAT,WIDTH, PRECISION,BUFFER);
String sMessage;
float nFloat;
char buffer[7]; //--Nombre total de chiffre y compris la virgule
//print float number
nFloat=3.45;
//dtostrf(FLOAT,WIDTH, PRECISION,BUFFER);
sMessage=dtostrf(nFloat,4,2,buffer);
Serial.println(sMessage);
http://community.thingspeak.com/forum/arduino/sending-float-data/
int to String :
int nombre;
String sNombre;
sNombre=String(nombre);
http://www.timewasters-place.com/arduino-string-and-float/
You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
- Serial.print(F(“Hello World”))
Pour saisir une valeur : int steps = Serial.parseInt();
Bien sûr la fenêtre du moniteur doit être ouverte, avec CR LF et bauds 9600
Formatage de chaîne, utilisation de sprintf :
char variable[256];
char prenom[] = "Anna";
char nom[] = "Dupond";
int age = 30;
sprintf(variable, "%s %s a %d ans", prenom, nom, age);
printf ("Resultat : %s\n", variable);
Macros :
A « #define » could be overwritten without error.
A « const int » can not be overwritten with a different value.
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define abs(x) ((x)>=0?(x):-(x))
#define bcd2bin(h,l) (((h)*10) + (l))
#define bin2bcd_h(x) ((x)/10)
#define bin2bcd_l(x) ((x)%10)
Nombre aléatoire :
SYNTAXE
long random(valeur_min, valeur_max)
randomSeed(analogRead(0)); Example
long randNumber; void setup(){ Serial.begin(9600); // if analog input pin 0 is unconnected, random analog // noise will cause the call to randomSeed() to generate // different seed numbers each time the sketch runs. // randomSeed() will then shuffle the random function. randomSeed(analogRead(0)); } void loop() { // print a random number from 0 to 299 randNumber = random(300); Serial.println(randNumber); // print a random number from 10 to 19 randNumber = random(10, 20); Serial.println(randNumber); delay(50); }
See also
https://code.google.com/p/tinkerit/wiki/TrueRandom
Entrée de données via le moniteur :
/* Read input serial */ int readSerial(char result[]) { int i = 0; while(1) { while (Serial.available() > 0) { char inChar = Serial.read(); if (inChar == '\n') //-- New Line = end of the function (after /r) { result[i] = ''; Serial.flush(); return 0; } if(inChar!='\r') //-- Carriage return : end of enterring data { result[i] = inChar; i++; } } } }
Votre commentaire