/* Forensketch Sun Tracker
https://forum.arduino.cc/t/solartracker-ldr-werte-vergleichen/1419255
2025-12-12 by noiasca
Liebe Lehrer:innen, lasst euch mal was neues einfallen und gebt den Schüler:innen endlich neue Aufgaben...
*/
#include <Streaming.h> // by by Peter Polidoro https://github.com/janelia-arduino/Streaming
#include <Servo.h> // by Michael Margolis, Arduino
class Axis {
const uint8_t ldrPin[2]; // two GPIOs to measure sun intensity
const uint8_t servoPin; // a GPIO for the servo
const uint16_t minPosition = 10; // lowest possible position for the servo (some have problems to start at 0) - you might make it static also
const uint16_t maxPosition = 170; // highest possible position for the servo (some have problems to go till 180) - you might make it static also
Servo servo; // a servo object for this axis
uint16_t position = 90; // current position of servo
uint32_t previousMillis = 0; // time management according IDE example "blink without dealy"
uint16_t interval = 500; // time management according IDE example "blink without dealy"
public:
Axis (const uint8_t pinA, const uint8_t pinB, const uint8_t pinS) : ldrPin{pinA, pinB}, servoPin{pinS} {}
// initialize hardare - to be called in setup()
void begin() {
servo.attach(servoPin);
servo.write(position);
}
// compare and handle - the "run" method to be called in loop()
void update(uint32_t currentMillis = millis()) {
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if ((analogRead(ldrPin[0]) > analogRead(ldrPin[1])) && position < maxPosition) position++; // increase servo
else if ((analogRead(ldrPin[0]) < analogRead(ldrPin[1])) && position > minPosition) position--; // decrease servo
Serial << (F("Servo on: ")) << servoPin << (F(": ")) << position << endl; // debug only
servo.write(position);
}
}
};
// we need two axis azimut and elevation:
Axis axis[] {
{A0, A1, 2}, // axis[0]: LDR pin A, LDR pin B, servo pin
{A2, A3, 3}, // axis[1]: LDR pin A, LDR pin B, servo pin
};
void setup() {
Serial.begin(115200);
Serial << (F("\r\nStart...\r\n")) << endl;
for (auto &a : axis) a.begin(); // start both axis
}
void loop() {
for (auto &a : axis) a.update(); // handle both axis
}
//