Go Down

Topic: Push button to start an array (Read 3833 times) previous topic - next topic

Mar 13, 2013, 05:40 pm Last Edit: Mar 13, 2013, 06:18 pm by CrossRoads Reason: 1
I am a first time user of arduino, and am using it for an art project. I want to use a push button to start and stop an array that is hooked up to six LEDs. As I'm verifying, errors keep coming up and it is saying certain things are not declared, but they are. My professor does not know how to fix it either. Here is a copy of the sketch so far, if someone could look at it, maybe you can tell me what the problem is? Thank you!

(code tags added by moderator)
Code: [Select]

/*
 Button

Turns on and off a light emitting diode(LED) connected to digital  
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
int timer = 200;           // The higher the number, the slower the timing.
int ledPins[] = {
 2, 7, 4, 6, 5, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount = 6;           // the number of pins (i.e. the length of the array)
   
   
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
int thisPin;
 pinMode(buttonPin, INPUT);  
 // 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() {
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {    
   // start array:  
   digitalWrite(ledPins[thisPin], HIGH);
 // loop from the lowest pin to the highest:
 for (int thisPin = 0; thisPin < pinCount; thisPin++) {
   // turn the pin on:
   digitalWrite(ledPins[thisPin], HIGH);  
   delay(60);                  
   // 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(250);
   // turn the pin off:
   digitalWrite(ledPins[thisPin], LOW);
 }
}

 else {
   // stop array:
   digitalWrite(ledPin, LOW);
 }
}

CrossRoads

#1
Mar 13, 2013, 06:15 pm Last Edit: Mar 13, 2013, 06:19 pm by CrossRoads Reason: 1
move this

int thisPin;

to the declarations area, that makes it usable anywhere in the program,
and stop re-declaring it else where, such as:

for (int thisPin = 0; ...

Also, please use the code tags ( # sign ) when posting code.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

This part

// stop array:
    digitalWrite(ledPin, LOW);

ledPin is not defined anywhere. If you mean to write the whole ledPins array LOW, you have to do it in a loop.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up