I want to perform multitasking, I don't know if it really is easy for you but I can't even make my own sketch.
So the sketch will perform these multitasking operations:
Led1 will blink (500 milliseconds) continuosly and Led2 will turn on only if the button is pressed, and even I'm pressing the button the Led1 is still blinking?
any Ideas?
thanks
jhave_21:
So the sketch will perform these multitasking operations:
Led1 will blink (500 milliseconds) continuosly and Led2 will turn on only if the button is pressed, and even I'm pressing the button the Led1 is still blinking?
any Ideas?
thanks
Apart from what the others have said, why should led1 stop blinking just because led2 is on?
What you have is this:
toggle led1 (if it's time to do so)
read button
if button pressed, led2 on
if button not pressed, led2 off
loop to top
There's nothing in the turning off of led 2 that explicitly prevents the toggling of led1.
What you need is this:
read button
if button is not pressed, toggle led1 (if it's time to do so) and off led 2
if button is pressed, on led2, and perhaps restore led1 to a default on or off state
loop to top
Though this is only pseudocode, it mixes button which relates to led2, and led1. The real code should keep them separated.
You have your main loop. You can read millis(). Just add if() blocks there that will perform things, when the condition is met. Inside the if() blocks, don't use while() or delay() or anything else that might prevent the program from advancing to the next if() block.
The first if() block could be the 500 ms blinking. Calculate millis() % 500. If remainder is less than 250, turn led1 on, otherwise turn led1 off. If you don't want to turn it on lots of times consecutively, use flags to remind that the led is already on - or off.
The second if() could check the button state and act accordingly.
More ifs could be added when needed and they wouldn't need to take in concern each other.
I want to perform multitasking, I don't know if it really is easy for you but I can't even make my own sketch.
So the sketch will perform these multitasking operations:
Led1 will blink (500 milliseconds) continuosly and Led2 will turn on only if the button is pressed, and even I'm pressing the button the Led1 is still blinking?
any Ideas?
thanks
Make a subroutine that checks button press and its done. But keep in mind 13 cycles are required for an analog read. This means your program will be slowed down. You can perform analog to digital conversion with more speed less accuracy. Since you are sensing button press, you can change the speed by changing ADCSRA register of the arduino or you can avoid analog to digital by doing digital to analog conversion. Have a nice day.