Coding a blink function that can also be interrupted with a switch

Hello!

I currently am having some issues with programming a code to turn my lights on and off, but also have a separate switch to turn that light on as well. i used the blink example and changed my pins to the one desired for my relay switch. the issue is i don't know how to make the switch integrated into that code. im just using the simple blink without delay sketch and could really use some help. i'm using and arduino uno with a seeeduino relay board. my input for the switch is pin 12 and the output for the relay would be pin 5. here is the code below. thank you for any help you can provide! iv'e spent countless hours on the internet trying to figure this out, you guys are my only hope! and by the way, its only set up this way because i use it as my alarm clock!

thanks
Craig Justus

#define LED_PIN 5
#define LED_ON 3600000 //milliseconds
#define LED_OFF 82800000

unsigned long ms; //time from millis()
unsigned long msLast; //last time the LED changed state
boolean ledState; //current LED state

void setup(void)
{
pinMode(LED_PIN, OUTPUT);
}

void loop(void)
{
ms = millis();
blinkLED();
}

void blinkLED(void)
{
if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}

Add a function to read the switch and save its value. If the decision to blink depends on the value of the switch then you can add an IF to the blinkLED() function.

You may find some useful stuff in Planning and Implementing a Program

...R

Thank you for such a fast reply! Actually what im trying to do is since the light is on a timer, im just trying to be able to use a switch to turn it on while the timer is running so i can still use the light normally instead of having to unplug it from the arduino just to use it. thats where im getting lost. i dont know how to code it to where ive got the loop running for my light to turn on and off, but then me be able to control it separatly with a switch without interrupting the timer.

Is the extra switch a momentary action or toggle switch ?

It is a toggle switch, just like a regular light switch

At the start or end of loop() read the state of the switch. If it is on, set a global boolean variable, let's call it flashing, to false. Wrap your existing code in if/else. If flashing is true run your current code, else turn the LED on.

UKHeliBob:
At the start or end of loop() read the state of the switch. If it is on, set a global boolean variable, let's call it flashing, to false. Wrap your existing code in if/else. If flashing is true run your current code, else turn the LED on.

What I was trying to convey in Reply #1

...R

The sames as me, but at the time you answered we did not know whether the switch was momentary or a toggle.

i tried the boolean but its not working, all its doing is keeping the LED on :frowning:

Post the code you tried.

#define LED_PIN 5 //led output
#define LED_ON 3600000 //1 hour for light on
#define LED_OFF 82800000 //23 hours light off(reset at alarm time
int switchpin = 2;

unsigned long ms; //time from millis()
unsigned long msLast; //last time the LED changed state
boolean ledState; //current LED state

void setup(void)
{
pinMode(LED_PIN, OUTPUT);
pinMode(switchpin, INPUT);
}

void loop(void)
{
{
ms = millis();
blinkLED();
}
{
digitalRead(switchpin);
if(switchpin,HIGH)
LED_PIN, HIGH;
else
LED_PIN, LOW;
}
}
void blinkLED(void)
{
if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}

i tried the boolean but its not working, all its doing is keeping the LED on :frowning:

I don't see a new boolean controlling blinking as Robin and I have been suggesting.

  {
    ms = millis();
    blinkLED();
  }
  {
    digitalRead(switchpin);  //reads the state of a pin and throws away the answer
    if (switchpin, HIGH)  //wrong syntax for an if
      LED_PIN, HIGH;     //presumably trying to set LED_PIN HIGH but it doesn't
    else
      LED_PIN, LOW;      //same problem with LOW
  }

Why so many pairs of braces ?

If all you want to do is turn the light off after it comes on, try this, you'll have to remember to turn the switch back on before next morning.

#define LED_PIN 13
#define LED_ON 6000       //milliseconds
#define LED_OFF 12000

unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
boolean ledState;        //current LED state
byte sw = 2;

void setup(void)
{
    pinMode(LED_PIN, OUTPUT);
    pinMode(sw,INPUT_PULLUP);
}

void loop(void)
{
    ms = millis();
    blinkLED();
    if(digitalRead(sw) && ledState){
      if(!digitalRead(LED_PIN)) digitalWrite(LED_PIN,HIGH);
    }
    else if(digitalRead(LED_PIN)) digitalWrite(LED_PIN,LOW);
}

void blinkLED(void)
{
    if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
        ledState = !ledState;
        msLast = ms;
    }
}

I thought booleans and if statements were the same

A “boolean” usually refers to a boolean variable whose value can be either true or false and it is also true that an if tests whether an expression is true of false but they are not the same thing.

You need something like this

start of loop()
  if blinking is true
    code here to blink the LED
  end if
  else
    turn on the LED
  end else

  if the switch is closed
    set blinking to false
  end if
  else
    set blinking to true
  end else
end of loop()

Would setting blinking to false stop or reset my timer? I really appreciate the help guys, I'm very new to programming. I just learned about Boolean 2 days ago. So thank you very very much!

Would setting blinking to false stop or reset my timer?

That is something for you to flesh out in the program. Do you want to stop/reset it or not ? You decide.

craigjustus:
Would setting blinking to false stop or reset my timer?

You need to get into the habit of writing short programs to try things out and see what happens. Doing that is very easy with an Arduino. You would probably have an answer in 5 or 10 minutes - even faster when you get more experienced.

...R