Let's Make Robots!

How to code this properly .....

Ok being distracted from my robot hack of late .... anyway being trying out the led tutorials on tronixstuff and have a little problem ...

 

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/

Video of problem at bottom of post

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);

  }

 

 

 

}

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
#define PIN_COUNT 8
#define DELAY 500

int lights[] = {2,3,4,5,6,7,8,9}; /* All the LED pins you need */

void setup(){
  for(int i=0; i<PIN_COUNT; i++){

    initOutput(lights[i]);  /* initialize LED pins */
  }
}

void loop(){
  for(int i=0; i<PIN_COUNT; i++){

  
    on(lights[i]);  /* turn light on (from left to right) */
    on(lights[reverse(i)]);  /* turn light on (from right to left) */

  
    delay(DELAY);
  
    off(lights[i]);  /* turn light off (from left to right) */

    off(lights[reverse(i)]);  /* turn light off (from right to left) */
  
  }

}

void on(int pin){
  digitalWrite(pin, HIGH);

}

void off(int pin){
  digitalWrite(pin, LOW);

}

int reverse(int positionInArray){
  return PIN_COUNT - positionInArray - 1;  /* calculate reverse order */

}

void initOutput(int pin){
  pinMode(pin, OUTPUT);

}

yep another way that works ... ty ty ... think its going to be tricky learning to code ..... 

As you learn to program, you will discover that programming a computer definately has a creative side to it and is not always just a straight way to do a particular task.  As you can see by both NilsB and my code examples, there are really multiple ways to do any one program.  Once you learn to do one particular task, you soon discover many ways to change it.  Welcome to computer science.

Maus

Yes. There is a lot of creativity here. And as many solutions as heads thinking about it.

But instead of calling it computer science I'd call it software craftmanship. It's a bit more down to earth.

As aspiring Software Craftsmen we are raising the bar of professional software development by practicing it and helping others learn the craft. Through this work we have come to value:

 Not only working software,
    but also well-crafted software

 Not only responding to change,
   but also steadily adding value

 Not only individuals and interactions,
   but also a community of professionals

 Not only customer collaboration,
   but also productive partnerships

That is, in pursuit of the items on the left we have found the items on the right to be indispensable.

But Photolong: Don't Panic. Mouse and NilsB share their software enthusiasm with you. Nothing more.

this could accompany you while learning to code:

http://clean-code-developer.com/Red-Grade.ashx

 

Your scan done with a for loop reacts the way it does because the loops are nested (ie one inside another). What occurs is very visible. The inside loop completes and then the outside loop increments and then the inside loop completes and the outside increments. So, one side flashes outside to in and then the otherside the led increments and then the otherside flashes outside to in and the otherside the led increments.

I was going to suggest something like what Maus offered, but, all kinds of dirty and just done inside the loops. 

nice this works ..... just dont know why ..........  understand the void lightUp but the (uint8_t pin1, uint8_t pin2)  i dont understand .... but im not long at code so im just trying to figure things out.

Thanks for this though .... and if your able to explain the (uint8_t pin1, uint8_t pin2) part id be very happy.

uint8_t is a unsigned integer of 8 bits in length.  It can store a value from 0-255 and I used it to take the least amount of memory.  A regular integer (int) is 2 bytes, uint8_t is 1 byte.  It's not that important now but with bigger programs, memory conservation will be very important.

Maus

I don't have the arduino ide available, but this should work.  Ask a bunch of questions. :)

 


int del=1000; // sets a default delay time, 1000 milliseconds (one second)

void setup()
{
  for (int i = 2; i < 10; i++)
  {
    pinMode(i, OUTPUT);
  }
} // end of setup

void loop()
{

  for (int i = 2; i <= 5; i++)
  {
    lightUp(i, 11 - i);
  }

  for (int i = 5; i >= 2; i--)
  {
    lightUp(i, 11 - i);
  }
}

void lightUp(uint8_t pin1, uint8_t pin2)
{
  digitalWrite(pin1, HIGH);
  digitalWrite(pin2, HIGH);
  delay(del);
  digitalWrite(pin1, LOW);
  digitalWrite(pin2, LOW);
}