Servo Light Gauge
Part of the Outputs section of Coddy's Arduino journey. Lesson 24 of 24.
With a needle on its arm, a servo becomes a gauge, like the speed dial in a car. Here it shows how bright it is.
As with the night light, the module's AO reads lower in brighter light: about 1023 in the dark and close to 0 in full light. A gauge should point higher when it is brighter, so the conversion runs backwards. As in the light meter, give map() the new range upside down:
int angle = map(reading, 0, 1023, 180, 0);A reading of 0, full light, becomes 180 degrees, and 1023, darkness, becomes 0. Everything in between lands in between, the other way round.
Challenge
MediumThe light sensor's AO is on A0 and the servo on pin 9. Make the arm a light gauge:
- darkness points the arm at 0 degrees, full light at 180
- use
map()with the new range backwards - print the angle, and wait 50 ms each time
Press Run and move the light slider on the sensor, then Check.
Try it yourself
#include <Servo.h>
const int lightPin = A0;
Servo arm;
void setup() {
arm.attach(9);
Serial.begin(9600);
}
void loop() {
// read the light, map it backwards to 180..0, move the arm, print the angle
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Outputs
Practice on your own: Arduino playground