Pages: [1]   Go Down
Author Topic: Stepping thru an array of pins with each button push  (Read 922 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all, it has been some time since I was here last. I am trying to get some code working to turn on /off each I/O pin sequentially each time the input button is pressed. Would someone be so kind as to check my code and let me know if it is going to do what I am trying to accomplish?

Code:
int INDICATOR = 13;        // choose the pin for power indicator
int buttonPin = 14;        // the number of the pushbutton pin
int timer = 500;           // Sets the timer for 0.5 seconds
int ledPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};       // an array of pin numbers to which LEDs are attached
int pinCount = 12;         // the number of pins (i.e. the length of the array)
int buttonState = 0;       // variable for reading the pushbutton status

void setup() {
pinMode(INDICATOR, OUTPUT);    // declare INDICATOR as output 
pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input
  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() {
  digitalWrite(INDICATOR, LOW);    // turn INDICATOR on
  buttonState = digitalRead(buttonPin);  // read the state of the pushbutton value
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  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);   
    }
  }
}

Thanks in advance.
Sean
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12451
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


why not run it to see if it does what it should do?

at first sight the code looks OK
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Waiting for some hardware to show up. I pieced that together from examples in tutorials with my variables thrown in and I am at work so I can't even see if it will compile. I will plug it into the software later and make sure that the code compiles.

Thanks for the review though.

Well, it compiled. I guess I just have to wait for my USB BUB to show up from Wulfden.
« Last Edit: June 14, 2011, 04:16:32 pm by RESET » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, got the new interface. I uploaded the sketch to the dorkboard and it does not work as desired. It steps thru the pins but does not wait for me to push the button each time. Why does it not read the button state every time thru the loop?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect you didn't wire a pull-down resistor on the button input pin? That would result often in the pin reading as button being pushed even when it is not, due to having an electrical 'floating input' condition. So wire a resistor (not critical to value, anything from a 1k to 20k will work fine) between the input pin and ground.

OR

A simple solution not requiring an external resistor is to rewire the button switch to ground and the input pin, rather then between +5vdc and the pin. Then enable the internal pull-up resistor for that pin and in your sketch test for button = LOW for the being pressed condition.

Setting the internal pull-up is like so:

Code:
pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input pin
digitalWrite(buttonPin, HIGH);   // sets the internal pull-up resistor for this pin


Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick reply.

My "button" is actually 5v being applied from an external source. When it is off, there is no voltage coming from it. Now that you mention "floating" I think I remember that from other electronics stuff that I have done in the past. Is there a way to pull down the pin without adding more to the circuit? Probably not huh.

EDIT: Just checked the circuit again, built these a year ago, there is a resistor between pin2 and the ground.
« Last Edit: June 29, 2011, 08:34:23 pm by RESET » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My "button" is actually 5v being applied from an external source.

So is the ground from this 'external source' wired to a arduino ground pin? That would cause problems also if not wired.

Lefty
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup, that is grounded to the circuit ground. I don't have any diodes in there so it actually power up the arduino if I trigger it, even with the circuit turned off.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So is it safe to assume that the sketch is correct and I have a hardware issue?

EDIT:
I just did a couple more tests. I initiated the program with the external trigger source and quickly disconnected the trigger from the Arduino. The program still advanced from pin1 to pin2. I am almost certain that the issue is software related rather than Hardware. I don't know much about Arduino but I am pretty good with electronics. Can someone please help me? I need this device working by the weekend.
« Last Edit: June 30, 2011, 04:31:04 pm by RESET » Logged

Pages: [1]   Go Up
Jump to: