Forum 2005-2010 (read only) > Syntax & Programs

C language - Simultaneous button push & Switch

(1/5) > >>

Hey guys,

I'm having trouble with the c language programing for my arduino board. I am trying to make it so that when I push button1, a servo motor will move one way. When I push button 2, a servo motor will move in the opposite direction. Finally if you push both buttons simultaneously it will reset the  servo motor back to a center position.

I'm not sure if my coding is right at all but have a look. I tried if/else statements at first and that seemed to work when I pushed button1 before button2 "simultaneously" but I want the part to work both ways.

If someone can get back to me on this, that would be great. Thanks

#include <Servo.h>

const int  buttonPin = 2;    
const int motorPin = 3;      
const int buttonPin2 = 4;

int buttonState = 0;        
int buttonState2 = 0;        

byte hh = 0;
byte hl = 0;
byte lh = 0;
byte ll = 0;
byte check = 0;

Servo myservo;
void setup() {
 pinMode(buttonPin, INPUT);
 pinMode(motorPin, OUTPUT);
 pinMode(buttonPin2, INPUT);
void loop() {
 buttonState = digitalRead(buttonPin);
 buttonState2 = digitalRead(buttonPin2);
hh = digitalRead(buttonPin)==HIGH && digitalRead(buttonPin2)==HIGH;
hl = digitalRead(buttonPin)==HIGH && digitalRead(buttonPin2)==LOW;
lh = digitalRead(buttonPin)==LOW && digitalRead(buttonPin2)==HIGH;
ll = digitalRead(buttonPin)==LOW & digitalRead(buttonPin2)==LOW;
check = digitalRead(buttonPin) && digitalRead(buttonPin2);
switch (check){
case hh:

case hl:
 for (int i=; i>= 0; i-=1) {
if (i == 0) i = 180;
if (digitalRead(buttonPin)== LOW) break;  

case lh:
 for (int i=; i<= 180; i+=1){
if (i == 180) i = 0;
if (digitalRead(buttonPin2)== LOW) break;  

case ll:



For starters:

Each time through the loop, you should read the buttons once and then make the decision.

If you are going to use a switch statement, you have to combine the button press information into something that has four different values, since there are four different courses of action for the four possible button conditions.

One possible way:

--- Code: ---
void loop()
   buttonState  = digitalRead(buttonPin);
   buttonState2 = digitalRead(buttonPin2);
   // Combine into a single int to use in a switch statement
   // buttonState  buttonState2    state
   //     0             0           0
   //     0             1           1
   //     1             0           2
   //     1             1           3
   int state = (buttonState << 1) | buttonState2;

   switch (state) {

   case 0:
       // Both are zero.
       // Do whatever is required for this condition.

   case 1:
       // buttonState is zero and buttonState2 is one
       // Do whatever is required for this condition;

   case 2:
       // buttonState is one and buttonState2 is zero
       // Do whatever is required for this condition;

   case 3:
       // Both are one.
       // Do whatever is required for this condition;

   // Do whatever you need to do after handling the buttons.


--- End code ---

Note: Debouncing may be required.  "Simultaneous" requirements may impose additional delay considerations.



You don't say how you've got your buttons wired or what sort of buttons they are, but I notice you're not using the built-in pullups.
This may cause you problems.

Hey guys, I'm definitely not skilled in the c-language as its been a couple years since I have taken a programming class so bear with me (I know basic c programming, so the bitwise operators are still somewhat foreign to me since I have not used them enough). I tried something what davekw7x was doing but am still having trouble with the bit information. I put a serial print to see what values were coming out and I am only getting cases of 1 and 2 but not 0 or 3 of when I try all four different combinations. The program now seems to ignore the other button when I push a button.

AWOL: I'm not sure what you mean by built-in pullups. My buttons only have two wire connections coming out of them, they are push buttons. One end I have the 5v source and the other I have a resister to ground connection in parallel with input pins to the arduino board.

My other idea I was having a problem with was to figure out that if I push one of the buttons, that as long as it is held down, the program will continuously check to see that the 2nd button is pushed whether button1 is held down first or button 2 is in order to get the "simultaneous" action when button1 and button2 are both pushed down within each other's time interval.

Basically my criteria is:

[*]On power up (void setup()), have the servo go to the center of its motion.

[*]Pressing and holding down Button1 will cause servo to rotate clockwise at approximately 90 degrees per 5 seconds (18 degrees/second). When I release Button1, the servo should remain at that position.

[*]Pressing and holding down Button2 does the same thing but moves in counter-clockwise direction instead.

[*]The last criteria is to reset the motor to its center position by pressing both buttons down simultaneously.


for this tid bit
int state = (buttonState << 1) | buttonState2;

wouldn't "&" be better in this case? For buttonState I would assume that I would get values of 1 and 2 while buttonState2 would give me 0 and 1 and with those 4 values I can get a range from 0 to 3 for the switch/cases.


[0] Message Index

[#] Next page

Go to full version