Go Down

Topic: new to arduino, basic servo/sensor question (Read 902 times) previous topic - next topic


Dec 12, 2011, 11:22 pm Last Edit: Dec 13, 2011, 01:00 pm by AWOL Reason: 1
I'm trying to hook up a sensor to an arduino to control a servo between the angles of 0 and 85. This code seems to be registering light gradually (which is what I want), but I'm not sure how to change it from 0 to 85. Any help would be much appreciated!! Thanks.

Code: [Select]
#include <Servo.h>
Servo servo1;
int sensorPin = 0; // light sensor is on analog in 0
int sensorValue01 = 3;

void setup() {

void loop() {
sensorValue01 = analogRead (sensorPin); //value of the light sensor, normally between 0 and 1023
int angle = sensorValue01 / 6; // to obtain angle between 0 and 170
angle = angle + 5; // to obtain value between 5 and 175
      servo1.write(angle); //move the servo
    Servo::refresh(); // necessary with this library
     delay(50); // to cool the jitter…

Moderator: code tags added.


sorry! i have no idea how that emoticon got in there?! it should be attach to pin 8.


Dec 12, 2011, 11:30 pm Last Edit: Dec 13, 2011, 12:59 pm by AWOL Reason: 1
refresh hasn't been necessary for quite some time.

The emoticon is there because you didn't post the code in a code box, using the # icon on the editor's toolbar.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


okay, I deleted refresh and it still seems to be working. How do I change the values to go from 0 degrees to 85?


What value are you actually reading from the sensor, and what value are you actually writing to the servo?


Well naively, divide the reading from the sensor by 12 instead of 6, but as PeterH alludes, you really need to see what range the sensor returns in the varying light conditions you're measuring and do the math with that.

Go Up