simple but terrible,,,,, for me

hi guys,

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

Hi,
Look at the IDE Example "Blink without Delay".
This will let you have the LED blinking while still checking and responding to the button press.

Is this a class project/practical?

You can only give the impression of multitasking as the code is a serial process.

Tom.. :slight_smile:

It seems you have missed the first sticky post of this topic.

Easiest would be to have LED1 blink using a timer interrupt. Then it will simply blink, pretty much regardless of what else you're doing.

Then in loop() you can just poll for a keypress to light up LED2.

Please try this tutorial with simple explains.

If you can't abide pork, the bacon is turkey-bacon, okay?

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

if button is not pressed, toggle led1...

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 don't think that this one was mentioned yet: Demonstration code for several things at the same time

sterretje:
I don't think that this one was mentioned yet:

I assumed that was what Reply #2 was referring to.

But the OP seems less interested in his/her project than we are :slight_smile:

...R

Or maybe busy learning and coming up with sharper questions, or somehow possibly has a real life?

jhave_21:
hi guys,

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.

nurimo:
Make a subroutine that checks button press and its done. But keep in mind 13 cycles are required for an analog read.

10 bit analog read takes 105 microseconds, 1680 cpu cycles to read according to the datasheets.

But since when do buttons require analog read? Mine don't!