Shorter Code

Is there any way to shorten this void loop() code. what it does is it lights 8 leds in a stream fashion, and whenever you push a button it streams faster, and a green led lights.

int pushButton = 2; //the variable to name the pin the button is connect to
int H = HIGH; //variable to make writing "HIGH" and "LOW" faster
int L = LOW;
int time = 200; //starting time




void setup() {
  pinMode(pushButton, INPUT); //The pin the button is connected to
  pinMode(12, OUTPUT);  //12-5 are the 8 leds
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT); //The green led that lights when you push the button
}


void loop() {
if (digitalRead(pushButton) == HIGH)
{
  time = 50; //Changing the delay to make the leds stream faster
  digitalWrite(4, H);
}
else
{
  time = 200;
  digitalWrite(4, L);
}  
  digitalWrite(5, L);
  digitalWrite(12, H);  
  delay(time);
if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
}   
      digitalWrite(12, L);
      digitalWrite(11, H);  
  delay(time);
  if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
} 
      digitalWrite(11, L);
      digitalWrite(10, H);
  delay(time);
  if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
} 
      digitalWrite(10, L);
      digitalWrite(9, H);
  delay(time);
  if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
} 
      digitalWrite(9, L);
      digitalWrite(8, H);
  delay(time);
  if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
} 
      digitalWrite(8, L);
      digitalWrite(7, H);
  delay(time);
  if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
} 
      digitalWrite(7, L);
      digitalWrite(6, H);
  delay(time);
  if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
} 
      digitalWrite(6, L);
      digitalWrite(5, H);
  delay(time);
  
}

Is there any way to shorten this void loop() code.

I'm pretty sure there is. I'd start by looking at arrays.

Sorry i am sort of a beginner at this stuff, i have no idea what that means :confused:

Here's one approach - should do just what yours does if I typed it right (has room for improvement using techniques as used in blink w/out delay - that would allow you prevent the up-to 0.2 sec delay when you press the button). I just did that off the top of my head; expect typos.

byte curpin=12;
#define maxpin 12
#define minpin 5

void loop() {

if (digitalRead(pushButton) == HIGH)
{
  time = 50;
  digitalWrite(4, HIGH);
}
else
{
  time = 200;
  digitalWrite(4, LOW);
}
delay(time);
digitalWrite(curpin,LOW);
if (curpin==minpin) { 
curpin=maxpin;
} else {
curpin--;
}
digitalWrite(curpin,HIGH); 
}

His leds are on sequential pins, he doesn't even need an array.

jammerbam: Sorry i am sort of a beginner at this stuff, i have no idea what that means :confused:

I bet Google does :)

...R

int H = HIGH; //variable to make writing "HIGH" and "LOW" faster

Nope.