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

Mainokset

Arduino with blinking lights

Simple blinking light code with Arduino Uno. See code at the end.

Basic idea:

Light on the left side(alphaLed) blink first and after this light next to it starts and will be on until end of the loop. Then alpaLed blinks again and new light starts. This will go over and over until all lights are up. Then all light will be shutdown and loop starts over.

Working code

int alphaLed = 13;
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;

void setup() {
// setup leds
// alphaLed should be the one most left and its the one that blinks in every turn.
// See the video.

pinMode(alphaLed, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {

// Blinking leds. AlphaLed is the first led in line that blinks. AlphaLed blink every time when new led lights up.

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led1 on High mode
digitalWrite(led1, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led2 on High mode
digitalWrite(led2, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led3 on High mode
digitalWrite(led3, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led4 on High mode
digitalWrite(led4, HIGH);
delay(1000);

digitalWrite(alphaLed, HIGH);
delay(1000);
digitalWrite(alphaLed, LOW);
delay(1000);

// leave led5 on High mode
digitalWrite(led5, HIGH);
delay(1000);

// Turn off all leds and start all over

digitalWrite(alphaLed, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2000);

}

What i used:

  • Arduino
  • 6 x Leds
  • Breadboard
  • Jumper Wires

This page is licensed under a GNU General Public License v2