Condensing a Flashing LED code

Hi all,

I am new to arduino, and have been playing around with a beginner kit for the last week. I am writing code for a series of flashing lights with a buzzer and potentiometer to control the speed.

At the moment, there is a 'powering up' phase where the lights activate in sequence, then flash and the buzzer makes two beeps. Then this reverts to the looping part where the lights blink in sequence and the buzzer beeps with them. Finally, when the switch is turned off, a 'powering down' phase activates which is pretty much the powering up phase in reverse.

I'm pretty happy with the code, but I feel like there is still refinement that can be done. The big thing I want to add is code to check if the switch is turned off during the looping phase. At the moment, if the switch is turned off, the program will step through all 7 rows of lights then trigger the 'power down'.

I played with the code, but best I could get, it included the power down in the normal loop.

See my code here;

int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int ledPin5 = 6;
int ledPin6 = 5;
int ledPin7 = 4;
int buzzPin = 2;      //Buzzer
int potPin = 0;       //Potentiometer
int switchPin = 11;    //Power Switch
int val = 0;          //Variable controlling the delay between flashes
int mindelay = 100;   //A minimum delay value added to the delay introduced by the pot. Stops the yellow lights from becoming one
int state = 0;        //Variable indicating whether or not the wand is powering up or powering down upon switch input. 0 = Power Off  1 = Looping

void setup()
{
  //pinMode initialize
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop()
{
  //check flash rate
  val == (analogRead(potPin) + mindelay);
  //check status of power switch
  if (digitalRead(switchPin) == LOW)
  {
    if (state == 1)
    {
      //power down
      poweringdown();
      state = 0;
    }
  }
  if (digitalRead(switchPin) == HIGH)
  {
    if (state == 0)
    {
      //power up
      poweringup();
      state = + 1;
    }
    else
    {
      if (state == 1)
      {
        //loop animation
        //insert a check for powerdown??
        digitalWrite(ledPin7, LOW);
        digitalWrite(ledPin1, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
        delay(val);
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay);
        delay(val);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay);
        delay(val);
        digitalWrite(ledPin3, LOW);
        digitalWrite(ledPin4, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay);
        delay(val);
        digitalWrite(ledPin4, LOW);
        digitalWrite(ledPin5, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay);
        delay(val);
        digitalWrite(ledPin5, LOW);
        digitalWrite(ledPin6, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay);
        delay(val);
        digitalWrite(ledPin6, LOW);
        digitalWrite(ledPin7, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay);
        delay(val);
      }
    }
  }
}

//Power Up Animation
void poweringup()
{
  //Powerup Lights animation
  digitalWrite(ledPin1, HIGH);
  delay(100);
  digitalWrite(ledPin2, HIGH);
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay(100);
  digitalWrite(ledPin5, HIGH);
  delay(100);
  digitalWrite(ledPin6, HIGH);
  delay(100);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 2500, 1000);
  delay(1000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
  delay(500);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 3000, 1000);
  delay(2000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
}

//Power Down Animation
void poweringdown()
{
  //Power Down Lights animation
  digitalWrite(ledPin1, HIGH);
  delay(100);
  digitalWrite(ledPin2, HIGH);
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay(100);
  digitalWrite(ledPin5, HIGH);
  delay(100);
  digitalWrite(ledPin6, HIGH);
  delay(100);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 3000, 1000);
  delay(1500);
  tone(buzzPin, 2500, 1000);
  delay(2000);
  tone(buzzPin, 2500, 1000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
}

When I look at the void loop function, I feel like there should be a way for me to change this;

digitalWrite(ledPin7, LOW);
        digitalWrite(ledPin1, HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
        delay(val);

Into one block of code that can replace the seven repeating blocks of code with something resembling this;

digitalWrite(ledPin(x), LOW);
        digitalWrite(ledPin(y), HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
        x =+ 1;
        y =+ 1;
        delay(val);

where x is the current pin and y is the next pin. And then in this I could add a check to see if the switch has been flipped, and if so, interrupt the loop to turn the lights off.

But I'm note entirely sure how to code that, and most thing I try either result in the power down code looping on itself or getting included in the general loop.

I hope this makes sense. Time for a break from coding for the evening, I am starting to confuse myself :stuck_out_tongue: Thanks for taking the time to read my question.

Start by putting the common code in a function and pass the led pin numbers as parameters

digitalWrite(ledPin(x), LOW);
        digitalWrite(ledPin(y), HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
        x =+ 1;
        y =+ 1;
        delay(val);

You are real close with this !

I think what you need is a table of the led pins like:

int LedPins= { ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7 };

then you can do:

  digitalWrite( LedPins[x], LOW);
  digitalWrite( LedPins[y], HIGH);
  tone(buzzPin, 2500, 100);
  val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
  x = x + 1;    // note: you had a typo here 
  y = y + 1;
  delay(val);

the x and y will have to go from 0 to 6 to index the LedPins array to select ledPin1 to ledPin7 when you put that into a loop.

Yours,
TonyWilk

Hmmm frustratingly, I went to solder some parts for the project and I come back to play with the code and now it skips the scrolling loop animation. It just cycles the power up and power down phases while the switch is thrown. I didn't change any code :confused: I feel like there is a short somewhere, but it's not the switch.

I took the power up and down code out to just play with the looping code as suggested above. But when typed as this;

int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int ledPin5 = 6;
int ledPin6 = 5;
int ledPin7 = 4;
int buzzPin = 2;      //Buzzer
int potPin = 0;       //Potentiometer
int switchPin = 11;    //Power Switch
int val = 0;          //Variable controlling the delay between flashes
int mindelay = 100;   //A minimum delay value added to the delay introduced by the pot. Stops the yellow lights from becoming one
int state = 0;        //Variable indicating whether or not the wand is powering up or powering down upon switch input. 0 = Power Off  1 = Looping
int x = 0;
int y = 6;
int LedPins[] = {ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7};

void setup()
{
  //pinMode initialize
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop()
{

  //loop animation
  digitalWrite(LedPins[x], LOW);
  digitalWrite(LedPins[y], HIGH);
  tone(buzzPin, 2500, 100);
  val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
  x = x + 1;
  y = y + 1;
  delay(val);

}

The lights seemingly jump around at random, sometimes not activating at all, sometimes two at once. The buzzer still beeps at the same intervals though, so I must have something wrong with respect to my array??

I even tried adding this to reset the x and y variables to 0 if they equaled 7, but no success.

  digitalWrite(LedPins[x], LOW);
  digitalWrite(LedPins[y], HIGH);
  tone(buzzPin, 2500, 100);
  val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
  x = x + 1;
  y = y + 1;
  if (x == 7)
    {
      x == 0;
    }
  if (y == 7)
    {
      y == 0;
    }
  delay(val);

Any suggestions?

You certainly need to reset the array indices back to zero when they exceed the number of entries in the array, so that is the correct approach. Whilst checking for equality using == it would in some circumstances be better to test for greater or equal to using >=

To help debug your program add some Serial.print()s of pertinent variables at significant places in the code. Are the values what you expect ?

Hmmm. That would be useful to find out the values being set to the variables, but how do I read the serial.print() function?? Does it display on my PC or do I need a LCD or other output hooked up to the board? Like I said, very rookie.

I just ran some if functions into the code to check the array values, and it works a charm!

//loop animation
  digitalWrite(LedPins[y], LOW);
  digitalWrite(LedPins[x], HIGH);
  tone(buzzPin, 2500, 100);
  val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
  if (x >= 6)
    {
      x = 0;
    }
    else
    {
      x = x + 1;
    }
  if (y >= 6)
    {
      y = 0;
    }
    else
    {
      y = y + 1;
    } 
  delay(val);

Now if I can only work out what is going on with the power up and down functions, the code would almost be there.

I also want to see if I can add in the feature to interrupt the loop animation when the switch is turned off and have all the power down sequence start when the switch is thrown, instead of waiting for the void loop() function to complete.

but how do I read the serial.print() function?? Does it display on my PC

Yes, it displays in the IDE Serial monitor. You could display it in a separate program but why bother ?

Now if I can only work out what is going on with the power up and down functions, the code would almost be there.

Can you please post the whole program as it is now.

Of course :slight_smile:

int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int ledPin5 = 6;
int ledPin6 = 5;
int ledPin7 = 4;
int buzzPin = 2;      //Buzzer
int potPin = 0;       //Potentiometer
int switchPin = 12;    //Power Switch
int val = 0;          //Variable controlling the delay between flashes
int mindelay = 100;   //A minimum delay value added to the delay introduced by the pot. Stops the yellow lights from becoming one
int state = 0;        //Variable indicating whether or not the wand is powering up or powering down upon switch input. 0 = Power Off  1 = Looping
int x = 0;
int y = 6;
int LedPins[] = {ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7};

void setup()
{
  Serial.begin(9600);
  //pinMode initialize
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop()
{
  //check flash rate
  val == (analogRead(potPin) + mindelay);
  Serial.print(state);
  //check status of power switch
  if (digitalRead(switchPin) == LOW)
  {
    if (state == 1)
    {
      //power down
      poweringdown();
      state = state - 1;
    }
  }
  if (digitalRead(switchPin) == HIGH)
  {
    if (state == 0)
    {
      //power up
      poweringup();
      state = state + 1;
    }
    else
    {
      if (state == 1)
      {
        //loop animation
        digitalWrite(LedPins[y], LOW);
        digitalWrite(LedPins[x], HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
        if (x >= 6)
        {
          x = 0;
        }
        else
        {
          x = x + 1;
        }
        if (y >= 6)
        {
          y = 0;
        }
        else
        {
          y = y + 1;
        }
        delay(val);
        Serial.print(state);
      }
    }
  }
}

//Power Up Animation
void poweringup()
{
  //Powerup Lights animation
  digitalWrite(ledPin1, HIGH);
  delay(100);
  digitalWrite(ledPin2, HIGH);
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay(100);
  digitalWrite(ledPin5, HIGH);
  delay(100);
  digitalWrite(ledPin6, HIGH);
  delay(100);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 2500, 1000);
  delay(1000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
  delay(500);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 3000, 1000);
  delay(2000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
}

//Power Down Animation
void poweringdown()
{
  //Power Down Lights animation
  digitalWrite(ledPin1, HIGH);
  delay(100);
  digitalWrite(ledPin2, HIGH);
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay(100);
  digitalWrite(ledPin5, HIGH);
  delay(100);
  digitalWrite(ledPin6, HIGH);
  delay(100);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 3000, 1000);
  delay(1500);
  tone(buzzPin, 2500, 1000);
  delay(2000);
  tone(buzzPin, 2500, 1000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
}

So now, in the debugger, state never changes from 0. even when the switch is thrown, but the lights all light up. They perform the power up, the loop animation plays for two rows of LEDs then the power down occurs, followed by the power up and the loop animation starts at the same place it stopped.

I suspect this is because I never reset x and y during the power down phase, but it’s annoying that I can’t seem to keep the three animation loops separate (hope that makes sense, saw the midnight Star Wars screening last night and I’m feeling pretty tired right now :P)

How is the switchpin input wired ?
Do you have a pullup resistor in place to keep it HIGH when not pressed or is its voltage floating at some uncertain value ? I suggest that you start by using INPUT_PULLUP in the pinMode() for the switch.

Your resetting of x and y is a little unorthodox. Normally you would change their value then immediately test whether it was out of range and reset it if it were. A neater way to reset them to zero would be to use

x++;
x %= 6;  //divide by 6 and put the remainder in x

Your code is crying out for the use of an array of LED pins

        x =+ 1;

The compiler ignores whitespace and sees that as

        x = +1;

Did you mean

        x += 1;

?

UKHeliBob:
How is the switchpin input wired ?
Do you have a pullup resistor in place to keep it HIGH when not pressed or is its voltage floating at some uncertain value ? I suggest that you start by using INPUT_PULLUP in the pinMode() for the switch.

Your resetting of x and y is a little unorthodox. Normally you would change their value then immediately test whether it was out of range and reset it if it were. A neater way to reset them to zero would be to use

x++;

x %= 6;  //divide by 6 and put the remainder in x




Your code is crying out for the use of an array of LED pins

Hmm, it would seem that when I played with the wiring I removed the pull up resistor. However, adding the line of:

pinMode(switchPin, INPUT_PULLUP);

has fixed the switch problem. Should I still work a resistor in to the circuit or is the code enough? Also, on the topic of resistors, should the LED resistor come before or after the LED. The arduino page said it dosen't matter so I figured I'd put one resistor between the cathode of all 7 LEDs and GND, though most circuits I'm looking at have a resistor before each LEDs anode (totalling 7 resistors). Is there any difference between one resistor at the collective cathode or individual resistors at each anode? They are all the same value resistor.

Thanks for the suggestion of using the better x & y addition code. I put it in and it works a treat. Checks the switch position as desired!

The only thing left to play with is condensing the code for void poweringup() and void poweringdown() but I'm not sure how best to do that.

I thought that an array had been created in a previous section with:

int LedPins[] = {ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7};

Though a quick google search revealed that I could be using for loops with an array?

The code as it stands now is;

int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int ledPin5 = 6;
int ledPin6 = 5;
int ledPin7 = 4;
int buzzPin = 2;      //Buzzer
int potPin = 0;       //Potentiometer
int switchPin = 12;    //Power Switch
int val = 0;          //Variable controlling the delay between flashes
int mindelay = 100;   //A minimum delay value added to the delay introduced by the pot. Stops the yellow lights from becoming one
int state = 0;        //Variable indicating whether or not the wand is powering up or powering down upon switch input. 0 = Power Off  1 = Looping
int x = 0;
int y = 6;
int LedPins[] = {ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7};

void setup()
{
  Serial.begin(9600);
  //pinMode initialize
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop()
{
  Serial.print("State");
  Serial.println(state);
  //check flash rate
  val == (analogRead(potPin) + mindelay);
  Serial.print(state);
  //check status of power switch
  if (digitalRead(switchPin) == LOW)
  {
    if (state == 1)
    {
      //power down
      poweringdown();
      state = state -= 1;
    }
  }
  if (digitalRead(switchPin) == HIGH)
  {
    if (state == 0)
    {
      //power up
      poweringup();
      state = state += 1;
    }
    else
    {
      if (state == 1)
      {
        //loop animation
        digitalWrite(LedPins[y], LOW);
        digitalWrite(LedPins[x], HIGH);
        tone(buzzPin, 2500, 100);
        val = (analogRead(potPin) + mindelay); //Reads the potentiometer, then adds the minimum delay
        x++;
        x %= 7;
        y++;
        y %= 7;
        delay(val);
        Serial.print("X =");
        Serial.println(x);
        Serial.print("Y =");
        Serial.println(y);
      }
    }
  }
}

//Power Up Animation
void poweringup()
{
  //Powerup Lights animation
  digitalWrite(ledPin1, HIGH);
  delay(100);
  digitalWrite(ledPin2, HIGH);
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay(100);
  digitalWrite(ledPin5, HIGH);
  delay(100);
  digitalWrite(ledPin6, HIGH);
  delay(100);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 2500, 1000);
  delay(1000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
  delay(500);
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 3000, 1000);
  delay(2000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
}

//Power Down Animation
void poweringdown()
{
  //Power Down Lights animation
  digitalWrite(ledPin1, HIGH);
  delay(100);
  digitalWrite(ledPin2, HIGH);
  delay(100);
  digitalWrite(ledPin3, HIGH);
  delay(100);
  digitalWrite(ledPin4, HIGH);
  delay(100);
  digitalWrite(ledPin5, HIGH);
  delay(100);
  digitalWrite(ledPin6, HIGH);
  delay(100);
  digitalWrite(ledPin7, HIGH);
  tone(buzzPin, 3000, 1000);
  delay(1500);
  tone(buzzPin, 2500, 1000);
  delay(2000);
  tone(buzzPin, 2500, 1000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, LOW);
  x = 0;
  y = 6;
}

aarg:

        x =+ 1;

The compiler ignores whitespace and sees that as

        x = +1;

Did you mean

        x += 1;

?

Ahh ok. I see the error there, thanks!