help a noob with code...

Well, I feel like an imbecile. I took Pascal about 20 years ago, and I got a sparkfun inventors kit a week ago. Playing with the second project, 8 leds in row, to pins 2-9. I want them to light back and forth like a larson scanner(KITT, or a Cylon centurion). I think I have the algorithm right, but the code won't compile. I get an error message at the second "Do - while" statement. Can someone take a look? I'm sure its a missed bracket or semicolon somewhere. Here it is (the error message is commented in at the bottom):

//declare pins
int ledPins[] ={2,3,4,5,6,7,8,9};
/setup to run once, initializes pins to output/
void setup ()
{
for(int i=0; i<8; i++){
pinMode(ledPins*,OUTPUT); //set all 8 pins to output*

  • }*
    }
    void loop (){
  • int delayTime=100; //define delayTime as a tenth of a second*
  • int up=0;*
  • do*
  • {delay (delayTime); //wait*
  • digitalWrite(ledPins[up],HIGH);//light LED # Up*
  • if (up>0){*
  • digitalWrite(ledPins[up-1],LOW);} //turn off LED #up-1, as long as it is not less than 0*
  • up++; //add one to up*
  • }*
  • while(up<8); //keep looping as long as up is less than 8*
    }
    do //<- this 'do' command is highlighted when I get the error below
    {delay (delayTime); //wait
    digitalWrite(ledpins[up],HIGH); //light LED #up
    if (up>8){
  • digitalWrite(ledpins[up+1],LOW);} //turn off LED #up+1 as long as it is less than 8*
  • up--; //subtract one from up*
    }
    while(up>0); //keep looping as long as up is more than 0
    }

/sketch_dec08a:21: error: expected unqualified-id before 'do'/
I don't want you to do the work for me (I'll never learn that way, just tell me the problem, and where to look. thanks
-Keith

If I'm correct you want a wave that goes back and forth across each led 1 after the other, yes?

I'm going to have to go against your wishes and do this.

/*
  Arrays
 
 Demonstrates the use of  an array to hold pin numbers
 in order to iterate over the pins in a sequence. 
 Lights multiple LEDs in sequence, then in reverse.
 
 Unlike the For Loop tutorial, where the pins have to be
 contiguous, here the pins can be in any random order.
 
 The circuit:
 * LEDs from pins 2 through 7 to ground
 
 created 2006
 by David A. Mellis
 modified 5 Jul 2009
 by Tom Igoe 

This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Array
 */

int timer = 100;           // The higher the number, the slower the timing.
int ledPins[] = { 
  2, 7, 4, 6, 5, 3 ,8 ,9 };       // an array of pin numbers to which LEDs are attached
int pinCount = 8;           // the number of pins (i.e. the length of the array)

void setup() {
  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);    

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}

Two things: 1) There is a icon in the forum's editor that is just a "#" sign. This sets up the markup so you can post code neatly in the forum. 2) Clean up your curly brackets.

    if (up>0) {
       digitalWrite(ledPins[up-1],LOW);[glow]} [/glow]//turn off LED #up-1, as long as it is not less than 0
       up++; //add one to up
    } 
   while(up<8);
[glow]}[/glow]

In reading your code, I completely missed the first highlighted bracket. it looks like the one 2 lines below it belongs with the if statement. By cleaning up the code and putting that bracket on a line by itself, you'll notice the second curly bracket matches with the void loop()'s opening bracket. (which is probably not what you wanted. remove that one to move on to your next issue.)

   if (up>0) 
   {
       digitalWrite(ledPins[up-1],LOW);
    } [/glow]//turn off LED #up-1, as long as it is not less than 0
    up++; //add one to up

    } while(up<8);

Thanks, both of you -
Burrito_master - Thanks for that - if I'm reading it correctly, it turns each LED 0n then off after a pause, before moving to the next -DUH! I've been sweating over turning one on and the previous one off....

James - I knew it was something to do with brackets. Brackets and semicolons were the bane of my existence back in my Pascal days, so theres no reason that would't continue into C++.

I'll play with both of these. My next project is to have each lit LED bracketed by half-lit LEDS, so It is more true to the original KITT or Cylon...

thanks again,
Keith

My next project is to have each lit LED bracketed by half-lit LEDS, so It is more true to the original KITT or Cylon...

Hint: PWM.

Hint: PWM.

Thanks. Hmm, only 6 PWM pins on my UNO (unless I'm missing something). Shift register if I want more?

Brackets and semicolons were the bane of my existence back in my Pascal days

I recall someone in C class who knew pascal did something like the following

#define BEGIN {
#define END };
#define IF if
#define THEN
#define ELSE } else {

void setup()
BEGIN
IF (x > y) THEN BEGIN
...
ELSE
...
END
END

and he had many more ..

but i'm lsoing my mind

You are if you think were going to click on your spam.