How to cancel an LED in Millis using bluetooth

Hello all. I’ve done some searching around Google and this forum and can’t seem to find an answer to m problem. I have a timer project that requires me to control an led from my Arduino Uno using an Android app that I created. I have been using delay for the shorter times but I have times as long as 10 minutes that I need to run and be able to cancel at any time. I found a great code from the bald engineer that I was able to tweak slightly to work with the bluetooth (his code is written for a pushbutton) but no matter what I try I can not get the led to cancel. I attached the code that I’ve been working with. ‘2’ is the OFF that I am trying to send from the bluetooth the cancel the led. Any input is greatly appreciated. :slight_smile:


test3.ino (1.6 KB)

const byte BUTTON=0; // our button pin
const byte LED=13;   // LED (built-in on Uno)

unsigned long buttonPushedMillis;  // when button was released
unsigned long ledTurnedOnAt;  // when led was turned on
unsigned long turnOnDelay = 3000; // wait to turn on LED
unsigned long turnOffDelay = 60000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.

void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

void loop() {

  unsigned long currentMillis = millis();

  // check the button
  if (digitalRead(BUTTON) == LOW) {
   // update the time when button was pushed
   buttonPushedMillis = currentMillis;
   ledReady = true;

  // make sure this code isn't checked until after button has been let go
  if (ledReady) {
    //this is typical millis code here:
    if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
       // okay, enough time has passed since the button was let go.
       digitalWrite(LED, HIGH);
       // setup our next "state"
       ledState = true;
       // save when the LED turned on
       ledTurnedOnAt = currentMillis;
       // wait for next button press
       ledReady = false;
    if (digitalRead(BUTTON) == '2'){
      digitalWrite(LED, LOW);

  // see if we are watching for the time to turn off LED
  if (ledState) {
    // okay, led on, check for now long
    if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
      ledState = false;
      digitalWrite(LED, LOW);

I found a great code from the bald engineer that I was able to tweak slightly to work with the bluetooth

I can't see what's great about that code. It looks overly complicated and under-commented.

Instead of borrowing someone else's code, write your own.

I don't even understand what you are trying to do. Nothing in that code communicates with a bluetooth device.

Describe your requirements fully.

Have a look at the demo Several Things at a Time. It uses millis() to manage timing and the button is always immediately responsive.


I don't believe that digitalRead(...) EVER returns '2', so this is not going to work:

    if (digitalRead(BUTTON) == '2'){
      digitalWrite(LED, LOW);

Thanks all for the replies. Vaj4088 you're right it wouldn't work. I was trying anything haha. PaulS I'm no programmer so to me it looked good haha So I'm making this timer for my shooting club to be able to run the competitions from outside of the booth. I made an app to run on Android which has a bunch of start buttons for different times. When I hit a start button I need to delay 3 seconds before outputting to pin 13 for a certain amount of time. Pin 13 at the club will be hooked up to a powertail relay to control a 120v outlet for speakers and a hydraulic ram. I've had the timer implemented for a year using delays but I'm getting tired of having to work around the long times. Some competitions use times up to 10 minutes which is rarely ever needed so I need to be able to cancel the long times. I have each time set up in ASCII which is where the '2' comes from. It relates to decimal 50 which is what the app sends out. I liked the code I found because I could put in the times I would need. Any more questions please let me know.

snipaz2420: but I'm getting tired of having to work around the long times.

Did you look at Reply #3 ?