Go Down

Topic: Utterly noob question about multiple pushbuttons (Read 529 times) previous topic - next topic

DavidH

Nov 10, 2011, 08:45 pm Last Edit: Nov 10, 2011, 09:13 pm by DavidH Reason: 1
Hi all,
My very first adventure into Arduino and as expected I have already encountered some problems.
Thing is, I need a few pushbuttons to go to each their own dig input, but I can't seem to get the code right. I have hooked them up with each their own 10K resistor to ground, but only one of the buttons seems to be read. It is not a hardware error as I have swopped them around to verify it isn't the case, but I can only get the first input in the code to read, the second one does nothing.
I have tried looking through the archives here and though there are quite a lot on multiple buttons, nothing seems to tackle this absolute beginners question...
I am trying to control a stand-alone stepper motor driver which is not only a step and direction controller but also has a pulsator built in. The different functions like enabling it and changing direction are just done by bringing a lead from each function to ground. Speed is done by voltage input - so after I solve this problem, I hope to get into PWM to control the speed. (I chose this board because it is simple and I need to control a stepper for a project kinda ASAP. First I just did it with analogue switches, but now I wanna try doing it through an Arduino and grow from there)
For now, I simply need to know how to get more than one button to send out either a HIGH or LOW dig output. Should be simple...
As said, I can get the first one to work...

This is my first code ever and hope the naming of the buttons and outputs is not too weird.
Code: [Select]


// CONSTANTS DON'T CHANGE
// Set pin number for BUTTONS:
const int buttonPinRUN = 12;     // the number of the pushbutton pin to make stepper RUN
const int buttonPinDIR = 10;     // the number of the pushbutton pin to make stepper change DIRECTION

// set pin number for OUTPUT wires to stepper controller:
const int RunStepper =  13;      // the number of the RUN wire pin
const int DIRStepper =  11;      // the number of the DIRECTION wire pin



// variables will change:
int buttonPinRUNState = 0;         // variable for reading the pushbutton status
int buttonPinDIRState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the stepper function wires pins as outputs:
 pinMode(RunStepper, OUTPUT);    
 pinMode(DIRStepper, OUTPUT);      
 // initialize the pushbuttons pin as inputs:
 pinMode(buttonPinRUN, INPUT);  
 pinMode(buttonPinDIR, INPUT);  

 
}


void loop(){
 // THIS IS TO CONTROL RUN/NOT RUN SIGNAL
 buttonPinRUNState = digitalRead(buttonPinRUN);   // reads the state of the RUN pushbutton value (checks if the pushbutton is pressed)
 
 // if it is, then buttonState is HIGH:
 if (buttonPinRUNState == HIGH) {    
 
   digitalWrite(RunStepper, LOW);  
 }
 else {

   digitalWrite(RunStepper, HIGH);
   
 }
// THIS IS TO CONTROL DIRECTION - THIS IS THE BUTTON WHICH IS NOT BEING READ...
buttonPinDIRState = digitalRead(buttonPinDIR);
   if (buttonPinRUNState == HIGH) {      
   digitalWrite(DIRStepper, LOW);  
 }
 else {
   digitalWrite(DIRStepper, LOW);
 


 }}




All my very best and hope someone out there can give an easy to understand answer to a beginner like me,
David
Photographer | Part-time Tinkerer

wildbill

You have a cut and paste issue:
Code: [Select]

// THIS IS TO CONTROL DIRECTION - THIS IS THE BUTTON WHICH IS NOT BEING READ...
buttonPinDIRState = digitalRead(buttonPinDIR);
if (buttonPinRUNState == HIGH)


You're reading buttonPinDIRState, but your if is the same as the first one, using buttonPinRUNState again.

CrossRoads

// THIS IS TO CONTROL DIRECTION - THIS IS THE BUTTON WHICH IS NOT BEING READ...
buttonPinDIRState = digitalRead(buttonPinDIR);
    if (buttonPinRUNState == HIGH) {   <<< as wildbill says, change this to buttonPinDIRState
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.

DavidH

Thanks so much wildbill & CrossRoads!
Great to wake up to these messages and I am glad it was a bit more an oversight than complete lack of any understanding - I guess that happens when trying to "proof read" code at almost four in the morning...;-) (I am in China)
Made the change and with the addition of correcting my mistake in the HIGH, LOW designations it worked.
Now off to add some more buttons, make them hold, double-click etc. and try to get PWM to work;-)

Best and have a great weekend,
David

Photographer | Part-time Tinkerer

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy