So now we’re looking at how to produce analog outputs using our Arduino. The first thing I tried was pulling y circuit apart and reassembling a simple one with just one LED. Success! I used the analogWrite() function with different values in the “duty” param and the LED indeed dimmed to my desired dimness. After it hadn’t worked at first, I realize the LED’s wire was plugged into the wrong port! Good learning experience, as those ports can be very hard to line up with the numbers. This stuff is small!
Anyway, onto the actual lab: Let’s use the Arduino to control the angle of a Servo motor…
I hooked up a Servo motor to Socket 3 (after accidentally placing it in the wrong hole, one off, and wondering why it wouldn’t work…), and uploaded code to make the force-sensor pressure set the angle of the Servo, using the following loop function:
void
loop
()
{
int
analogValue =
analogRead
(A0);
// read the analog input
Serial.println(analogValue);
// print it
int
servoAngle =
map
(analogValue, 0, 1023, 0, 179);// map pressure values to Servo angle number.
// move the servo using the angle from the sensor:
servoMotor.write(servoAngle);
}
Here’s what it looks like in action:
Pretty cool– I could definitely see using this, for instance, to trigger switches in a spectrum of degrees.