Advertisement
DuboisP

Nano_button_led_hc05

Nov 25th, 2024 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | Source Code | 0 0
  1. /*
  2.      cible : Arduino Nano
  3.      chip Bluetooth HC05
  4.      led rouge (ou autre)
  5.      bouton
  6.      résistance 1 kOhm
  7. */
  8.  
  9. #include <Arduino.h>
  10. #include <SoftwareSerial.h>
  11.  
  12. // Mapping Arduino Digital ports to Bluetooth Serial
  13. #define rxPin A4
  14. #define txPin A5
  15. #define baudrateBT 38400
  16.  
  17. #define ledPin 6
  18. #define buttonPin 2
  19.  
  20. bool bLed, bBTLed;
  21. long unsigned int oldMillis, newMillis;
  22. const unsigned int tempo = 500;
  23. String msg;
  24.  
  25. SoftwareSerial hc05(rxPin, txPin);
  26. String readhc05_Serial();
  27.  
  28. void setup() {
  29.     pinMode(ledPin, OUTPUT);
  30.     pinMode(buttonPin, INPUT_PULLUP);
  31.     Serial.begin(9600);
  32.     oldMillis = millis();
  33.     bLed = bBTLed = false;
  34.     digitalWrite(ledPin, bLed);
  35.     hc05.begin(baudrateBT);
  36. }
  37.  
  38. void loop() {
  39.    String msg = "";
  40.  
  41.     newMillis = millis();
  42.  
  43.    // debug
  44.    //Serial.println("in loop(), newMillis " + String(newMillis) + ", bLed: " + String(bLed) + " bBTLed: " + String(bBTLed));
  45.    //delay(500);
  46.  
  47.     if (( !digitalRead(buttonPin)  && newMillis > (oldMillis + tempo)) || (bBTLed != bLed)) {
  48.         oldMillis = newMillis;
  49.         bBTLed = bLed = !bLed;
  50.         digitalWrite(ledPin, bLed);
  51.         hc05.println(bBTLed);
  52.       // debug
  53.       //Serial.println("in if(), bLed: " + String(bLed) + " bBTLed: " + String(bBTLed));
  54.       //delay(500);
  55.     }
  56.    
  57.    while (hc05.available() > 0) {
  58.       int inChar = hc05.read();
  59.       if (isDigit(inChar)) {
  60.          // convert the incoming byte to a char and add it to the string:
  61.          msg += (char)inChar;
  62.          bBTLed = msg.toInt();
  63.       }
  64.    }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement