Hi all..
new to the forum and coding....ish.
I have built a solar tracker and is tracking well...
I want to add in couple of parameters....eg, if it is cloudy (sensor reading low light value) Tracker makes intermittent steps to track where the sun would be....
Also, if dark, (sensor reading very low light value) tracker returns to sunrise position. (ie, rotate CCW 180, or, step CCW overnight, and hold until sensors detect high value).
Here is my current working code.....
dont know why but my readings are back to front, so high light levels = low percentage reading( bright = 0, dark = 100.
const int rightSensorPin = A0;
const int leftSensorPin = A1;
int rightSensorRead = 0;
int leftSensorRead = 0;
int rightLightPct = 0;
int leftLightPct = 0;
int degreeChange = 2;
//Arduino's CustomStepper library
//Author: Igor Campos
#include <CustomStepper.h>
CustomStepper stepper(8, 9, 10, 11);
void setup() {
Serial.begin(9600);
stepper.setRPM(1);
stepper.setSPR(2100);
}
void loop() {
rightSensorRead = analogRead(rightSensorPin);
leftSensorRead = analogRead(leftSensorPin);
rightLightPct = map(rightSensorRead, 0, 1023, 0, 100);
leftLightPct = map(leftSensorRead, 0, 1023, 0, 100);
degreeChange = map(abs(rightLightPct-leftLightPct),0,100,2,10);
Serial.print("right = ");
Serial.print(rightSensorRead);
Serial.print(" left = ");
Serial.print(leftSensorRead);
Serial.print(" Pct r/l = ");
Serial.print(rightLightPct);
Serial.print("/");
Serial.println(leftLightPct);
if(rightLightPct == leftLightPct ){
Serial.println("Aligned");
}else if(rightLightPct > leftLightPct +1){
Serial.println("Turning CW!");
rotateRight();
stepper.run();
}else if(leftLightPct > rightLightPct +1){
Serial.println("Turning CCW!");
rotateLeft();
stepper.run();
}
delay(750);
}
void rotateLeft(){
stepper.setDirection(CW);
stepper.rotateDegrees(degreeChange);
while(stepper.isDone() == false){
stepper.run();
}
}
void rotateRight(){
stepper.setDirection(CCW);
stepper.rotateDegrees(degreeChange);
while(stepper.isDone() == false){
stepper.run();
}
}
I have been tinkering with the code but no........
if(rightLightPct <80 &= leftLightPct){ // if all aligned no movement
Serial.println("Aligned");
} else if(leftLightPct <10 &> rightLightPct +1){ // Bright Sun = sensor reading below 10%
Serial.println("Tracking Sun");
rotateRight();
stepper.run();
} else if(leftLightPct >10 &> rightLightPct +1){ // cloudy = Tracking
Serial.println("No Sun -- Step Tracking");
rotateRight();
stepper.run();
} else if(rightLightPct >80 == leftLightPct +1){ // Night detection = sensor reading above 80%
Serial.println("Repositoning for Sunrise");
rotateLeft();
stepper.run();
Any guidence woul be greatly appreciated....
Sarah..

