Trying to pull information from serial monitor

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
}

You cannot "pull" data from the Serial Monitor - it is not a data store.

Just save the values in an array and then you can check all the values to see which is highest.

I don't see why it is necessary to scan 180 deg. If the device starts with (say) the sun on the right then it just needs to step until it finds the first value that is lower than the previous value. Then it should stay where it is until the sun has caught up and passed it out. (Assuming you are in the Northern hemisphere).

However a better way is to use two sensors arranged so that they only show the same value when they are facing the sun. Then the code just moves the device towards the brighter sensor until the values are even. This system can follow the sun in either direction.

...R

Thank you for your kind reply! I will dig into those ideas. This is only my 2nd day using Arduino. I have no previous programming experience or knowledge so this is all new and exciting for me!!!

Yes ideally it can be very much more efficient but Im just working with what the starter kit came with, haha.