Interupting and restarting loop

I am working on a project that will use arduino to control 8 printers. I have the wiring in mind but I am trying to program the task. So far I am using 8 LED's and the time they are on represent the time they will be printing. So I can get the lights to blink but i want to be able to stop the lights at any moment and restart from the same point. Currentlly I have to hold the button to eventually stop the loop.

I also know ther are better ways to write this code but I am really new at this. Please Help int switchPin2 = 2; int switchPin = 1; int led1Pin = 13; int led2Pin = 12; int led3Pin = 11; int led4Pin = 10; int led5Pin = 9; int led6Pin = 8; int led7Pin = 7; int led8Pin = 6;

int val; int val2; int buttonState; int lightMode = 0;

void setup() { pinMode(switchPin, INPUT);

pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(led3Pin, OUTPUT); pinMode(led4Pin, OUTPUT); pinMode(led5Pin, OUTPUT); pinMode(led6Pin, OUTPUT); pinMode(led7Pin, OUTPUT); pinMode(led8Pin, OUTPUT); Serial.begin(9600); // Set up serial communication at 9600bps buttonState = digitalRead(switchPin); // read the initial state }

void loop(){ val = digitalRead(switchPin2); // read input value and store it in val delay(50); // 10 milliseconds is a good amount of time val2 = digitalRead(switchPin2); // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (lightMode == 0) { // if its off lightMode = 1; // turn lights on! } else { if (lightMode == 1) { // if its all-on lightMode = 0; // make it blink! } else { if (lightMode == 2) { // if its blinking lightMode = 3; // make it wave! } else { if (lightMode == 3){ // if its waving, lightMode == 0; // turn light off! } } } } } } buttonState = val; // save the new state in our variable } if (lightMode == 0) { // test done digitalWrite(led1Pin, LOW); digitalWrite(led2Pin, LOW); digitalWrite(led3Pin, LOW); digitalWrite(led4Pin, LOW); digitalWrite(led5Pin, LOW); digitalWrite(led6Pin, LOW); digitalWrite(led7Pin, LOW); digitalWrite(led8Pin, LOW); delay(100); } if (lightMode == 1) { // "wave" digitalWrite(led1Pin, HIGH); delay(1000); digitalWrite(led1Pin, LOW); digitalWrite(led3Pin, HIGH); delay(1000); digitalWrite(led3Pin, LOW); digitalWrite(led6Pin, HIGH); delay(1000); digitalWrite(led6Pin, LOW); digitalWrite(led4Pin, HIGH); delay(1000); digitalWrite(led4Pin, LOW); digitalWrite(led5Pin, HIGH); delay(1000); digitalWrite(led5Pin, LOW); digitalWrite(led7Pin, HIGH); delay(1000); digitalWrite(led7Pin, LOW); digitalWrite(led2Pin, HIGH); delay(1000); digitalWrite(led2Pin, LOW); digitalWrite(led8Pin, HIGH); delay(1000); digitalWrite(led8Pin, LOW);

}}

Currentlly I have to hold the button to eventually stop the loop.

Do you know why?

Where do you read the switch state? At the top of loop(). Then, what happens?

Lots of calls to delay(). What happens during delay(). Nothing, nada, zip, zilch. No switch readings, no reacting to switch state changes.

You need to look at the blink without delay example to see how to make the sketch do something at different times, without needing to use the delay() function.

can you use that no delay logic to control an array of LED's?

Oh yes.

I eventually want to use the no delay code to be able to stop this but I cant get a button to start this.
Why doesnt this work???

int buttonPin = 2;     
int pinArray[] = {6, 7, 8, 9, 10, 11, 12, 13};     
int timer = 1000;
int count = 0;
int buttonState = 0;         

void setup() {
  
for (count=0;count<8;count++) {
  pinMode(pinArray[count], OUTPUT);     
  pinMode(buttonPin, INPUT);     

void loop() {
  
  buttonState = digitalRead(buttonPin);
 
  if (buttonState == HIGH) { for (count=0;count<8;count++) {
   digitalWrite(pinArray[count], HIGH);
   delay(timer);
   digitalWrite(pinArray[count], LOW);
     }

Could be a hardware issue. How is the switch wired? You are not using the internal pull-up resistor, so an external resistor is implied. The fact that you expect the switch to read HIGH when pressed implies a pull-down resistor. You do have a pull-down resistor, right?

this wont upload.

Why doesnt this work!!!!?

Well, that doesn't surprise me. The loop function is missing the closing curly brace.

It would be helpful when you posted code if you explained what the problem is. There is a big difference between "This code does not work the way I intended" and "This crap code won't even compile". Providing the right description would allow us to focus on your problem, instead of guessing what "this doesn't work" means.

Sorry about that. I have just been a little frustrated with this. I am not even really sure I am going about the solution in the right way. What I need is a button to start an array of output signals. Then read a state change, pause the array and note what led was on so that it ccould start again from the same spot. I will also have a reset button. But if I cant get the array to start with a button press i am dead before i get started. Any help will be appreciated.

Your code looks ok, apart from the missing closing braces on setup and loop. Get it to compile, run it, repost it & let us know what it does. If you're still having trouble, it's likely your wiring. Let us know what that is too.

But if I cant get the array to start with a button press i am dead before i get started. Any help will be appreciated.

Break the problem up into smaller pieces. Write a sketch that reliably reads the state of a switch - pressed or not pressed. If you can't, it's likely a hardware issue.

If you can, then add to that sketch, lighting some LEDs, using the blink without delay example to create a state machine. You have one state where one LED is on, and another state where 3 LEDs are on, for instance. Initially, transition from one state to another when the switch is pressed.

Then, expand that to transition from one state to another (having three or more states makes this easier to see that it works) every 10 seconds (for instance) and restarts at the first state when the switch is pressed.

If you get all this working, then the project you want to accomplish will be a minor extension of what you already have working.

Okay so here s an update; I have successfully used the serial port to see if my buton is HIGH or LOW/ on off. I have also used the button to activate an array(with delay blinking) the code here is my attempt to use the button to start the blink without delay(which i reallt have had trouble fully understanding). I can see in the serial port that the button is not pressed and then when i press it I get the on reading (and a count). But no blinks…

int buttonPin = 2;     
int pinArray[] = {6, 7, 8, 9, 10, 11, 12, 13};     
int count = 0;
int buttonState = LOW;
long previousMillis = 0;        
long interval = 1000;           // interval at which to blink (milliseconds)
int pinArrayState = LOW;
int buttonPushCounter = 0;
int lastButtonState;

void setup() {
 for (count=0;count<8;count++) {
  pinMode(pinArray[count], OUTPUT);     
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);    
 }}

void loop(){
  buttonState = digitalRead(buttonPin);
   
     if (buttonState == HIGH)
     {buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter, DEC);
    } 
    else 
         {Serial.println("off"); 
         
    lastButtonState = buttonState;
         }

  if (buttonState = HIGH){ 
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;   

       if (pinArray[count] == LOW)
      pinArrayState = HIGH;
    else
      pinArrayState = LOW;

    digitalWrite(pinArray[count], pinArrayState);
  }
}}

There are a number of issues with your code:

if (buttonState = HIGH)
  { 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval)
    {
    previousMillis = currentMillis;   

    if (pinArray[count] == LOW)
      pinArrayState = HIGH;
    else
      pinArrayState = LOW;

    digitalWrite(pinArray[count], pinArrayState);
    }
  }

First is the classic favourite in C: you are not testing ButtonState with the if, you're assigning it a value - use == instead. Then your use of pinArray is flawed: you are trying to use pinArray[count] but count is 8 because of your use of it in Setup, so you're reading memory outside your array Even if you weren't, you're testing the value in pinArray to see if it is low. pinArray holds pin numbers, not whether the pin is high or low you need to use digitalRead(pinArray[SomeValidIndex]) to check that.

I suggest you try getting a single led to blink at this point, then move on to the array.