I finished a little project I worked on in which I have 5 LEDs lined up on a bread board which turn on then off in sequence. I wrote the code to get this working, and it does work, but I'm wondering if it's possible to get this shortened. Here's the code:
int x=40;
//delay time(speed)
void setup()
{
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
}
void loop ()
{
digitalWrite(3,HIGH);
delay(x);
digitalWrite(3,LOW);
digitalWrite(5,HIGH);
delay(x);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
delay(x);
digitalWrite(6,LOW);
digitalWrite(9,HIGH);
delay(x);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
delay(x);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
delay(x);
digitalWrite(11,LOW);
}
So I am wondering if there is a better way to code this. It's a pain having to digitialWrite each individual pin, considering that the pins are not numbered in order (1,2,3,4...). originally I set this up on the first 6 digital pins and created some code that would determine which pin to digitalWrite to. I had a variable (pin) that represented which pin to write to, and would simply loop a statement. The code was similar to:
int x=1;
int pin=1;
void setup()
{}
void loop()
{
if (pin==7)
{
x=-x;
}
if (pin==0)
{
x=-x;
}
pin=pin+x;
digitalWrite(pin,HIGH);
delay(100);
digitalWrite(pin,HIGH);
}
I want to be able to do the same thing, but with PWM capabilities (for a fading effect). So basically, I just want to know if there is an easier way to setup pins 3,5,6,9,10,11 So I can do something similar to my second code I posted above.