Labs and Works done in Physical Computing Class at SVA IXD, by Beatriz Vizcaíno.
Lab-analog output
In this lab we learned how to use libraries. The first one we used was the Servo library. In this part we first had to load the code “Controlling a servo position using a potentiometer (variable resistor)” and test the servo motor, in this case the motor will just move from 0 degrees to 180 degrees which is its maximum range. After that the code was modified to allow the potentiometer to take the servo through its entire range multiple times. The following was done to the code:
(I added various ifs with conditionals that were ranges from 0, 250, from 251 and 500, ect.)
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023
if((val >= 0) && (val<=250)) {
val = map(val, 0, 250, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
if((val >=251) && (val<=500)) {
val = map(val, 251, 500, 179, 0); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
if((val >=501) && (val<=750)) {
val = map(val, 501, 750, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
if((val >=751) && (val<=1023)) {
val = map(val, 751, 1023, 179, 0); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
}