How to run a code and a loop at the same time?

Hello I'm desperately in need of solutions on how to make this work for example 3 led(green,red,yellow) will on and off continuously now if a condition is met green will increment without interrupting the led's. Sorry for noob question newbie here...

yeah, uh, what? more detail required.

You can not do two things at once. The Arduino can't, either.

What it can do is do two, or more, things in succession so quickly that it looks like "at the same time".

Imagine how you would stir pasta sauce and knead bread at the same time. Stir a little, roll a little. Repeat as required, until one job is complete.

On any given pass through loop(), it may, or may not, be time to turn one of the LEDs on or off. The blink without delay example shows how to do that for one pin. Extending that to two, three, or a hundred pins is trivial.

WHAT HAVE YOU TRIED? WHAT WERE THE RESULTS? If you want me to write the code, I get $100 per hour, and I'll start as soon as I have the first hour's fee in hand.

Have a look at the demo Several Things at a Time

...R

PaulS: You can not do two things at once. The Arduino can't, either.

What it can do is do two, or more, things in succession so quickly that it looks like "at the same time".

Imagine how you would stir pasta sauce and knead bread at the same time. Stir a little, roll a little. Repeat as required, until one job is complete.

On any given pass through loop(), it may, or may not, be time to turn one of the LEDs on or off. The blink without delay example shows how to do that for one pin. Extending that to two, three, or a hundred pins is trivial.

WHAT HAVE YOU TRIED? WHAT WERE THE RESULTS? If you want me to write the code, I get $100 per hour, and I'll start as soon as I have the first hour's fee in hand.

Yeah thank. Anyways this is what I have tried int beam = 2;//Beam sensor int greenled = 4; int redled = 7; int yellowled = 13;

int CurrentgreenDuration; //normal time int DefaultgreenDuration = 1000; int greenIncrement = 5000; //5 seconds

const long interval =1000;

unsigned long previousMillis = 0; unsigned long greenPrevMillis; float DetectionTime; unsigned long currentMillis;

enum Stoplight{Green,Yellow,Red}; Stoplight state = Green;

struct Stoplight { private: float interval; public: Stoplight() {

} void Loop() {

} };

void setup() { //Set values CurrentgreenDuration = DefaultgreenDuration;

pragma region Setup digital pins

// set the digital pin as output: pinMode(greenled, OUTPUT); pinMode(redled, OUTPUT); pinMode(greenled, OUTPUT); pinMode(beam,INPUT); Serial.begin(9600);

pragma endregion

}

void loop() { currentMillis = Millis();

Sensor();

//Initialize Stoplight //set default state //Run Timer //Counter per state interval if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis;

//manage current state transitions switch (state) { case Green: //Move to Yellow { digitalWrite(greenled, LOW); digitalWrite(yellowled, HIGH); //set interval

state = Yellow; } break; case Yellow: { digitalWrite(yellowled, LOW); digitalWrite(redled, HIGH);

state = Red; } break; case Red: { digitalWrite(redled, LOW); digitalWrite(greenled, HIGH);

state = Green; } break; } } }

void Sensor() { //If sensor detects something if (digitalRead(beam) == HIGH) { greenPrevMillis = currentMillis;

if (currentMillis - greenPrevMillis >= DetectionTime) { Serial.println("Green light duration has increased."); CurrentgreenDuration += greenIncrement; //Add 5 seconds everytime greenPrevMillis = currentMillis; } } else //No detection CurrentgreenDuration = DefaultgreenDuration; }

Robin2: Have a look at the demo Several Things at a Time

...R

its too complicated

Then try start with the blink without delay from examples.

Budvar10: Then try start with the blink without delay from examples.

tried it and it doesn't work the way I want but thanks anyway

PaulS: You can not do two things at once.

My wife can; she can burn the food and overflow the basin at the same time will doing something else :D

enum Stoplight{Green,Yellow,Red};
Stoplight state = Green;

struct Stoplight

So, what exactly is Stoplight?

Is it an enum or a struct?

 currentMillis = Millis();

The name currentMillis doesn’t mean a thing. theTimeThroughLoop or now are far better choices.

   previousMillis = currentMillis;

This would be the previous time that something happened. The name should reflect what that something is.

void Sensor()

A good function name makes it clear what the function does. From this, I can’t even begin to guess what this function is going to do.

What does “green will increment…” mean? What good does it do to increment the green duration time, when loop() never uses it, and the poorly named Sensor() function doesn’t actually use that time, either?

sterretje: My wife can; she can burn the food and overflow the basin at the same time will doing something else :D

She's not actually doing anything to burn the food, is she? Ignoring it is not actively doing anything. Same with overflowing the basin. For your sake, I hope that she doesn't read the forum. :)

@dosthefake One way to solve it to give each state it's own timing. Then yellow and red can use the interval and green can use greenDuration variable.

dosthefake: its too complicated

I suspect you will have to come to grips with that level of complexity to achieve your project.

If you explain what part of Several Things at a Time you don't understand I will try to help.

...R