Modify servo_serial.pde so that it monitors a kill switch: - If the Kill switch
ID: 2081986 • Letter: M
Question
Modify servo_serial.pde so that it monitors a kill switch:
- If the Kill switch (P3 pushbutton) is pressed, the program should enter the Stop state.
- If the Start switch (P4 pushbutton) is pressed, the program should enter the Start state.
- In the Stop state, the Serial should not accept any commands. It should display: “Press Start switch to start servo”.
- In the Start state, the program should function normally.
- If power is disconnected and reconnected, the program should enter the Stop State.
- Hint: Think about adding a variable to keep track of your start and stop states.
- Hint: Use debug to see what your variables are seeing, and the ADI scope to see your voltages.
Here is the code for servo_serial:
servo serial #includeExplanation / Answer
The explanation to the changes in the code is explained in the comments to the commands as below-
#include <Servo.h>
Servo myservo; //create servo object to control a servo
int pos = 0; //variable to store the servo position
int myDirection;
int start = 0; //start variable to check if Start button is pressed
int stop = 0; //stop variable to check if Stop button is pressed
void setup()
{
myservo.attach(9);
Serial.begin(9600);
start = 0; //making sure that when system is restarted, it enters stop state
stop = 0; //making sure that when system is restarted, it enters stop state
Serial.println("Enter Direction (L or R):"); //prompt the user
pinMode(13, INPUT); //Define Pin 13 for taking input for Start Button
pinMode(14, INPUT); //Define Pin 14 for taking input for Stop Button
}
void loop()
{
start = digitalRead(13); //Read value from start button
stop = digitalRead(14); //Read value from stop button
if((start == HIGH) && (stop == LOW)) //Check whether only the start button is pressed. In any other case, it goes to the else condition.
{
if(Serial.available()) //check for input
{
myDirection = Serial.read(); //read the input
}
if(myDirection == 'L') //go left
{
if(pos < 180)
{
pos += 1;
}
}
else if(myDirection = 'R') //go right
{
if(pos > 0)
{
pos -= 1;
}
}
myservo.write(pos);
delay(15);
}
else
{
Serial.write("Press Start switch to start servo"); //Print if stop button is pressed or any other undesired case.
}
}
If required, you can change the input pins for the start and stop buttons.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.