Go Down

Topic: My Code (Read 435 times) previous topic - next topic

D.J.B

Can anybody help me with my code!
Does it need work or can you give me tips!

D.J.B

/*

Tell Me What You Think Of This Code Put The Leds in the right PLACE!
By D.J.B

03:00
22/August/09

*/
int a = 7;
int b = 8;
int c = 9;
int d = 10;
int e = 11;
int f = 12;
int g = 13;
void setup ()   {
 pinMode (a, OUTPUT);
 pinMode (b, OUTPUT);
 pinMode (c, OUTPUT);
 pinMode (d, OUTPUT);
 pinMode (e, OUTPUT);
 pinMode (f, OUTPUT);
 pinMode (g, OUTPUT);
}
void loop ()    {
 digitalWrite(a,HIGH);
 delay(500);
 digitalWrite(a,LOW);
  digitalWrite(b,HIGH);
 delay(500);
 digitalWrite(b,LOW);
  digitalWrite(c,HIGH);
 delay(500);
 digitalWrite(c,LOW);
  digitalWrite(d,HIGH);
 delay(500);
 digitalWrite(d,LOW);
  digitalWrite(e,HIGH);
 delay(500);
 digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
 delay(500);
 digitalWrite(f,LOW);
  digitalWrite(g,HIGH);
 delay(500);
 digitalWrite(g,LOW);
  digitalWrite(f,HIGH);
 delay(500);
 digitalWrite(f,LOW);
  digitalWrite(e,HIGH);
 delay(500);
 digitalWrite(e,LOW);
  digitalWrite(d,HIGH);
 delay(500);
 digitalWrite(d,LOW);
  digitalWrite(c,HIGH);
 delay(500);
 digitalWrite(c,LOW);
  digitalWrite(b,HIGH);
 delay(500);
 digitalWrite(b,LOW);
  digitalWrite(a,HIGH);
 delay(500);
 digitalWrite(a,LOW);
}

Fjornir

Learn to use arrays! Learn to use loops! Let the computer do the work for you and save yourself a ton of typing. Haven't tested it but the following code is probably equivalent, is certainly more compact, and would be easier to maintain going forward.

Code: [Select]
#define PINCOUNT 6
const int pins[] = {7,8,9,10,11,12};


void setup ()   {
   for(int i=0;i<PINCOUNT;i++)
   {
       pinMode( pins[i], OUTPUT);
   }
}
void loop ()    
{
   for(int i=0;i<PINCOUNT;i++)
   {
       digitalWrite(pins[i], HIGH);
       if( (i-1>0) )
       {
          digitalWrite(pins[i-1], LOW);
       }
       else
       {
         digitalWrite(pins[PINCOUNT-1], LOW);
       }
       delay(500);
   }
}



Good first effort though, you're on the right track!

PS: There's a # sign button in the toolbar. Click it and then paste inside the tags it makes for you.

Go Up