void setup()
{
// attaches the servo on pin
// 9 to the servo
myservo.attach(9);
// initialize the serial
// communication:
Serial.begin(57600);
Serial.flush();
Serial.println(“Voltage
control servo Angle 1.0”);
}
void loop()
{
if(Serial.available())
{
char c = Serial.read();
if(c == ‘r’)
{
// read the value from
// the sensor:
sensorValue =
analogRead(sensorPin);
Serial.print
(“AnalogRead: “);
Serial.print
(sensorValue);
Serial.print
(“ Voltage: “);
Serial.print( ((5.0*
(float)sensorValue)/
1024.0), 3);
■ FIGURE 14: Voltage control
servo photo.
//float val = (float) sensorValue;
//val = (float)map(val,0.0,1023.0,0.0,180.0);
int val = (float)sensorValue;
val = (float)map(val,0,1023,0.0,180);
Serial.print(“ Angle = “);
Serial.println(val);
}
}
}
❏ Compile and run the program.
❏ Open the Serial Monitor and sequentially move the jumper between each
resistor, and send an
‘r’ to the Arduino
between each move.
❏ Verify that you
receive the text
shown in
Figure 15.
That’s all for this
month. Next time,
we’ll continue with
analog output and
learn about a
continuously variable
resistor known as a
potentiometer. NV
■ FIGURE 15: Voltage control servo output.
66 June 2014