system
199
#include <Servo.h>
//================================================================================
//PINS INDICATRICES DU RAFRAICHISSEMENT (Chaque réception)
//========================================================
#define LEDPIN_PINMODE pinMode (13, OUTPUT);
#define LEDPIN_OFF PORTB &= ~(1<<5);
#define LEDPIN_ON PORTB |= (1<<5);
//=====================================================
#define HORS_TEMPS 50000 //Hors temps de 50 ms
Servo myservo;
Servo myservo2;
byte tab_val[6];
int valeurEchel2;
int valeurEchel;
//=====================================================
//=====================================================
void setup()
{
LEDPIN_PINMODE
//Serial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
// Serial.println("enter communication");
Serial.begin(57600); // Vitesse de transmission bluetooth défini
// dans le programmmes de paramétrage esclave/maitre
for (int i = 0; i <= 5; i++) { //initialisation des tableaux de réception
tab_val[i] = 0; //de valeur en byte (00000)
delay(5);
}
for (int i = 0; i <= 5; i++) {
Serial.print(tab_val[i]);
delay(5);
}
delay(500);
}
//==================================================================================
//==================================================================================
void loop()
{
if (Serial.available() > 0) // Détection de la communication bluetooth
{
for (int i = 0; i <= 5; i++) {
tab_val [i] = Serial.read(); //Reception et enregistrement de chaque byte
long temps=micros();
while (Serial.available() == 0 && i<5 && micros()<(temps+HORS_TEMPS));
if (micros()>(temps+HORS_TEMPS)){
break; //permet de sortir de la boucle si condition vrai
}
}
if (tab_val[0] == '*' && tab_val[5] == 'F') // Detection des caractères d'identification trame (début "*" et fin "F")
{
LEDPIN_ON
// Serial.print(" CSV formatted string: ");
// Serial.print(tab_val[1] + (tab_val[2] << 8),DEC); //recompose la valeur de A0 qui avait été découpé
// Serial.print(","); //et l'imprime sur le moniteur ( tab_val[1] + (tab_val[2] << 8) = A0 )
// Serial.print(tab_val[3] + (tab_val[4] << 8),DEC); //idem
// Serial.print(",");
/*Serial.print(tab_val[5] + (tab_val[6] << 8),DEC); //Possibilité d'ajouter la réception des autres
Serial.print(","); //valeurs reçu par bluetooth des entrées analogique
Serial.print(tab_val[7] + (tab_val[8] << 8),DEC);
Serial.print(",");
Serial.print(tab_val[9] + (tab_val[10] << 8),DEC);
Serial.print(",");
Serial.print(tab_val[11] + (tab_val[12] << 8),DEC);
*/
//commande servo 1
//===================================================================
valeurEchel = map(tab_val[1] + (tab_val[2] << 8), 0, 1023, 0, 179); //tab_val[1] + (tab_val[2] << 8) : valeur de
// A0 recomposé
if(0<=valeurEchel && valeurEchel<=180) //Commande le servo uniquement si il reçoit une donnée
//comprise entre 0-180°
{
myservo.write(valeurEchel);
}
else {
// Serial.print(" pas de commande servo 1 ");
}
//==================================================================
//commande servo 2
valeurEchel2 = map(tab_val[3] + (tab_val[4] << 8), 0, 1023, 0, 179);
if(0<=valeurEchel2 && valeurEchel2 <=180) //Commande le servo uniquement si il reçoit une donnée
//comprise entre 0-180°
{
myservo2.write(valeurEchel2);
}
else {
// Serial.print(" pas de commande servo2 "); //on ne fait rien
}
//==================================================================
tab_val[0] = -1; //on efface les caractère de début et de fin
tab_val[5] = -1;
LEDPIN_OFF
}
else
{
// Serial.print("Données reçues incorrectes"); //on ne fait rien
}
// Serial.println("");
}
}