const int button1 = 2; //connects to constantly spinning button
const int button2 = 4; //connects to timed button
const int motorPin = 9; //connects to transistor
int switchState1 = 0; //holds value of button1
int switchState2 = 0; //holds value of button2
int potentiometerIn; //holds the input from the potentiometer
int pin1 = 5; //output to the H-Bridge for motor
int pin2 = 6; //output to the H-Bridge for motor
void setup() {
pinMode(motorPin, OUTPUT); //motors are an output because they respond to the buttons
pinMode(button1, INPUT); //buttons are inputs because they initiate the motor
pinMode(button2, INPUT);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
}
void loop() {
potentiometerIn = analogRead(A0);
int output = potentiometerIn / 4; //enables input to connect to the AnalogWrite function
switchState1 = digitalRead(button1); //button1 is associated with switchstate1
switchState2 = digitalRead(button2); //button2 is associated with switchstate2
//if the buttons aren’t pressed, then the motor won’t start
if (switchState1 == LOW && switchState2 == LOW) {
digitalWrite(motorPin, LOW);
}
//else: the motor will spin
else {
digitalWrite(motorPin, HIGH);
}
//if button1 is pressed, then constantly spin
if(switchState1 == HIGH) {
analogWrite(pin1, output);
}
//else: don’t spin
else {
digitalWrite(pin1, LOW); //unpressed forward button(doesn't spin)
}
//if button 2 is pressed, then spin for a limited amount of time
if(switchState2 == HIGH) {
analogWrite(pin2, output);
}
//else: don’t spin
else {
digitalWrite(pin2, LOW);
}
delay(25); //delay at end as loop gets called continuously without any delay
}
Comments
Please log in or sign up to comment.