hi, i've been working on a photoresistor mounted on a servo wich auto-orients towards the strongest source of light
the code is this
#include <Servo.h>
const int servoPin = A0;
int Degrees;
const int photoresistor=A4;
int foto; //the ammount of ligh the photoresistor recieves
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(servoPin);
pinMode(photoresistor, INPUT);
servo.write(90); //the starting potition of the servo
delay(1000);
}
void loop() {
servo.write(Degrees);
foto=analogRead(photoresistor);
Serial.print(foto);
Serial.print(" ");
Serial.println(Degrees);
if(foto<foto){ //if the ligh is increasing
if(Degrees<Degrees){//and the digrees too
servo.write(Degrees=Degrees+1);//continue to move in that direction
}else{
servo.write(Degrees=Degrees-1);//continue to move in that direction
}
}
if(foto>foto){//if the ligh is decreasing
if(Degrees<Degrees){//and the degrees are increasing
servo.write(Degrees=Degrees-1);//than turn the other way
}else{
servo.write(Degrees=Degrees+1);//than turn the other way
}
}
delay(250);
}
How can foto and Degrees be less than itself?
if(foto<foto){
if(Degrees<Degrees){//
or greater?
if(foto>foto){//
Seems like you'd need an oldFoto and oldDegrees to compare the latest foto and Degrees against, then make oldFoto = foto for the next comparison.
CrossRoads:
How can foto and Degrees be less than itself?
if(foto<foto){
if(Degrees<Degrees){//
or greater?
if(foto>foto){//
Seems like you'd need an oldFoto and oldDegrees to compare the latest foto and Degrees against, then make oldFoto = foto for the next comparison.
undestand, but how can i say: "if a value is increasing, than do that"?
The value is increasing, but you are not looking at a physical value throughout time, you have
captured the instantaneous state in a variable.
You need to capture the state repeatedly and compare the latest observation with a previous
observation - you need at least two variables, perhaps foto and prev_foto, etc...
Simple code to test increasing/decreasing sensor. May need a little tweaking to compile.
int foto;
int oldFoto;
byte sensor = A4;
void setup(){
Serial.begin(9600);
}
void loop(){
foto = analogRead(sensor);
if (foto > oldFoto){
Serial.print("foto increased by ");
Serial.println (foto - oldFoto);
}
if (foto == oldFoto){
Serial.print ("foto stayed at ");
Serial.println (foto);
}
if (foto <oldFoto){
Serial.print("foto decreased by ");
Serial.println (oldFoto - foto);
}
oldFoto = foto; // save for next pass comparison
delay (250); // lazy way to get ~ 4 readings a second
}
I think you may have a misunderstanding of how for loops work. Try writing a simple sketch that just prints out some numbers and make sure they work the way you want. For example this for loop will never execute at all:
for(int i=Degrees; i>Degrees+1; i+=1){
If i starts out at Degrees, and must be greater than Degrees +1 for the loop to run, then it fails on the first iteration as Degrees is obviously less than Degrees +1 so the stuff in the for loop never runs.
Since photo1 and photo2 never change in that while loop it will be infinite. If photo1 is greater than photo2 to get into the loop there and they don't change, then it will always be greater and this while loop will just keep repeating forever and ever.
Again in this section note the mistake in the for loop. If a is equal to i at the beginning and must be less than i -2 to run the loop, then it will never run as it is impossible for i to be less than i - 2 so even the first iteration here fails the condition.
Francesco_Sacco:
so, to fix the first problem i have to start in a degree greater than 2
If you start with a degree greater than 2 it still won't be greater than degree +1.
Imagine Degree is 5. So the for loop says to set create a new variable i and set it equal to Degree. So i is 5. The middle part of a for loop is the test. It says if i is greater than Degree + 1 then run the stuff in that block of code. So if i is 5 and Degree + 1 is 6 so 5 is not greater than 6 so the test fails and it doesn't run the block of code.
I think what you need to do is just what I said, put this code aside for a while and study up on how for loops work. Write some code that just prints lists of numbers to the screen or something with a for loop and work with it until you can make it work.
For the part about the while loop you need to have code inside the while loop that updates either photo1 or photo2 so that it may one day be possible for that condition to not be true anymore.
Francesco_Sacco:
how can i have a sort of analogRead of the servo (i remember that it doesn't work like that whith servos)
If you wrote the code that sent it to a certain position, then you should always know in your code what position it is at. You shouldn't need to ask the servo where you told it to go last.