Go Down

Topic: Stepping thru an array of pins with each button push (Read 1 time) previous topic - next topic

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: [Select]
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

robtillaart


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

at first sight the code looks OK
Rob Tillaart

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

RESET

#2
Jun 14, 2011, 09:40 pm Last Edit: Jun 14, 2011, 11:16 pm by RESET Reason: 1
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.

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?

retrolefty

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: [Select]
pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input pin
digitalWrite(buttonPin, HIGH);   // sets the internal pull-up resistor for this pin



Lefty

RESET

#5
Jun 30, 2011, 03:31 am Last Edit: Jun 30, 2011, 03:34 am by RESET Reason: 1
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.

retrolefty

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

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.

RESET

#8
Jun 30, 2011, 02:33 pm Last Edit: Jun 30, 2011, 11:31 pm by RESET Reason: 1
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.

Go Up