light sensor controling a servo motor

Hey all!

I am new in arduino and i would like to use a light sensor resistor to control a servo motor. The functioning will be more or less like this: if you have more light the servo motor goes on one direction and if this light start do diminish the servo motor start to go on the opposite direction.

I found codes for moving servo motors and to turn on LED with light sensors. They work alone but I can not right a code that gives me the result that I want to. any suggestions?

Thanks

Ricardo N.

Hello,

First be clear, Servo-motors have only 180° turning possibility, so you would say more acurately "positioning" than "turning".

Yes you can send a servo to different positions, using "servo" or "servotimer1" library and using the value of your photoresistance to the servo angle.

If you want to turn something and change the speed and direction, you must use a DC motor with a L293 chip, a PWM out for speed and 2 binary out for direction. Search in the wiring site (http://wiring.co.org) to find design.

You will find sample in the Arduino playground.

jhoepffner

Ok. I want to use a servo motor, cause i do not need a complete revolution. But i want to make the servo goes in both directions (inside its range of action).

I am using the code bellow but it is not working... is this the right code to send a servo different positions using the photoresistance?

/* Servo control from an analog input

The minimum (minPulse) and maxiumum (maxPuluse) values will be different depending on your specific servo motor. Ideally, it should be between 1 and 2 milliseconds, but in practice, 0.5 - 2.5 milliseconds works well for me. Try different values to see what numbers are best for you.

This program uses the millis() function to keep track of when the servo was last pulsed. millis() produces an overflow error (i.e. generates a number that's too big to fit in a long variable) after about 5 days. if you're making a program that has to run for more than 5 days, you may need to account for this.

by Tom Igoe additions by Carlyn Maw Created 28 Jan. 2006 Updated 7 Jun. 2006 */

int servoPin = 0; // Control pin for servo motor int minPulse = 50; // Minimum servo position int maxPulse = 2500; // Maximum servo position int pulse = 0; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse int refreshTime = 20; // the time needed in between pulses

int analogValue = 0; // the value returned from the analog sensor int analogPin = 0; // the analog pin that the sensor's on

void setup() { pinMode(servoPin, OUTPUT); // Set servo pin as an output pin pulse = minPulse; // Set the motor position value to the minimum Serial.begin(9600); }

void loop() { analogValue = analogRead(analogPin); // read the analog input pulse = (analogValue * 80) / 10 + minPulse; // convert the analog value // to a range between minPulse // and maxPulse.

// pulse the servo again if rhe refresh time (20 ms) have passed: if (millis() - lastPulse >= refreshTime) { digitalWrite(servoPin, HIGH); // Turn the motor on delayMicroseconds(pulse); // Length of the pulse sets the motor position digitalWrite(servoPin, LOW); // Turn the motor off lastPulse = millis(); // save the time of the last pulse // print out value over the serial port printInteger(pulse);

// and a signal that serves as seperator between two values printByte(10); } }

Hello,

That’s the old way to use servos, you have now 2 different libraries: servo (you can use many sevos but you need to refresh the position, beware with delay), and servotimer1 (only 2 servos on pins 8 and 9, but refresh is inside the library, more confortable).

A proposition with servo library :

#include <Servo.h>

Servo servo1;
int sensorPin = 0; // light sensor is on analog in 0
int sensorValue01 = 0;

void setup() {
servo1.attach(8);
}

void loop() {
sensorValue01 = analogRead (sensorPin); //value of the light sensor, normaly between 0 ad 1023
int angle = sensorValue / 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(10); // to cool the jitter?
}

jhoepffner

Super! it works.

danke

Ricardo N.

i done it a few days ago without knowledge of this post…

It looks like this: http://youtube.com/watch?v=hau3xIjnx4g

The code that i used was a little bit botched… and it can be easy improved making that to do a high light mesurement and a low light mesurement… it has been modified so many times thats why there are a lot of variables and is too long :wink:

I’m newbie on programming and it works so i’m happy with the result.

#include <Servo.h>

Servo servo1; 

int valoranalogic;  
int Pinanalogic = 4;   
int marje = 800;
int led = 13;
int marje1;
int marje2;
int marje3;

void setup() {

  pinMode(1,OUTPUT);
  servo1.attach(14);
  servo1.setMaximumPulse(2200);
  pinMode(led, OUTPUT);

digitalWrite(led, HIGH);
delay(1000);                 
digitalWrite(led, LOW);  
delay(200); 
digitalWrite(led, HIGH);
delay(500);                  
digitalWrite(led, LOW);   
delay(500); 
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);   
marje1 = analogRead(Pinanalogic);
delay(100);
marje2 = analogRead(Pinanalogic);
delay(100);
marje3 = analogRead(Pinanalogic);
delay(100);
marje = (marje1 +  marje2 + marje3) / 3;
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);  
} 



void loop() {

  static int angle = 0;
  valoranalogic = analogRead(Pinanalogic);
  angle = (valoranalogic - marje) * 2 ; 
  servo1.write(angle);
  Servo::refresh();

  }

I’m working now in a servo robot arm controlled with arduino…

I doesn’t have the final desing decided… i will be similar as lynxmotion arm… but with the cheapest servos that i could find, the dynam 34g.