I have managed to make a sketch that allows me to send a one character value over BLE from an app on my phone and then run some code depending on the character.
Right now, I have it set up with 10 buttons, with each sending a different character to the Arduino. One turns on blinking mode, a bunch set the different intervals, one sets the led to just be on permanently, and one turns the LED entirely off.
I am using a board called a 'BLE-Nano' that can be found on Amazon here.
There is definitely room for improvement in my code. While my setup now does work, the biggest thing wrong with it is that I can only send over one character messages. I have no way for the Arduino to differentiate each message, so currently, I just have it checking serial for the letter or number I have assigned to its action. My method of setting the interval uses this technique, but it's not ideal. Right now, I am just sending it a number that corresponds with an interval. Ideally, I would like to send it the actual interval, and have the Arduino receive it and set the variable to it.
The second thing that I think could be improved is my way of receiving messages. I am combining the one example sketch provided with the board found on their github here and another example from the Arduino project hub here.
The last thing is something that I would like to add, but I am not entirely sure how to implement it. I would like the LED to turn off if it has been on for say 30 min, but allow me to turn it back on afterwards like how I would normally. I was thinking maybe something like a timer that gets reset each time the LED 'blink' or 'solid on' mode is turned on, and after the interval of 30 min, it sets turnOn and ledBlink to false.
I'm not sure how to improve these points, as I have been unable to find any examples that are similar enough to what I am trying to achieve. If anyone has any ideas to contribute or could help me work on this code, I would really appreciate it.
This is what I am working with now. I've tried to comment it the best I can, but let me know if anything isn't clear.
/*
* Based on Bluetooth Basic: LED ON OFF - Avishkar
* Original program creator's website - http://bit.do/Avishkar
* This program allows you to use BLE to control an LED connected to an Arduino.
*/
byte comdata;
char incomingValue = 0; // Variable for storing incomingValue
unsigned long previousMillis = 0; // will store last time LED was updated
long interval = 500; // interval at which to blink (milliseconds)
bool ledBlink = false;
bool turnOn = false;
int ledPin = 5;
void(* resetFunc) (void) = 0; // Emergency shut off - if something glitches and I don't have access to it.
void setup()
{
Serial.begin(9600); // Sets the data rate in bits per second (baud) for serial data transmission
pinMode(5, OUTPUT); // Sets digital pin 5 as output pin
}
void loop()
{
if(Serial.available() > 0)
{
Serial.write(comdata);
incomingValue = Serial.read(); // Read the incoming data and store it into variable incomingValue
Serial.print(incomingValue); // Print Value of incomingValue in Serial monitor
//Serial.print("\n"); //New line
if(incomingValue == 'a') // Blink the LED
ledBlink = true;
else if(incomingValue == 'b') // Turn off LED entirely
{
ledBlink = false; // Set LED blink to off
turnOn = false; // Set 'LED solid on' to off
}
else if(incomingValue == 'r') // Emergency shut off - if something glitches and I don't have access to it.
{
resetFunc();
}
else if(incomingValue == '0') // 25ms
interval = 25;
else if(incomingValue == '1') // 50ms
interval = 50;
else if(incomingValue == '2') // 100ms
interval = 100;
else if(incomingValue == '3') // 250ms
interval = 250;
else if(incomingValue == '4') // 500ms
interval = 500;
else if(incomingValue == '5') // 750ms
interval = 750;
else if(incomingValue == '6') // 1000ms
interval = 1000;
else if(incomingValue == '7') // Turn LED on solid
{
turnOn = true; // Set LED to turn on solid
ledBlink = false; // Set LED blink to off
}
}
unsigned long currentMillis = millis();
if ( currentMillis - previousMillis >= interval )
{
previousMillis = currentMillis; // save the last time you blinked the LED
if ( ledBlink && !turnOn ) // If I am told to blink and I am not currently being told to stay on non-stop, toggle the LED
{
digitalWrite( ledPin, !digitalRead( ledPin ) ); // Toggle LED
}
else if ( !ledBlink )
{
digitalWrite( ledPin, LOW );
}
}
if ( turnOn ) // If told to be on non-stop, set the ledPin to ON each loop
{
digitalWrite(ledPin, HIGH);
}
}