How to go through each LED pattern 5 times - Arduino Assignment

I have 3 LEDS that go through 3 different patterns/cases of lighting up. I need the LEDS to go through each pattern 5 times each before going on to the next pattern.

My lecturer says that I need to change this part of my code

  state++; 
  state=state % 3;
  count = count + 1; 
  count = count % 5;

Here's the full code

const int Led1 = 13; // the number of the LED pin
const int Led2 = 12; // the number of the LED pin
const int Led3 = 11; // the number of the LED pin
const int ms = 300; // the duration of the led blink in milli seconds
int state = 0;
int count = 0;
int counter = 5;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  Serial.begin(9600);
}

void together() {
 digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
}

void traffic() {
  digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
}

void flashing() {
  digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
}

// the loop function runs over and over again forever
void loop() {
  state++; 
  state=state % 3;
  count = count + 1; 
  count = count % 5;
  Serial.print("state= ");
  Serial.print(state);
  Serial.print("  count= ");
  Serial.println(count);
  switch (state) {
    case 0:
      together();
      break;
    case 1:
      traffic();
      break;
    case 2:
      flashing();
      break;
  }
}

I've attached the full Arduino file below as well. You'll be a huge help to assist me in getting this right!

Leds2 Goodwin-NS.ino (4.3 KB)

Whenever count equals 5 then increment the state and set count back to 0.

Can You turn the poetry description into a drawing showing what You want. Tell what's the happening now?

const int Led1 = 13; // the number of the LED pin
const int Led2 = 12; // the number of the LED pin
const int Led3 = 11; // the number of the LED pin
const int ms = 300; // the duration of the led blink in milli seconds
int state = 0;
int count = 0;
int counter = 5;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  Serial.begin(9600);
}

void together() {
 digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
}

void traffic() {
  digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
}

void flashing() {
  digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, HIGH);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, HIGH);
  delay(ms);                       // wait for a second
  digitalWrite(Led1, LOW);    // turn the LED off by making the voltage LOW
  digitalWrite(Led2, LOW);
  digitalWrite(Led3, LOW);
  delay(ms);                       // wait for a second
}

// the loop function runs over and over again forever
void loop() {
  state++; 
  state=state % 3;
  count = count + 1; 
  count = count % 5;
  Serial.print("state= ");
  Serial.print(state);
  Serial.print("  count= ");
  Serial.println(count);
  switch (state) {
    case 0:
for (int i = 0; i < 5; i++){
      together();
}
      break;
    case 1:
for (int i = 0; i < 5; i++){
      traffic();
}
      break;
    case 2:
for (int i = 0; i < 5; i++){
      flashing();
}
      break;
  }
}

like this? Does this do what you're after?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.