/*
cible : Arduino Nano
chip Bluetooth HC05
led rouge (ou autre)
bouton
résistance 1 kOhm
*/
#include <Arduino.h>
#include <SoftwareSerial.h>
// Mapping Arduino Digital ports to Bluetooth Serial
#define rxPin A4
#define txPin A5
#define baudrateBT 38400
#define ledPin 6
#define buttonPin 2
bool bLed, bBTLed;
long unsigned int oldMillis, newMillis;
const unsigned int tempo = 500;
String msg;
SoftwareSerial hc05(rxPin, txPin);
String readhc05_Serial();
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
oldMillis = millis();
bLed = bBTLed = false;
digitalWrite(ledPin, bLed);
hc05.begin(baudrateBT);
}
void loop() {
String msg = "";
newMillis = millis();
// debug
//Serial.println("in loop(), newMillis " + String(newMillis) + ", bLed: " + String(bLed) + " bBTLed: " + String(bBTLed));
//delay(500);
if (( !digitalRead(buttonPin) && newMillis > (oldMillis + tempo)) || (bBTLed != bLed)) {
oldMillis = newMillis;
bBTLed = bLed = !bLed;
digitalWrite(ledPin, bLed);
hc05.println(bBTLed);
// debug
//Serial.println("in if(), bLed: " + String(bLed) + " bBTLed: " + String(bBTLed));
//delay(500);
}
while (hc05.available() > 0) {
int inChar = hc05.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char and add it to the string:
msg += (char)inChar;
bBTLed = msg.toInt();
}
}
}