need some help with project using servo-motors and light sensors

Hi guys, i’m new here, and generally quite new to arduino.

I need some help with a project i’m doing:

It’s basically a servo-motor that’s connected to three light sensors.

The light sensors will be attatched to three lights on a toy, only one light will be active at one time.

Each light sensor will provide the servo-motor with a different funciton, the left sensor will make the servo turn clockwise, the right sensor will make the servo turn anti-clockwise, and the centre servo will make the servo stop moving. It’s fairly simple.

Like i said though, I’m really new to arduino, and i’m a bit lost when it comes to the coding aspect, so I could really use some help and guidance there.

I’ve also posted a diagram of how I’ve wired it, I think it’s right, but i’m quite unsure.

Anyway, thanks for reading, hope you can help :slight_smile:

Hi, A separate power source for your servo/s is one thing to consider, see the links in my signature for some background. Other than that have a read of the oomlout beginners guide for some servo and light sensor examples which you should be able to combine to achieve your goal.

Duane B

Thanks man, that guide’s really helpful. I’m gonna start working on the code, and i’ll get back to you.

I’m not really sure what you mean, regarding the separate power source for my servo though, and how it will affect my project?

Read this

then watch this

These examples for coding don't help me, sorry. I can't use the net. framework/netduino for my coding. Only the standard arduino for my college project.

The examples are all standard Aduino hardware and software.

Duane B

Duane, you posted the link to the Oomlout Netduino document. You need for the Arduino version.

Hi, Sorry about that and thanks for the correction.

Duane B

I've sorted my servo out, wrote the code for it. I can't find any code for the light sensors though.

I need them to be active only at a certain light level, if that makes any sense... let's say like while they're exposed to a certain level of light, the servo function activates.

So light sensor 1 gains light input then turns the servo clockwise until light sensor 2 gains light input. Light sensor 3 functions the same, turning the servo anti-clockwise until light sensor 2 gains light exposure.

I could also do it so that when light sensor 1 & 3 lose light exposure the process stops.

How would I go about actually coding this feature?

Well to start with, just try and use one sensor. Use analogRead to get a reading from it. Use an if statement to check if it's greater than a threshold you determine by experiment & if so, use the servo code you already have to move the servo. Use serial.println to show the readings you're getting on the serial monitor.

Ok I’ve got a reading, the light sensor needs to be above the value of ‘150’ for it to be classed as active and power the servo.

How/where would I implement the other functions? I’m totally clueless when it comes to coding this…

Here’s the code i’ve got so far:

#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
void loop() {
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position

Need to finish this urgently for tomorrow, so any help would be greatly appreciated! Thanks.