Brandon Palmer | Button Combo

Input: Three Buttons
Servo: 
Output
LED: Output

Troubleshooting:
Turning off all the components once the output is set to HIGH

Button Combo Code:

//Make a global variable = 0
//Make if statements with && stating if button is pressed turn global variable to 1
//Make final output to = 3 after all the buttons are pressed.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

//This is the servo
int pos = 0; // variable to store the servo position
int ledPin = 13;
int CURRENT_STATE = 0;
int OFF_STATE = 0;
int buttonOne = 9;
int buttonTwo = 10;
int buttonThree = 11;
int buttonState = 0;
int buttonPushCounter =0;
int lastButtonState = 0;

void setup() {
// put your setup code here, to run once:
myservo.attach(6); // attaches the servo on pin 9 to the servo object
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
pinMode(buttonThree, INPUT);
pinMode(ledPin, OUTPUT);

}

void loop() {

// put your main code here, to run repeatedly:
if (digitalRead(buttonOne) == HIGH && CURRENT_STATE == 0){
CURRENT_STATE = 1;
}
if (digitalRead(buttonTwo) == HIGH && CURRENT_STATE == 1){
CURRENT_STATE = 2;
}

if (digitalRead(buttonThree) == HIGH && CURRENT_STATE == 2){
CURRENT_STATE = 3;

}
if (digitalRead(buttonThree) == HIGH && CURRENT_STATE == 3){
buttonPushCounter++;
}

lastButtonState = buttonState;

if (buttonState != lastButtonState) {

}

if(CURRENT_STATE == 3){
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

if(CURRENT_STATE == 3){
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}

Leave a reply

Skip to toolbar