Wednesday, February 22, 2012


Those of you that follow tronixstuff will have seen the tutorials on leds  ... im trying them out at the moment.  First tutorial is http://tronixstuff.wordpress.com/2010/04/04/getting-started-with-arduino-chapter-zero/
So I did this the long way (shown on chapter 0)  and then used the  ... for  .... example  which is the shorter version of code to do the same thing (shown on chapter 1)

Well I wanted to flash my leds in a different pattern  ... eg  led   2,3,4,5 >  .........   <9,8,7,6 so I had tried doing this with the for code but ran into problems ... where as doing it the long way was simple but long.
One thing I did change was with the int(I =2 ; I <=9  .... I changed this to int(I =2 ; I <=5 .... and then added a  int(t=6 ; t <=9
But I cant get it to work the way I want ... it runs allright but not the way I planned it to

Have the two code versions here .... first is the long way that works the way I want it ... second is the short way that only part works ... watch video and see one side works fine other side dosent  (swap two lines of code with each other and working side swaps)

int del=100; // sets a default delay time, 1000 milliseconds (one second)
void setup()
{
// initialize the digital pins as outputs:
// later on there will be easier ways to do this
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
// the loop() method repeats indefinitely until you turn off the power
void loop()
{
digitalWrite(2, HIGH);
digitalWrite(9, HIGH);
delay(del);            
digitalWrite(2, LOW);  
digitalWrite(9, LOW);
digitalWrite(3, HIGH);  
digitalWrite(8, HIGH);
delay(del);             
digitalWrite(3, LOW);   
digitalWrite(8, LOW);
 digitalWrite(4, HIGH);  
digitalWrite(7, HIGH);
 delay(del);             
digitalWrite(4, LOW);   
digitalWrite(7, LOW);
 digitalWrite(5, HIGH);  
digitalWrite(6, HIGH);
 delay(del);             
digitalWrite(5, LOW);   
digitalWrite(6, LOW);
 digitalWrite(6, HIGH);  
digitalWrite(5, HIGH);
delay(del);             
digitalWrite(6, LOW);   
digitalWrite(5, LOW);
digitalWrite(7, HIGH);  
digitalWrite(4, HIGH);
delay(del);             
digitalWrite(7, LOW);  
digitalWrite(4, LOW);
 digitalWrite(8, HIGH);  
digitalWrite(3, HIGH);
delay(del);             
digitalWrite(8, LOW);   
digitalWrite(3, LOW);
 digitalWrite(9, HIGH);  
digitalWrite(2, HIGH);
 delay(del);             
digitalWrite(9, LOW);   
digitalWrite(2, LOW);
 }

Second version ....

int del=100;// sets a default delay time, 1000 milliseconds (one second)
 void setup()
{
// initialize the digital pins as outputs:
for (int i = 2; i<=5 ; i++)
for  (int t = 6; t<=9 ; t ++)
{
  pinMode(i, OUTPUT);
   pinMode (t, OUTPUT);
 } // end of for loop
} // end of setup
void loop()
{
 for (int i = 2; i<=5; i++)
for (int t = 9; t>=6; t--)
 {
 digitalWrite(t, HIGH);
  digitalWrite(i, HIGH);
 delay(del);
digitalWrite(t, LOW);
 digitalWrite(i, LOW);
  }
 for (int i = 5; i>=2; i--)
for (int t = 6; t<=9; t++)
 {
 digitalWrite(t, HIGH);
  digitalWrite(i, HIGH);
 delay(del);
digitalWrite(t, LOW);
 digitalWrite(i, LOW);
  }
 }


No comments:

Post a Comment