//ENME 461 Lab 2 - Motors
#include <Servo.h>
Servo labservo;
int degrees = 0;
int potPin = A0;
int potVal;
int adjustedPV;
void setup() {
labservo.attach(9);
Serial.begin(9600);
}
void loop() {
POTI();
//FIVEd();
}
//part 3:
// The objective of this project is to have the servo motor arm perform a half-revolution sweep in
// discrete steps of 5 degrees and back. The servo arm will start in the position of zero degrees,
// move in discrete steps of 5 degrees to its 180° position, and dwell for a half second. It will then
// return from the 180° position to the zero-degree position in discrete steps of 5 degrees each,
// dwell for another half second, and continue in this manner indefinitely.
void FIVEd() {
labservo.write(0);
// from 0 degrees to 180 degrees
for (degrees = 0; degrees <= 180; degrees += 5) {
labservo.write(degrees);
delay(100); // Adding a delay so the servo has time to move
} delay(500); // Adding a delay after resetting to 0
//180 degrees back to 0.
for (degrees = 180; degrees >= 0; degrees -= 5) {
labservo.write(degrees);
delay(100); // Adding a delay so the servo has time to move
} delay(500);
}
// Similar to previous projects, use the multi-turn (big blue) potentiometer (the manufacturer calls it
// a rotation sensor) to control the position of the servo, where the extreme ends of the servo
// rotation correspond to the ends of the potentiometer knob turn
void POTI() {
potVal = analogRead(potPin);
Serial.println(potVal); //print to computer serial monitor.
// Mapping potentiometer value (0 to 1023) to servo angle (0 to 180 degrees)
adjustedPV = map(potVal, 0, 1023, 0, 180);
labservo.write(adjustedPV);
delay(100);
}