Ciao a tutti,
il mio progetto consiste nel muovere un servo motore con tre foto resistenze.
Oscurandone una, tramite ciclo Sweep, lo muove a sinistra e l'altra lo muove a destra.
Il problema mi arriva con la terza che con la funzione BREAK dovrebbe fermarlo nella posizione corrente.
Finora sono soltanto riuscito a riportarlo a zero.
#include <Servo.h>
Servo myServo;
int x=0;
int sensorPin=A0;
int sens;
const int threshold= 500;
int prevX = 0;
int right;
int left;
void setup() {
myServo.attach(6);
}
void loop() {
right = analogRead(A1);
left = analogRead(A2);
if(right > threshold){
if(left < threshold){
for (x = 0; x < 180; x ++)
{
myServo.write(x);
sens = analogRead(sensorPin);
if (sens > threshold){
myServo.write(0); // Here I've got the problem!
break;
}
delay(30);
}
}
}
if(left > threshold){
if(right < threshold){
for (x < 180; x > 0; x --)
{
myServo.write(x);
sens = analogRead(sensorPin);
if (sens > threshold){
myServo.write(0); // Here I've got the problem!
break;
}
delay(30);
}
}
}
}