Hi, i am very new to using arduinos and coding, but i am trying to power an led strip and make it strobe to different frequencies using a push button. Ultimately, i want it so when i press a push button once it stobes at 80 hz, when i push the second push button once it strobes at 40 hz. If there is a way to make each stobe for a certain amount of time that would be great. Thanks
Code:
int button = 15;
void setup() {
// initialize the digital pin as an output.
pinMode(12, OUTPUT);
pinMode(button,INPUT); //
digitalWrite(button,LOW);
}
void loop() {
if (digitalRead(button) == LOW){
digitalWrite(12, HIGH); // set the LED on
delay(100); //
digitalWrite(12, LOW); // set the LED off
delay(100); //
}
}
Right now, it is on all the time and then only when i hold down a button it strobes.
I'm not going to try to write your code but I'll try to give you a couple of hints...
You currently have two 100ms delays, so that's 5Hz.
You can make a variable called TimeDelay (or whatever you want) so you can change the variable to change the frequency.
You can use a an if statement to change the frequency when the button is pushed.
For example, if the frequency is 80Hz AND the button is pushed, change the frequency to 40Hz (or double the delay, or whatever).
You can make another if-statement if you want to toggle-back to 80Hz, the next time the button is pushed, etc.
You'll also probably need to [u]debounce[/u] the switch.
NOTE - At 40 or 80Hz you should be OK, but you can't read the switch during a delay, so with long delays you'll have to hold-down the switch until the program loops-around and reads the switch. The millis() method from the blink Without Delay Example an avoid that problem.
Also, there is a slight inaccuracy with the delay() method because every program-step takes a small amount of time (a few microseconds) and that time gets added to the delay().