Je cherche un moyen d'envoyer des paramètres (principalement à des fins de DEBUG) par le serial du PC vers l'Arduino.
Pour ce faire j'utilise la commande Serial.read, mais je galère pour ensuite convertir les caractères reçus en valeurs (char->int)
Voila où j'en suis :
char incomingStr[10];
char myWord[10];
byte curChar=0;
byte wordLong=0;
int myVal=0;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingStr[curChar] = Serial.read();
// say what you got:
Serial.print("I received a character: ");
Serial.print(incomingStr[curChar]);
Serial.print("-->");
Serial.println(incomingStr[curChar],DEC);
if (incomingStr[curChar]==116)
{
wordLong=curChar;
for (int i=0;i<9;i++)
{
myWord[i]=incomingStr[i];
incomingStr[i]=0;
}
curChar=0;
Serial.print("I received a word : ");
for (int i=1;i<wordLong;i++)
{
Serial.print(myWord[i]);
myVal=myVal+(myWord[i]-48)*pow(10,wordLong-i-1); // C'est ici que le calcul se fait
}
Serial.println("");
Serial.print(myVal,DEC);
Serial.println("");
myVal=0;
}
else
curChar++;
}
}
Les "mots" type que je souhaite envoyer sont du genre :
F2200t
t sert à terminer le mot, F à indiquer le paramètre à changer (fréquence par exemple), on doit donc convertir myVal doit au bout du compte être égal à 2200.
N'hésitez pas à me signaler si je complique inutilement
Ce programme me sert à trouver la bonne fréquence d'un Piezzo, il suffit de le brancher sur le PIN 8 et on change les paramètres directement à l'aide du moniteur série de l'Arduino.
#define piezPin 8 // Put the Pin number where is conneceted the piezo
// Serial reading data
char defLet,endLet; // Beggining & Ending character
int myVal; // the value is between
// Time data
unsigned long time,lasTime;
// Tone data
int freq=150;
unsigned long dura=500;
unsigned long paus=200;
void setup() {
Serial.begin(9600); // Serial begin for bi-directional use
pinMode(piezPin,OUTPUT); // Set the Piezo Pin as an output
showData(); // Only to show the beggining data
}
void loop() {
time=millis(); //What time is it please ?
if ((time-lasTime) > (dura+paus)) { // Tone without delay ;)
lasTime=time;
tone(piezPin,freq,dura);
}
if (Serial.available() > 0) { // Only if serial data arrived
defLet = Serial.read();
Serial.print("J'ai recu un mot : ");
Serial.print(defLet);
Serial.print("-");
myVal=Serial.parseInt();
Serial.print(myVal);
endLet = Serial.read();
Serial.print("-");
Serial.print(endLet);
if (endLet=='t') { // Word must finish by a 't' (lower case)
Serial.println(" VALIDE");
if (defLet=='F') freq=myVal;
if (defLet=='D') dura=myVal;
if (defLet=='P') paus=myVal;
showData(); // Show the updated data (updated only if word begin by F D or P in capital letter)
}
else Serial.println(" NONVALIDE");
}
}
void showData() {
Serial.print("Frequ: ");
Serial.print(freq);
Serial.print(" Duree: ");
Serial.print(dura);
Serial.print(" Paus: ");
Serial.println(paus);
}