I'm a bit new to this, I've got my Arduino a couple of weeks ago and I'm still in the early beginner's phase.
I've made a couple of LED's blink in different ways, but I can only get them to change into another pattern if I hold my switch button exactly when the loop starts over again.
I want to make these LED's blink in several different patterns, and I want to control it by clicking on my button. One click is pattern 1, one more click is pattern 2, one more click is pattern 3 and one more click is off. And so on...
I've thought about making a variable that counts how many times the button have been pressed, but how do I do it? It seems to me that now I have to press the button exactly when the loop ends. Is there any way to do it so that it will change immediately when I push the button? Or that it will change immediately after it's finished with the loop. So that I don't need to press the button exactly when the loop ends.
the loop is going to be running at a very high speed, what you can do is have it set a global variable when you push the button, and when the next loop comes around you set it to update accordingly
keep in mind, unless your bogging the cpu down with software its going to be doing the loop millions of times per second
As Osgeld says, the chip is running millions of calculations a second so it probably runs the loop at least 10 times even with the shortest of button presses.
You want to do something like:
int count = 1;
if(button == HIGH);{
count++
if(count == 4){
count = 1;
}
}
then use a switch case command at the start of the code to change the LED flashy...
Simple code so should be easy for you to modify and fiddle with. If you have any issues then look through the reference about switch case etc and there are some examples on those pages.
I'm at work now, so I will have to wait until I get home to try the things out.
But my blinking loops takes 500 ms and 1000 ms to go through.
I don't quite understand where I should put the loop that checks if the button is pressed. Is this a loop that will go on and on and on all the time? Even though my main loop takes 1000 ms to go through?
You need to look at the blink-without-delay example, which replaces the "delay"s with a different mechanism that will allow you to check your button presses.
Does your blinking loop use delay? If so, that stops all execution until delay ends.
The loop function WILL get called much more frequently if delay is not used.
The code to check whether the button was pressed should be referred to as a block, not a loop. And, yes that block of code goes into the loop function.
By the way, this test for the button being pressed is not very good. It will detect if a button is being pressed, but that code might get called thousands of times while the button is pressed, even if the button is pressed quickly.
What I think you really want to count is to record the old button state, and only increment count when the current button state, as reported by digitalRead is not the same as the previous state AND the current state is HIGH.
You might also want to look at the Button class library. It abstracts a lot of the low level bits away and allows for a pretty easy to understand programatic interface.
Thank you all for very good answers!
I've looked into the documentation now, and I've started to modify my little program. I'm still reading, but I will post the result when I'm finished.