A switch a 10k resistor and 7 leds

Hi all im trying to make a project using relay boards but waiting for them in the mail so im using leds for now to see if it works and i think im missing something.. What im wanting to do is turn on 7 relays with delays between them once a circuit is closed i have a switch connected just like the Button example except using A0 for the input and 7 leds from the DI/O pins to arduino ground. Everything seems to work till i open the circuit again there is like a 6 sec sometimes longer delay before all the leds turn off any ideas?

const int buttonPin = A0;

const int Relay1 = 2;
const int Relay2 = 3;
const int Relay3 = 4;
const int Relay4 = 5;
const int Relay5 = 6;
const int Relay6 = 7;
const int Relay7 = 8;

int buttonState = 0; 

void setup ()
{
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  pinMode(Relay5, OUTPUT);
  pinMode(Relay6, OUTPUT);
  pinMode(Relay7, OUTPUT);
  pinMode(buttonPin, INPUT);
 }

void loop (){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
  
  digitalWrite(Relay1, HIGH);    // #1 on
  delay(1000);                   //wait
  digitalWrite(Relay2, HIGH);    // #2 on
  delay(1500);                   //wait
  digitalWrite(Relay3, HIGH);    // #3 on
  delay(1500);                   //wait  
  digitalWrite(Relay4, HIGH);    // #4 on
  delay(1500);                   //wait   
  digitalWrite(Relay5, HIGH);    // #5 on
  delay(120000);                  //wait   
  digitalWrite(Relay6, HIGH);    // #6 on 
  delay(500);                   //wait   
  digitalWrite(Relay7, HIGH);    // #7 on
  } 
  else {
  // all off  
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
  digitalWrite(Relay4, LOW);
  digitalWrite(Relay5, LOW);
  digitalWrite(Relay6, LOW);
  digitalWrite(Relay7, LOW);
  }
}

Everything seems to work till i open the circuit again there is like a 6 sec sometimes longer delay before all the leds turn off any ideas?

Yes, get rid of all the calls to "delay()" and (Vaclav's gonna hate me for this) have a look at the blink without delay example.

(Vaclav's gonna hate me for this)

He hates everyone doesn't he?

lol ok i checked it out but i dont understand how to make it work with my code i get how it works kinda but i dont want to turn any of the leds off unless the switch is opened and if its closed again to start over

The way you have written that code every time round the loop you have a load of delays even if the LEDs are already on. You have to work through those before you can go round the loop again in order to see the switch has been turned off and the LEDs go out.

Hint - only turn on the LEDs the first time you see the button is pressed. Look at the state change example in the Arduino IDE.

Ok so i played with those in the IDE but its still not clicking very well in my head lol. I get i cant use the delay() and millis seems like the way to go but i still not to sure how to make it work how i want it to. like what would a blink without delay script look like that would turn one led on wait 5 sec then another without turning them back off. Need more hints! thanks

Before any of us can formulate an approach to this, we need to have a full specification.

OK, pressing a button (which had you been reading here, you would realise should be connected to ground and employ the INPUT_PULLUP function) will start a progressive sequence of seven relay closures. Easy enough.

What you failed to specify to us - and I suspect to yourself - is what happens when the button is released.

OK, if all closures have occurred, how long do you want them to be held after the last one? More to the point, what should happen if you release the button before the sequence is complete? Should the sequence abort? If so, instantly on the button release, or after the duration of the current relay closure?

What happens if the button "stutters" during the sequence - was pressed, but is released briefly and held again?

Only when you have decided all these things can a procedure be designed to match (and to be truthful, "Blink without delay" may not matter; depending entirely on your [u]actual[/u] requirements).

You might find this video about Blink Without Delay useful.

More hints

AWOL: More hints

I see you got there before me.

I had no idea from the title that the OP had any interest in timing things and I nearly did not look at this Thread at all.

...R

State change involves looking at both the current state of the switch and the state it was last time round the loop. Only turn on the LEDs if last time the switch was off and this time the switch is on. This prevents you going through all those delays every time like I said. There is no need to delve into millis if your problem is confined to the function of the code you posted. Otherwise there is but that was not the question you asked before.

Paul__B:
Before any of us can formulate an approach to this, we need to have a full specification.

OK, pressing a button (which had you been reading here, you would realise should be connected to ground and employ the INPUT_PULLUP function) will start a progressive sequence of seven relay closures. Easy enough.

What you failed to specify to us - and I suspect to yourself - is what happens when the button is released.

OK, if all closures have occurred, how long do you want them to be held after the last one? More to the point, what should happen if you release the button before the sequence is complete? Should the sequence abort? If so, instantly on the button release, or after the duration of the current relay closure?

What happens if the button “stutters” during the sequence - was pressed, but is released briefly and held again?

Only when you have decided all these things can a procedure be designed to match (and to be truthful, “Blink without delay” may not matter; depending entirely on your actual requirements).

Ok maybe this will help so what i want to happen is when button is pushed

LED1 ON wait 500ms then LED2 ON wait 2000ms then LED3 ON wait 2000ms then LED4 ON wait 2000ms then LED4 ON wait 2000ms the LED5 ON wait 10000ms
then LED6 on wait 500ms then LED7 on
then keep the LEDs on until the button is released then turn them all OFF

and if the button “stutters” start from the beginning.

i have a toggle switch hooked up with a 10k resistor just like you would in the button example not to sure about the INPUT_PULLUP function and that video and link were pretty helpful but i still dont get it lol this is my first arduino experience iv played with a lot of the examples but this is all new still

and if the button "stutters" start from the beginning.

OK that means you need a state machine.

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html and Robin2's several things at once http://forum.arduino.cc/index.php?topic=223286.0

Kpshc707: LED1 ON wait 500ms then LED2 ON wait 2000ms then LED3 ON wait 2000ms then LED4 ON wait 2000ms then LED4 ON wait 2000ms the LED5 ON wait 10000ms then LED6 on wait 500ms then LED7 on then keep the LEDs on until the button is released then turn them all OFF

If you implement this using the delay() function you won't be able to turn it off (whether you release the button or not) until it has progressed through the whole sequence.

If you use millis() to manage the timing you can check for the button status at any time.

...R

If you implement this using the delay() function you won't be able to turn it off (whether you release the button or not) until it has progressed through the whole sequence.

There is a half way house. Write your own delay that keeps on until millis() reaches the required value OR the button is released.

i feel like im getting closer but got a little lost on the button hows this looking?

boolean LEDstate1, LEDstate2, LEDstate3 = LOW;
const int buttonPin = A0;
const int buttonInterval = 300;
unsigned long currentMillis = 0;
unsigned long previousButtonMillis = 0;
int nextTime1 = 500;  // Do this every second or 1000 milliseconds
int nextTime2 = 1000;  // Do this every 600 milliseconds
int nextTime3 = 1500;
long int goTime1, goTime2, goTime3;


void setup(){
pinMode(buttonPin, INPUT_PULLUP);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);

goTime1 = millis();
goTime2 = millis();
goTime3 = millis();
}


void loop(){
currentMillis = millis();

if(millis() >= goTime1) functionGo1();
if(millis() >= goTime2) functionGo2();
if(millis() >= goTime3) functionGo3();
}


void functionGo1(){

if(LEDstate1 == HIGH) {

digitalWrite(2,LOW);

LEDstate1 = LOW;

}

else {

digitalWrite(2,HIGH);

LEDstate1 = HIGH;

}

goTime1 = millis() + nextTime1;

}


void functionGo2(){

if(LEDstate2 == HIGH) {

digitalWrite(3,LOW);

LEDstate2 = LOW;

}

else {

digitalWrite(3,HIGH);

LEDstate2 = HIGH;

}

goTime2 = millis() + nextTime2;

}

void functionGo3(){

if(LEDstate3 == HIGH) {

digitalWrite(4,LOW);

LEDstate3 = LOW;

}

else {

digitalWrite(4,HIGH);

LEDstate3 = HIGH;

}

goTime3 = millis() + nextTime3;

}

this stuff is confusing lol

You are not looking at the buttons at all!

They have to control your actions. You need another variable to kick off the state machine and need to reset all the LEDs whe. It is not pressed by forcing the state.

Write it down on paper before trying to encode it.

What is the application that needs this, it sounds like an arbitrary homework question?

lol sorry for my nub lvl this is a lot for me to take in. Im going to use this code once i figure it out to:

if switch is closed turn on relay #0 wait 500ms turn on relay #1 wait 2000ms turn on relay #2 wait 2000ms turn on relay #3 wait 2000ms turn on relay #4 wait 120000ms turn on relay #5 wait 1000ms turn on relay #6 and stay like that until the switch is opened and if switch "stutters" at start it over

Ok this is were im at.

const int buttonPin = 9;

const int Relay1 = 2;
const int Relay2 = 3;
const int Relay3 = 4;
const int Relay4 = 5;
const int Relay5 = 6;
const int Relay6 = 7;
const int Relay7 = 8;

int buttonState = 0; 

void setup ()
{
    pinMode(Relay1, OUTPUT);
    pinMode(Relay2, OUTPUT);
    pinMode(Relay3, OUTPUT);
    pinMode(Relay4, OUTPUT);
    pinMode(Relay5, OUTPUT);
    pinMode(Relay6, OUTPUT);
    pinMode(Relay7, OUTPUT);
    pinMode(buttonPin, INPUT);
 }

int sumFrom(int arr[], int i, int j) {
    int total = 0;
    for (int k = i; k < j; k++) {
        total += arr[k];
    }
    return total;
}

bool runIt(int watchPin[], int state) {
    // runs when HIGH
    int delays[] = {0, 1000, 1500, 1500, 1500, 10000, 1000};
    int relays[] = {Relay1, Relay2, Relay3, Relay4, Relay5, Relay6, Relay7};

    int startTime = millis();
    boolean keepGoing = true;
    int newState = digitalRead(watchPin);

    while (keepGoing) {
        for (int i = 0; i < 7; i++) {
            if (sumFrom(delays, 0, i) + startTime > millis()) {
                digitalWrite(relays[i], HIGH);
            }
        }
        
        newState = digitalRead(watchPin);
        if (newState == LOW) {
            keepGoing = true;
        } else {
            keepGoing = false;
        }
    }

    for (int i = 0; i < 7; i++) {
        digitalWrite(relays[i], LOW);
    }
}

void loop (){
    if (digitalRead(buttonPin)) {
        runIt(buttonPin, HIGH);
    }
}

but it wont compile hmm

Sorry you have still not got the hang of a state machine. All the code must be in a tight loop checking if it is time to do something OR if something has happened to change the sequence. You don't seem to be taking that into account at all.