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!
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.
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.
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.
i tried the boolean but its not working, all its doing is keeping the LED on
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
}
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!
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.