Measuring light and turning continuous rotation servos

I decided to use light measuring sensor and continuous rotation servo to turn servo based on the light ammount. I have released full code for free use.

Basic idea

Measure ammount of light and divide it with 5 to get speed for servo and then turn servo. This could be used to to automatically lower curtains when night comes maybe

//Petri Mäki
// 19.4.2013
// http://www.petrimaki.wordpress.com

#include <Servo.h>
Servo myServo;

float lightValue;
int lightPin = 1;
int servoPin = 2;
int delayTime = 1000; // ms

void setup(){
Serial.begin(9600);
myServo.attach(servoPin);
}

void loop()
{

lightValue = analogRead(lightPin);
Serial.print(”Light value: ”);
Serial.println(lightValue); //Print light value, not sure what the scales are. 1000 seems to be realy dark and close 0 means there is much light

/* Turn light value to speed. I decide to divide light value
with 5 since i think it gives good accurate. e.g 1000 / 5 = 200
where 1000 would be realy dark light value and 200 is result
for speed*/
int newSpeed = lightValue / 5;
Serial.print(”Speed: ”);
Serial.println(newSpeed);
if(newSpeed > 90) {
if (newSpeed > 180) { // Speed can only be 180 MAX
newSpeed = 180;
}

myServo.write(newSpeed);

} else {
if (newSpeed < 0) {
newSpeed = 0; //Speed cannot be -1
}
myServo.write(newSpeed);
}
delay(delayTime); //ms

}

// Continuous rotation servos
// Controlling continuous rotation servos with servo.h
// 90 = servo wont move
// 0 = Full speed to left
// 180 = Full speed to right

void servoRight(int rotatingSpeed) { // Turn right based on value
myServo.write(rotatingSpeed);
}

void servoLeft(int rotatingSpeed) { //Turn left based on value
myServo.write(rotatingSpeed);
}

What i used:

  • Arduino UNO
  • Breadboard
  • Jumper Wires
  • Light sensor (Unknown model)
  • SpringRC SM-S4303R Continuous Rotation Servo

Source:

This page is licensed under a GNU General Public License v2

Advertisement