====== Gestion de trois leds via bluetooth ====== Suite à notre découverte du module bluetooth [[tel0026]] et à un premier essais [[allumer une led via bluetooth]], continuons l'apprentissage en utilisant cette fois un [[https://www.dfrobot.com/wiki/index.php/Joystick_Module_For_Arduino_SKU:DFR0061|joystick]] d'un côté et trois leds de l'autre. ===== Mise en place ====== * Arduino "master" : * Une [[https://www.dfrobot.com/product-786.html|Dreamer Nano V4.1]] avec un ATmega32u4. * Avec un [[https://www.dfrobot.com/product-68.html|shield]]. * Sur lequel on met un [[https://www.dfrobot.com/product-360.html|TEL0026]] et un [[https://www.dfrobot.com/wiki/index.php/Joystick_Module_For_Arduino_SKU:DFR0061|joystick]]. * Arduino "slave" : * Une [[https://www.arduino.cc/en/Main/arduinoBoardMega2560|Arduino MEGA 2560]] * Avec un [[https://www.dfrobot.com/product-560.html|shield]] * Sur lequel on met trois [[https://www.dfrobot.com/product-490.html|LED]]s et on connecte un TEL0026. **TODO:** Ajouter photos! ===== Code "Master" ===== /* Minimal code for testing bluetooth module TEL0026 with a joystick The circuit is based on Dreamer Nano V4.1, compatible Arduino Leonardo ( https://www.dfrobot.com/product-786.html ). TEL0026 and button are plugged on Nano I/O Shield ( https://www.dfrobot.com/product-68.html ) Created 2017 by Simon Lefort. */ const int joyX = A0; const int joyY = A1; const int joyZ = 8; //button is on pin 8 byte joyXValue = 0; byte joyYValue = 0; byte joyZValue = 0; void setup() { Serial.begin(38400); // Serial to talk with computer via USB Serial1.begin(38400); // Serial to talk to bluetooth module TEL0026 } void loop() { //lecture des capteurs joyXValue = analogRead(joyX) / 4; //équivalent à un map(valeur, 0, 1023, 0, 255) joyYValue = analogRead(joyY) / 4; joyZValue = digitalRead(joyZ); //envoi Serial1.write("X"); Serial1.write(joyXValue); Serial1.write("Y"); Serial1.write(joyYValue); Serial1.write("Z"); Serial1.write(joyZValue); Serial1.println(); //debug Serial.print("X"); Serial.print(joyXValue); Serial.print(" Y"); Serial.print(joyYValue); Serial.print(" Z"); Serial.println(joyZValue); delay(1000); } ===== Code "Slave" ===== /* Minimal code for testing bluetooth module TEL0026 with leds. This program must be used with "bluetooth-TEL0026-commande-avec-joystick.ino". The circuit is based on Arduino Mega 2560. TEL0026 and leds are plugged on a shield. Created 2017 by Simon Lefort. */ int ledPinRed = 14; int ledBrightX = 15; int ledBrightY = 16; int incomingByte = 0; String chaine = ""; byte xValue = 0; byte yValue = 0; byte zValue = 0; void setup() { Serial.begin(38400); //communicate via USB Serial1.begin(38400); //communicate via Bluetooth digitalWrite(ledBrightX, LOW); digitalWrite(ledBrightY, LOW); digitalWrite(ledPinRed, LOW); } void loop() { if (Serial1.available() > 0){ incomingByte = Serial1.read(); delay(20); chaine.concat(incomingByte); if (isAlpha(incomingByte)){ Serial.write(incomingByte); Serial.print(" "); if(incomingByte == 'X'){ xValue = Serial1.read(); chaine.concat(xValue); Serial.println(xValue); } else if(incomingByte == 'Y'){ yValue = Serial1.read(); chaine.concat(yValue); Serial.println(yValue); } else if(incomingByte == 'Z'){ zValue = Serial1.read(); chaine.concat(zValue); Serial.println(zValue); } } } analogWrite(ledBrightX, xValue); analogWrite(ledBrightY, yValue); digitalWrite(ledPinRed, zValue); delay(50); if(chaine != "") { if(incomingByte == 10 || chaine.length() > 28){ //Debug: Serial.println(chaine); chaine = ""; xValue = 0; yValue = 0; zValue = 0; } } } /* NOTES: Ce qu'on lit dans le terminal série : X 126 Y 129 Z 1 88126891299011310 */