Sorry if this is confusing :o . I am building a primitive sun tracking device with only the parts supplied in the starter kit. I already have a servo sweeping 0-180* and back with a photoresistor attached. The servo stops at each degree increment for 250ms and takes a photo resistor reading and stores it into the serial monitor, then continues on until it reaches 180* and returns to 0* and starts over again. I have the servo stop for 30 seconds if its reading from the photoresistor reaches a threshold value.
However, that is not very efficient. I would like the servo to sweep 180* while taking a photoresistor reading at each degree increment, store that into the serial monitor, and after doing its sweep, return to a degree angle that the best value from the photoresistor was read at and delay there for 30 seconds.
Is there a way to pull info from the serial monitor and send that back to the servo, and clear the serial monitor of data? Is there a way to log the degree angle that the servo was at associated with the best value from the photoresistor?
I believe I just dont know the actual commands for this. Using an Arduino Uno. Thanks!
//note - photoresistor writes bright light at a lower number
//note - photoresistor writes low light at a higher number
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
const int lightPin = A0; // define a pin for Photo resistor
const int threshold = 60; // define threshold value for exposure limit
void setup(){
{Serial.begin(9600);} //Begin serial communcation
{myservo.attach(9);} // attaches the servo on pin 9 to the servo object
}
void loop(){
{for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(analogRead(lightPin)); // write photoresistor data in between servo movement
int analogValue = analogRead(lightPin); // read the value from the photoresistor
if (analogValue < threshold){delay(10000);} // if value from photoresistor is less than threshold, delay servo movement
else { delay(0250); }} // if value from photoresistor is more than threshold, wait 0250ms and repeat servo movement
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(analogRead(lightPin)); // write photoresistor data in between servo movement
int analogValue = analogRead(lightPin); //read the value from the photoresistor
if (analogValue < threshold){delay(10000);} //if value from photoresistor is less than threshold, delay servo movement
else {delay(0250);}}} // if value from photoresistor is more than threshold, wait for 0250ms and repeat servo movement
}