New to urduino need help

Hello from greece... I am an very new to arduino uno and any help will be appreciate
...
I am trying to make some led's to work with a very specific way ...
I have led 1 led 2 led 3 led 4 and led 5 and a on off switch

The plan is when the switch goes to on position to trigger the following sequence:

Led 4 opens , 3 seconds later led 2 is opening (led 4 remains on) after 1 sec led 1 & 2 are opening and remain on and 1 sec later led 4 turn off (led 1,2,3 remains on and staying like that until the switch goes to off position)

When the switch goes to off

(The sequence start with led's 1,2,3,on )

Led 4 is open ...after 1 sec led's 1, 2, 3 are off after that led 5 is opens and stay open from 5 sec after that time led 5 and 4 are closing and remain close untill the switch goes to on position again

Sorry to be the one to tell you this, but you must use current limiting resistors with LEDs. And you can get more help by showing how you have everything connected in the next post, rather than people guessing what you have done.

Thanks for the reply
Yes i know about limiting resistor's....i will upload a photo after work

Hi @kostakef
It's been a while since I made any programs with Arduino, and unfortunately I don't have one to test with right now. At least it should get you going it the right direction, try this program:

int switch = 13;
int led1 = 8, led2 = 9, led3 = 10, led4 = 11, led5 = 12;

void setup()
{
  pinMode(13, INPUT);
  pinMode(8, 9, 10, 11, 12, OUTPUT);
}

int main(void)
{
    if(digitalRead(switch) == 1)
    {
    	digitalWrite(led4, HIGH);
    	delay(3000);
    	digitalRead(led2, HIGH);
    	delay(1000);
    	digitalRead(led1, HIGH);
    	delay(1000);
    	digitalRead(led4, LOW);
    }
  else{
       digitalWrite(led1, HIGH);
       delay(500);
       digitalWrite(led2, HIGH);
       delay(500);
       digitalWrite(led3, HIGH);
       digitalWrite(led4, HIGH);
       delay(1000);
       digitalRead(led1, LOW);
       digitalRead(led2, LOW);
       digitalRead(led3, LOW);
       digitalRead(led5, HIGH);
       delay(5000);
       digitalRead(led4, LOW);
       digitalRead(led5, LOW);   
  }
}

???

Maybe you meant loop() ?

Another ??? That will not even compile :wink:

Also that switch is not going to be very responsive. Maybe add some "blink without delay" zest.

If we use loop the leds will not be in the desired state with this logic. Did you test it?

No.

If you use main(), setup() will not be executed unless you call it from main(). So the pins will not be set to OUTPUT. Further no initialisations are done so delay might (or will) not work.

this is the code untill now but not work well at least the second part (it just looping )

const int switchPin = 13;             //Switch Connected to PIN 13
const int led8 = 8;                //LED Connected to PIN 8
const int led9 = 9;                //LED Connected to PIN 9
const int led10 = 10;                //LED Connected to PIN 10
const int led11 = 11;                //LED Connected to PIN 11
const int led12 = 12;                //LED Connected to PIN 12
int switchState = 0;                 // Variable for reading Switch status
void setup()
{
    pinMode(led8, OUTPUT);          //LED PIN is Output
    pinMode(led9, OUTPUT);          //LED PIN is Output
    pinMode(led10, OUTPUT);          //LED PIN is Output
    pinMode(led11, OUTPUT);          //LED PIN is Output
    pinMode(led12, OUTPUT);          //LED PIN is Output
    pinMode(switchPin, INPUT);        //Switch PIN is input with PULLUP
}
void loop()
{
    switchState = digitalRead(switchPin);   //Reads the status of the switch.
    if (switchState == LOW)                 //If the switch is pressed
    {
        digitalWrite(led9, HIGH);         //LED ON
        delay(3000);                        //3 Second Delay
        digitalWrite(led8, HIGH);         //LED ON
        delay(3000);                        //3 Second Delay
        digitalWrite(led10, HIGH);         //LED ON
        digitalWrite(led11, HIGH);         //LED ON
        delay(1000);                        //1 Second Delay
        digitalWrite(led9, LOW);          //LED OFF
        
        
    } 
   switchState = digitalRead(switchPin);   //Reads the status of the switch.
    if (switchState == HIGH)                 //If the switch is pressed 
    { 
       digitalWrite(led8, HIGH);
       digitalWrite(led10, HIGH);
       digitalWrite(led11, HIGH);
       digitalWrite(led9, HIGH);
       delay(1000);
       digitalWrite(led8, LOW);
       digitalWrite(led10, LOW);
       digitalWrite(led11, LOW);;
       digitalWrite(led12, HIGH);
       delay(5000);
       digitalWrite(led9, LOW);
       digitalWrite(led12, LOW);   
     }

}

Because what you have is essentially:

if(something == true){
  doThing1;
}

else if(something == false){
  doThing2;
}

You need to -

a. Use the switch to enable or reset a timer.
b. If the timer is enabled and the timer value is between X and Y produce a number which the state machine (switch/case construct) uses to select which case to execute.
c. if timer value is between R and S produce a different number for the state machine. And so on.
d. Important - if the timer is NOT enabled you need to generate a state for that, too.

A simple state machine: Timer counter state change toggle FSM - all at once. There are many other examples on this site and the web.

sketch.ino - Wokwi Arduino and ESP32 Simulator

here is the project if you can help will be perfect
problem is that the led 9 is not turned off (digitalWrite(led9, LOW); ) in the first part and in the second is looping the second part

thanks for the reply but i dont now yet how to use milils

Try INPUT_PULLUP for you pinMode() on that switch.

Also, in the else part, the logic will turn off led9 then immediately (next loop) turn it back on. Place a delay() after the last digitalWrite() to give yourself time to see that it went off.

Pro tip: in the wokwi, you can set all the delays quite a bit smaller, then you aren't wasting time a few seconds at a time to see if something works.

You could

# define THREE_SECONDS 700 // lie! should be 3000

and use THREE_SECONDS everywhere instead of 3000 - when the time does arrive for "real time" testing, just change the one lie 700 to 3000 and the thing will get r e a l s l o w like you want.

a7

Then it's time to learn. Put your project on hold and put "millis arduino timing" or something like that in your favorite search engine. You'll get hundreds of hits.

+1.

google

 arduino blink without delay

 arduino two things at once

 arduino finite state machine

 arduino traffic lights

and drink from the firehose. Life changing!

a7

my main problem is i can not understand why is not folowing the switch..
in theory if switch is low it must do the first part and if high the second part but i can make it work

How are the switch and pullup resistor wired? What is the value of the resistor?

sketch.ino - Wokwi Arduino and ESP32 Simulator

You don't seem to be reading our replies and thinking about what we say. Here's your sketch again, with some helpful printing statements added.

You'll just have to read it carefully. Sry.

a7

the oposite i have read all the replies and now i am reading about milis.
that is actually my first code as i am in arduino for only 15 days with no prior coding expirience