Trouble with a contineous rotating servo

I am making a project where i have a contineous rotating servo, i need it to spin some laps , for that i used a photoresistor , when the photoresistor recieves a determinated amount of light the servo should move some laps and stay there until the photoresistor recieves another specific value.

This is my code:

#include <Servo.h>

int sensorluz = A0;
int LEDrojo = 2;
int LEDamarillo = 4;
Servo myservo;

int luz = 0;
int valordelsensor = 0;
const int speed0 = 0;
int i;

void setup() {

Serial.begin(9600);
pinMode (sensorluz, INPUT);
pinMode (LEDrojo, OUTPUT);
pinMode (LEDamarillo, OUTPUT);
myservo.attach(3);

{ valordelsensor = analogRead(sensorluz);
luz = (5.0 * valordelsensor * 100.0)/ 1024.0;

Serial.print (luz);
Serial.print ("luz");
delay (100);}

if (luz <= 120)
{
digitalWrite (LEDrojo, HIGH);
digitalWrite (LEDamarillo, LOW);
}
for(i = 0; i < 90; i++)
{
myservo.write(speed0 + i);
delay(15);
}

{ myservo.write(speed0);
delay(1000);}

if (luz >= 200)
{
digitalWrite (LEDrojo, LOW);
digitalWrite (LEDamarillo, HIGH);
}

for(int i = 0; i < 90; i++)
{
myservo.write(speed0 - i);
delay(1000);
}
{
myservo.write(speed0);
delay(100);
}

}

void loop() {

{ valordelsensor = analogRead(sensorluz);
luz = (5.0 * valordelsensor * 100.0)/ 1024.0;

Serial.print (luz);
Serial.print ("luz");
delay (100);}

if (luz <= 120)
{
digitalWrite (LEDrojo, HIGH);
digitalWrite (LEDamarillo, LOW);
}
for(i = 0; i < 90; i++)
{
myservo.write(speed0 + i);
delay(15);
}

{ myservo.write(speed0);
delay(1000);}

if (luz >= 200)
{
digitalWrite (LEDrojo, LOW);
digitalWrite (LEDamarillo, HIGH);
}

for(int i = 0; i < 90; i++)
{
myservo.write(speed0 - i);
delay(1000);
}
{
myservo.write(speed0);
delay(100);
}

}

The problem is that the servo dont stop spinning, I´ve try this with a normal servo & it works my code for that is:

#include <Servo.h>

int sensorluz = A0;
int LEDrojo = 2;
int LEDamarillo = 4;
Servo myservo;

int luz = 0;
int valordelsensor = 0;

void setup() {

myservo.attach(3);

Serial.begin(9600);
pinMode (sensorluz, INPUT);
pinMode (LEDrojo, OUTPUT);
pinMode (LEDamarillo, OUTPUT);

}

void loop() {

myservo.write (0);
valordelsensor = analogRead(sensorluz);
luz = (5.0 * valordelsensor * 100.0)/ 1024.0;

Serial.print (luz);
Serial.print ("luz");
delay (100);

if (luz <= 120)
{
digitalWrite (LEDrojo, HIGH);
digitalWrite (LEDamarillo, LOW);
myservo.write(180);

}
if (luz >= 200)
{
digitalWrite (LEDrojo, LOW);
digitalWrite (LEDamarillo, HIGH);
myservo.write(0);

}
}

I can´t find out the problem :confused:

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Then come back to your posts and edit it (lower right of your post screen) add the code tags and make us happy.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile: