self-oriented photoresistor

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);
}

please help me

i just noticed that i put this post on the wrong section

What is not working as you expect?

I don't think you need this double test.

if(foto<foto){ //if the ligh is increasing
   if(Degrees<Degrees){//and the digrees too

The only external influence is the value from the photo-resistor

And, as @zoomkat said, tell us what the problem is.

...R

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.

Reading that might have some info.

https://www.google.com/search?ie=UTF-8&oe=UTF-8&q=solar+track&btnG=search&domains=http%3A%2F%2Fforum.arduino.cc&sitesearch=http%3A%2F%2Fforum.arduino.cc&gws_rd=ssl

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
}

ok, i managed do make a led turn on when a value is increasing and tun off when decreasing

now i need to make a servo go in one direction until the light decreases

i tried this code, but it doesn't work

#include <Servo.h>
Servo servo;
const int servopin=A0;
const int photoPin=A1;
int photo1;
int photo2;
int degree;
int i;
int a;
int del=30;


void setup() {
  pinMode(photoPin, INPUT);
  servo.attach(servopin);
  servo.write(0);
  Serial.begin(9600);
}

void loop() {
  photo1=photo2;
  photo2=analogRead(photoPin);
  degree=i;
  Serial.println(photo2);
  if(degree=1){
   for(int i=degree; i=1+degree; i+=1){
    servo.write(i);
    delay(del);
   }
  }
  if(degree=179){
   for(int i=degree; i=degree-1; i-=1){
    servo.write(i);
    delay(del);
   }
  }
  if(photo1>photo2){
    for(int i=degree; i=1+degree; i+=1){
      for(int a=i;a=i-2; a-=1){
        servo.write(a);
        delay(del);
        }
    }
 }
 if(photo1>photo2){
    for(int i=degree; i=degree-1; i-=1){
      for(int a=i; a=i+2; a+=1);{
        servo.write(a);
        delay(del);
      }
    }
  }
}

My project is to make a photo-resistor oritented servo
this is my code and it doesn't work, any ideas?

#include <Servo.h>
Servo servo;
const int servoPin=A0;
const int photoPin=A1;
int photo1;
int photo2;
int i;
int a;
int Degrees;
int Delay=30;


void setup() {
  pinMode(photoPin, INPUT);
  servo.attach(servoPin);
  servo.write(0);
  Serial.begin(9600);
}

void loop() {
  photo1=photo2;
  photo2=analogRead(photoPin);
  Serial.println(photo2);
  Degrees=i;
  if(Degrees=1){
   for(int i=Degrees; i>Degrees+1; i+=1){
    while(photo1>photo2){
      for(int a=i;a<i-2; a-=1){
        servo.write(a);
        delay(Delay);
     }
    }
    while(photo1<=photo2){
        servo.write(i);
        delay(Delay);
    }
   }
  }
  if(Degrees=179){
   for(int i=Degrees; i<Degrees-1; i-=1){
    while(photo1>photo2){
     for(int a=i; a>=i+2; a+=1){
        servo.write(a);
        delay(Delay);
     }
    }
    while(photo1<=photo2){
        servo.write(i);
        delay(Delay);
    }
   }
  }
  for(int i=0; i>Degrees+1; i+=1){
      servo.write(i);
      delay(Delay);
  } 
 }

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.

This is also an error:

while(photo1>photo2){
      for(int a=i;a<i-2; a-=1){
        servo.write(a);
        delay(Delay);
     }

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.

so, to fix the first problem i have to start in a degree greater than 2

but i don't know how to fix the second

Why are you double posting?

You have a perfectly good Thread here.

...R

Robin2:
Why are you double posting?

You have a perfectly good Thread here.

...R

Becouse i was the wrong section

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.

ok, undestand

how can i have a sort of analogRead of the servo (i remember that it doesn't work like that whith servos)

for the second problem i'm trying to solve this right now

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.

ok, i made a code trying to solve this problem

const int led1=13;
const int potPin=A0;
int pot1=0;
int pot2=0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  pot1=pot2;
  pot2=analogRead(potPin);
  while(pot1<pot2){
    digitalWrite(led1, HIGH);
  }while(pot1>pot2){
    digitalWrite(led1, LOW);
  }
 Serial.print(pot1);
 Serial.print("   ");
 Serial.println(pot2);
 delay(150);
}

and i solved this using if instead of while

const int led1=13;
const int potPin=A0;
int pot1=0;
int pot2=0;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  pot1=pot2;
  pot2=analogRead(potPin);
  if(pot1<pot2){
    digitalWrite(led1, HIGH);
  }else{
    digitalWrite(led1, LOW);
  }
 Serial.print(pot1);
 Serial.print("   ");
 Serial.println(pot2);
 delay(150);
}

but it doesn't seem to work with my code

#include <Servo.h>
Servo servo;
const int servoPin=A1;
const int photoPin=A0;
int photo1;
int photo2;
int i;
int a;
int Degrees=90;
int Delay=30;


void setup() {
  pinMode(photoPin, INPUT);
  servo.attach(servoPin);
  servo.write(0);
  Serial.begin(9600);
  servo.write(Degrees);
}

void loop() {
  photo1=photo2;
  photo2=analogRead(photoPin);
  Serial.println(photo2);
  Degrees=i;
  if(Degrees=1){
   for(int i=Degrees; i>Degrees+1; i+=1){
    if(photo1>photo2){
      for(int a=i;a<i-2; a-=1){
        servo.write(a);
        delay(Delay);
     }
    }
    else{
        servo.write(i);
        delay(Delay);
    }
   }
  }
  if(Degrees=179){
   for(int i=Degrees; i<Degrees-1; i-=1){
    if(photo1>photo2){
     for(int a=i; a>=i+2; a+=1){
        servo.write(a);
        delay(Delay);
     }
    }
    else{
        servo.write(i);
        delay(Delay);
    }
   }
  }
  for(int i=0; i>Degrees+1; i+=1){
      servo.write(i);
      delay(Delay);
  } 
 }

plus i've think to have solved the problem with the degrees by putting in the setup

servo.write(Degrees);

Degrees=i;

Since you never give i any other value anywhere in your code, this will always go to zero.

The i in your for loops isn't the same i as the i you have at global scope. If you want it to be, then lose the "int" in front of it.

Please define what you mean by "it doesn't work". What is it supposed to do? What does it actually do?