Stop the light

so, this is gonna be a simple version - (a much better version will be done after i have this one working correctly)..

so i have 7 leds - (6 red & 1 blue.. blue being in the center)

//using BYTE vs INT because the values are less than 255

// Lights
byte led1 = 2; // red led
byte led2 = 3; // red led
byte led3 = 4; // red led
byte led4 = 5; // blue led
byte led5 = 6; // red led
byte led6 = 7; // red led
byte led7 = 8; // red led

//button
byte buttonPin = 9;
byte buttonState = 0;


void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(buttonPin, INPUT);

  // blink all the lights 3x before each new game
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(500);
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(500);
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(500);

}

void loop() {

  //Lights go one way

  //first light on
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //first light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //third light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //fourth light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //fifth light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //sixth light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, LOW);
  delay(100);
  //seventh light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, HIGH);
  delay(100);

  // Lights go the other way now

  //sixth light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, LOW);
  delay(100);
  //fifth light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //fourth light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //third light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);
  //seconf light on
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  delay(100);

  //When the player presses the button...
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) { // if the button is pressed

    // blink the lit led 3x to show which one was lit when button was pressed
// HERE IS WHERE I NEED THE HELP...........................................

// and then keep going through the sequence
//AND HERE.......................................................................

  }
}

would i be wrong in assuming i would have to store which led is currently on.. or is that not needed?

insignia:
would i be wrong in assuming i would have to store which led is currently on.. or is that not needed?

start with learning about arrays:

#define NUM_LEDS 7
//using BYTE vs INT because the values are less than 255
const byte myLed[NUM_LEDS] = {2,3,4,5,6,7,8};

//button
byte buttonPin = 9;
byte buttonState = 0;

void setup() 
{
  for(int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }
  
  // blink all the lights 3x before each new game
  
  for(int i = 0; i < 3; i++)
  {
    for(int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for(int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop() 
{

}

You have a fixed sequence for the leds with the use of delay(). That means it is not possible to check for the button.

There are a few options:
1 ) Put the sequence in a table, and run it with millis() from the table. The loop() will run very fast over and over again. In the loop also the button is checked. This will create a good base to make changes and add thing.
2 ) Instead of using delay(), use your own function that checks the button during the delay. It is a quick-and-dirty solution, but it will work.

Before you make a decision, you better create a function to shorten your sketch. That will make the sketch simpler and easier to change.

hmm.. ok.. i'll work on it.. thank you

ok, i got the lights to go 1 way and the other...

#define NUM_LEDS 7
//using BYTE vs INT because the values are less than 255
const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};

//button
byte buttonPin = 9;
byte buttonState = 0;

void setup()
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }




}

void loop()
{

  // Lights go on 1 way in sequence
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(myLed[i], HIGH);
    delay(100);
    digitalWrite(myLed[i], LOW);
  }

  // Lights go the way in sequence
  for (int i = 6; i < NUM_LEDS; i--) {
    digitalWrite(myLed[i], HIGH);
    delay(100);
    digitalWrite(myLed[i], LOW);
  }


}

but there's issues after it goes back to the first led :confused:

There are 7 leds, and the index of the array is 0...6.
You use the most common loop that is used to go up:

for (int i = 0; i < NUM_LEDS; i++)

That's perfect.

Going down is no problem when using a signed integer. Since 'i' is a 'int', that is okay. This is the common way to do that:

for (int i = NUM_LEDS - 1; i >= 0; i--)

The 'i' starts at 6 keeps decrementing as long as it is above or equal to zero.

AHHH.. thank you..

i haven't gotten to the button yet, but i see this only has 1 speed.. but what if i wanted this to start slow and when the person presses the button and the BLUE led is on.. make it go faster each time?

right now i'm just looking at it's set speed..

anyways, now i gotta attempt the button code so when the person presses it.. whatever led that is on blinks 3x quickly but the sequence continues without starting over

#define NUM_LEDS 7
//using BYTE vs INT because the values are less than 255
const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};

//button
byte buttonPin = 9;
byte buttonState = 0;
byte lastButtonState = 0;

void setup()
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }

}

void loop()
{

  // Lights go on 1 way in sequence
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(myLed[i], HIGH);
    delay(75);
    digitalWrite(myLed[i], LOW);
  }

  // Lights go the way in sequence
  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    digitalWrite(myLed[i], HIGH);
    delay(75);
    digitalWrite(myLed[i], LOW);
  }


  // Player presses button
 //blink whichever led is on 3x quickly and then continue on without starting over..
// if a red led was caught then blink it 3x quickly and start sequence from the beginning..

  buttonState = digitalRead(trigger);
  if (buttonState == LOW && lastButtonState == HIGH) {
//hmmmmm.............................................................................
  }

}

There are many ways to do that. It's important that you understand your own sketch and feel comfortable with it.

Did you make a decision about the two options in my Reply #3 ?
The delay is now fixed at 75. That has to become a variable so it can be changed.
I also would move turning the leds on and off into a function. It is possible to do the delay outside the function or inside it (with a parameter for the amount of delay).

try something like this:

#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
byte buttonPin = 9;
byte buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  static unsigned long lastMillis;
  static byte activePin = 0;
  const static unsigned long duration = 1000UL;
  static byte indexer = 1;
  const byte oldState = HIGH;
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i) 
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin == i? "X" : "O");
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  delay(30);  //crude debounce
}

void blinkThreeTimes(byte &pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin? HIGH : LOW);
      if(j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

outputs:

XOOOOOO
OXOOOOO
OOXOOOO
OOOXOOO
OOOOXOO
OOOOOXO
OOOOOOX
OOOOOXO
OOOOXOO
OOOXOOO
OOXOOOO
OXOOOOO
XOOOOOO
OXOOOOO
OOXOOOO...

@Bulldog.. each led blinks 3x slowly.. in sequence both ways.. button is useless. but thank you for attempting..

@Koepel.. not sure as for #3

BulldogLowell took a big leap forward, and I want you to have a sketch that you are confortable with.

To have a real live situation and to be able to catch that into software code, that is what programming is all about.

Some software engineers try to visualize everything as seperate modules. A button on one side and leds that go like the "knight rider" on the other side. A processing module in the middle. Then visualize splitting the processing module into seperate parts until you get to the variables. After that build functions around the seperate parts and glue everything together.

You have to decide in which way you want to go.

insignia:
@Bulldog.. each led blinks 3x slowly.. in sequence both ways.. button is useless. but thank you for attempting..

I didn't test the button bit. :confused: If you want faster blinks, turn up the speed by shortening the delays.

Delta_G:
[That would have thrown an error if you had included ...

yes, the two errors combined made it not workable, I threw the button in as a (f'ed up) freebie!

#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
byte buttonPin = 9;
byte buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  static unsigned long lastMillis;
  static byte activePin = 0;
  const static unsigned long duration = 1000UL;
  static byte indexer = 1;
  static byte oldState = HIGH; // thanks Delta_G!!!
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i) 
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin == i? "X" : "O");
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  // button read bits..
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  oldState = state;
  delay(30);  //crude debounce
  //
}

void blinkThreeTimes(byte pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin? HIGH : LOW);
      if(j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

Delta_G:
And why are you taking pin by reference if you won't be changing it?

I originally planned to move the lit led in a function and never got to changing it... the program works either way, of course.

(if Arduino wasn't 8 bit, I would have waxed on about performance...)

BulldogLowell, you don't have to use 'byte' instead of an 'int' so often. The compiler will take care of many of those. For constants, they will be put directly in the code, without using a variable.

When the array myLed[] is changed from 'byte' to 'int', that increases the code.
However when the 'inputPin', 'buttonPin', 'buttonState', 'oldState' are made 'int' the code size stays the same.
For an example sketch for someone new, it is better to use 'int'. Such advanced optimizations is not the first concern.

Koepel:
BulldogLowell, you don't have to use 'byte' instead of an 'int' so often....
...Such advanced optimizations is not the first concern.

Actually, I can use whatever the heck I want to use. >:(

Learning by example works for me so I like to show examples.

IMHO, I benefited a lot from folks here beating these minor fundamentals into my code early on.

For me, un-learning bad habits can be harder than changing one's name.

Delta_G:

byte oldState = HIGH;

You got it not const now, but it should probably be static if you really want it to represent the state of the button from the last loop.

just can't catch a break!!!

still led issues tho.. starts at the second light after the initial blink 3x startup for new game.. the button still not working correctly and the leds do not move button press is 1 led off..

#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
byte buttonPin = 9;
byte buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  static unsigned long lastMillis;
  static byte activePin = 0;
  const static unsigned long duration = 1000UL;
  static byte indexer = 1;
  static byte oldState = HIGH; // thanks Delta_G!!!
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i)
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin == i? "X" : "O");
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  // button read bits..
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  oldState = state;
  delay(30);  //crude debounce
  //
}

void blinkThreeTimes(byte pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin? HIGH : LOW);
      if(j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

i'm gonna have to play with this for awhile.. not sure what all the "serial" stuff is about... but thanks everyone

insignia:
i'm gonna have to play with this for awhile.. not sure what all the "serial" stuff is about... but thanks everyone

Use your serial monitor and see for yourself...

i went over it, but i cannot see the issue.. but here's what it does:

all 7 leds blink 3x to show start of new game.. CORRECT

then it does this...

second led blinks 3x..INCORRECT - it shouldn't blink 3x at this point and it should start with first light and go in sequence and then back.. so on and so forth..

then, after the second led blinks 3x it then does this..

moves quickly to the sixth light, then follows through with the sequence - if left alone..

but if i press the button at any time.. the button press is one led off.. it blinks the lit led 3x and then it speeds up in sequence and for 4 leds and then slows back down in sequence thing.. this happens every time the button is pressed :confused:

my wiring diagram:

#define NUM_LEDS 7

const byte myLed[NUM_LEDS] = {2, 3, 4, 5, 6, 7, 8};
const byte inputPin = 9;

//button
//byte buttonPin = 9;...........................no need for this to be here since it's declared above
byte buttonState = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP);
  for (int i = 0; i < NUM_LEDS; i++)
  {
    pinMode(myLed[i], OUTPUT);    //setup as output
  }

  // blink all the lights 3x before each new game

  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], HIGH);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

void loop()
{
  static unsigned long lastMillis;
  static byte activePin = 0;
  const static unsigned long duration = 1000UL;
  static byte indexer = 1;
  static byte oldState = HIGH; // thanks Delta_G!!!
  if (millis() - lastMillis >= duration)
  {
    for (byte i = 0; i < NUM_LEDS; i++)
    {
      if (activePin == i)
      {
        digitalWrite(myLed[i], HIGH);
      }
      else
      {
        digitalWrite(myLed[i], LOW);
      }
      Serial.print(activePin == i ? "X" : "O");
    }
    Serial.println();
    activePin += indexer;
    if (activePin == 0 || activePin == NUM_LEDS - 1)
    {
      indexer *= -1;
    }
    lastMillis += duration;
  }
  // button read bits..
  byte state = digitalRead(inputPin);
  if (oldState == HIGH && state == LOW)
  {
    blinkThreeTimes(activePin);
  }
  oldState = state;
  delay(30);  //crude debounce
  //
}

void blinkThreeTimes(byte pin)
{
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], j == pin ? HIGH : LOW);
      if (j == pin) Serial.println(pin);
    }
    delay(500);
    for (int j = 0; j < NUM_LEDS; j++)
    {
      digitalWrite(myLed[j], LOW);
    }
    delay(500);
  }
}

Pushbutton should be wired to 9 and Ground, not 5V.