Using a light sensor to control servo

Hi all!
I am really new to using an arduino as you can probably tell, so would appreciate any help you could offer. Seems like you guys are really helpful!

I am trying to design a mechanical arm that will move when a certain number of coins pass a light sensor. The arm is powered by a servo motor. The idea is that the servo will move through 20 degrees, once a minute, and move through a full 360 degrees when 15 coins have passed the light sensor.
I am using an arduino micro and have connected up my circuit so that the light sensor is connected to pin 2 and 3. And the motor is connected to pin 5.

I have written the following code but all that seems to happen is the servo starts rotating continuously when I pass a coin by the light sensor. Nothing happens otherwise.
I’m not too sure what I am doing wrong so any pointers would be really appreciated, thanks.

#include <Servo.h> //Include the Servo Library
Servo myServo; //Create the Servo object

int lightPin =2; //Define Pins
int lightPin2 =3;
int servoPin =5;
int count = 0;

//boolean currentlight; //Light sensor variables
boolean prevLight = LOW;
// boolean servoOn = false;

void setup ()
{
myServo.attach(servoPin); //“Attach” The Servo Obkect
pinMode(lightPin, INPUT);
pinMode(lightPin2, INPUT);
}

void loop ()
{
if (digitalRead(lightPin) == HIGH && prevLight == LOW || digitalRead(lightPin2) == HIGH && prevLight == LOW )
// measure change in light sensing condition
{
int count = 1; count < 15; count++;
// count number of times that coin passes light sensor up to 15
}
if (count = 15)
{
myServo.write(360);
//servo moves full rotation if coin passes 15 times
}
else
{ for (int i=0; i<=360; i=i+20)

myServo.write(i);
//move servo through 20 degrees
delay(60000);
// hold in each position for 1 minute
}
}

lc500:
I am really new to using an arduino as you can probably tell

Certainly can! The first giveaway clue is you did not read the "read me first" post at the top of the forum before you posted. You should have put your code inside code tags (the # button). Please edit your original post and fix that. If you do that we will help because we will then know you are still around waiting for an answer and we are not wasting our time (which happens a lot).

Paul

  int count = 1; count < 15; count++;

Novel construct.

AWOL:

  int count = 1; count < 15; count++;

Novel construct.

Indeed! Worse still, its making a hole in the scope of the global variable of the same name, so even the “count++” is having no effect…

I am trying to design a mechanical arm that will move when a certain number of coins pass a light sensor. The arm is powered by a servo motor. The idea is that the servo will move through 20 degrees, once a minute, and move through a full 360 degrees when 15 coins have passed the light sensor.

If you have a servo that can rotate through 360 degrees then they normally don't allow you to move a fixed amount (like 20 degrees) but only allow you to specify direction and speed. You may need a separate encoder to determine angle.

Hi - thanks for your replies! :slight_smile:

Firstly sorry for the poor formatting - here is the code again:

#include <Servo.h>                  //Include the Servo Library
Servo myServo;                  //Create the Servo object
                 
int lightPin =2;                   //Define Pins
int lightPin2 =3;
int servoPin =5;  
int count = 0;

//boolean currentlight;              //Light sensor variables
boolean prevLight = LOW;
// boolean servoOn = false; 

void setup ()
{  
  myServo.attach(servoPin);     //"Attach" The Servo Obkect
   pinMode(lightPin, INPUT);
   pinMode(lightPin2, INPUT);
}

void loop ()
{
 if (digitalRead(lightPin) == HIGH && prevLight == LOW || digitalRead(lightPin2) == HIGH && prevLight == LOW ) 
 // measure change in light sensing condition 
 {
   int count = 1; count < 15; count++;
 // count number of times that coin passes light sensor up to 15
 }
 if (count = 15)
 {
    myServo.write(360);
 //servo moves full rotation if coin passes 15 times
 }
 else 
 {   for (int i=0; i<=360; i=i+20) 
 
    myServo.write(i);
//move servo through 20 degrees
    delay(60000);
// hold in each position for 1 minute
 }
 }
  1. I am not quite sure what exactly is wrong the counting function?

PaulRB:

AWOL:

  int count = 1; count < 15; count++;

Novel construct.

Indeed! Worse still, its making a hole in the scope of the global variable of the same name, so even the “count++” is having no effect…

Please could you explain this a little more?
3. I have checked the servo running a simple angle increment program and it seems you are right Riva.

Riva:
If you have a servo that can rotate through 360 degrees then they normally don’t allow you to move a fixed amount (like 20 degrees) but only allow you to specify direction and speed. You may need a separate encoder to determine angle.

The servo’s speed and direction is the variable. But I should be able to modify this code using delays and speed right to set it to a specific angle? Super new to coding!

Thanks so much, really appreciate your help!

lc500:
here is the code again

Not what I asked you to do. It was a simple enough request.

I didn't ask because I love the feeling of power or superiority over newbie programmers. I don't.

Computers of all sizes execute instructions to the letter no matter how stupid those instructions are. To be a programmer you have to play the part of the computer, running the program with part of your mind, to the letter. Meanwhile another part of your mind monitors and criticises what's going on by comparing it to the original intention. Actually the first of those two things is the more difficult for us humans.

Think I worked out that part now, thanks for giving me the heads up!