Pages: [1]   Go Down
Author Topic: Push button to start an array  (Read 3480 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
  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);
  }
}
« Last Edit: March 13, 2013, 12:18:27 pm by CrossRoads » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26307
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 13, 2013, 12:19:47 pm by CrossRoads » Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 518
Posts: 26307
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: