Pages: [1]   Go Down
Author Topic: Photosensor controlled servo programming help?  (Read 1103 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm an artist who is not too great at programming, I would really appreciate your advice.
 I'm just trying to make a continuous rotation servo go when the light shines bright on a photocell, and turn off when the light is dim.  I tried this program below which I googled:

What it does with a continuous rotation servo is make it go forward when the light is dim, and go backwards when the light is bright.  I've been following lady adas tutorials on photosensors and have an idea of the range I'm wanting to target: it should be 700 to 1023 when the servo rotates.  Anything dimmer than that it should just be off.


Code:
#include <Servo.h>      // include the servo library

 Servo servoMotor;       // creates an instance of the servo object to control a servo

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

 int servoPin = 9;       // Control pin for servo motor. As of Arduino 0017, can be any pin

 void setup() {
   servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
 }

 void loop()
 {
   analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
   analogValue = map(analogValue, 0, 1023, 0, 179);     // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
   servoMotor.write(analogValue);                       // write the new mapped analog value to set the position of the servo
   delay(15);                                           // waits for the servo to get there
 }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm just trying to make a continuous rotation servo go when the light shines bright on a photocell, and turn off when the light is dim.
Go which way? How fast? Is the speed to be proportional to the brightness?

There is far too little detail in your post to help you.
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this in the loop() and let me know what happens.

Code:
   analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
   if(analogValue > 700) {
        servoMotor.write(0);                   // Spin servo at max speed clockwise
   } else {
        servoMotor.write(90);                 //  Stop the servo
   }
   delay(15);                                           // waits for the servo to get there
 
Logged

Experience, it's what you get when you were expecting something else.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow!  Thanks so much for the replies!  In answer to the question, I just need the servos to go one direction, it doesn't matter which one.  The speed it's been going is fine for me, it's not so important, just something in the medium range. I want each servo to start moving when you shine a light on it. The servos are are attached to a flower mechanism that opens and closes:



There are 15 of these in an icosahedron, it's supposed to be a virus.

 I love your idea about making it go faster when the light is brighter, but right now I just need to make it go at all by next weekend!  I can think about making it technologically cooler later.

I'm about to try the little program one of you posted, thanks for that!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so I tried this in the loop:
Code:
   analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
   if(analogValue > 700) {
        servoMotor.write(0);                   // Spin servo at max speed clockwise
   } else {
        servoMotor.write(90);                 //  Stop the servo
   }
   delay(15);                                           // waits for the servo to get there
 

and it rotates the servo slowly in a clockwise direction, and then speeds up the rotaion when I shine the light on the servo.  That's awesome, now I just need it to hold still without a light?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, I tried covering the photosensor with my finger, and it still keeps turning slowly.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9542
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the wiring for the photocell? It may need to be wired as part of a voltage divider circuit.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4238
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Has the servo got any way of adjusting its centre (motionless) position ?
If not, then try adjusting the value (currently 90) that is sent to the servo when the light is off.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Servos vary, so as UKHeliBob notes, you may find that 89 or 91 (or whatever) is the magic value that stops yours. If there isn't one, using servo.writemicroseconds will give finer control to find the elusive motion free setting.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for all the help!  I got the servos to stop at 93.  My next question is:  I was thinking about running 5 heavy duty servos with a bit of a load on an Arduino without any motor controller. The servos have a separate power supply, and only one should be fully working at maximum speed at any one time. Do you think this will blow up my Arduino? There is a bit of code that can protect your Arduino from current spikes, does anyone know what that is and if it would be all I need?  I have a Renbotics servo shield that I could try to use, but I didn't just because I've never used it. Maybe I should though?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4238
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should be OK running the servos directly from the Arduino as long as they have a separate power supply and the Arduino is only connected to them with a common ground and the signal wires. 

You can use the same 6 volt power supply for the servos and the Arduino as long as the 6V supply for the servos goes directly to them.  What is not advisable is to take the 5V and GND for the servos from the Arduino power pins as this relies on the power regulator of the Arduino which cannot supply enough current.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: