digitalWrite using variables

Hi,
What is wrong with this? I am trying to avoid turning on and off the laser LED manually (which works). I am using pins 3 to 7

Thanks

int laserPin;

void setup() {
// put your setup code here, to run once:
// initialize serial communication:
Serial.begin(9600);
for(int x = 3; x < 8; x++) {
pinMode(laserPin, OUTPUT);
digitalWrite(laserPin, LOW);
}
}

void loop() {
// put your main code here, to run repeatedly:
for(int x = 3; x < 8; x++) {
laserPin = x;
digitalWrite(laserPin, LOW);
delay(500);
digitalWrite(laserPin, HIGH);
delay(1000);
}
}

Hint
Look in this area:
pinMode(laserPin, OUTPUT)

As a first pass, you never use the x variable in the for loop in setup. Also, the line "laserPin = x;" is unnecessary. Also, you begin serial comms but never use it in the loop.

Also, you didn't post your code using code tags ;).

Comment out variable 'laserPin' from your code and you will know the bug.

What is wrong with this?

You have that backwards. You have some code that you posted incorrectly.

It does something that you did not explain, assuming that it even compiles.

You expect it to do something that you did not explain.

You expect us to guess what the problem is?

Thanks for the replies. I don't know how I missed the variable assignment :slight_smile: