#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
// ---------------------- OLED Setup ----------------------
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ---------------------- Pins ----------------------
#define POT_PIN A0 // Slider / potentiometer
#define SOLAR_PIN A1 // Simulated solar sensor
#define SERVO_PIN 9 // Servo PWM signal
// ---------------------- Servo & Constants ----------------------
Servo panelServo;
const float CURRENT_SCALING = 0.05; // Current per % sunlight
const float VOLTAGE = 5.0; // Example voltage of solar panel
// ---------------------- Functions ----------------------
int readPotentiometer() {
return analogRead(POT_PIN); // 0-1023
}
int readSolarSensor(int potValue) {
// Simulate solar sensor with small variation
float variation = random(-50, 50) / 100.0; // -0.5% to +0.5%
int solar = potValue / 1023.0 * 100 + variation;
if (solar < 0) solar = 0;
if (solar > 100) solar = 100;
return solar;
}
int calculatePanelAngleIST(int hour, int minute) {
// Map 6:00 AM -> 0°, 6:00 PM -> 180°
int totalMinutes = hour * 60 + minute;
int angle = map(totalMinutes, 6*60, 18*60, 0, 180);
if (angle < 0) angle = 0;
if (angle > 180) angle = 180;
return angle;
}
// ---------------------- Setup ----------------------
void setup() {
Serial.begin(9600);
panelServo.attach(SERVO_PIN);
Wire.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
}
// ---------------------- Main Loop ----------------------
void loop() {
// ---------- Simulated Time ----------
int hour = 9; // Example: 9 AM
int minute = 30; // Example: 9:30 AM
// ---------- Panel Angle ----------
int panelAngle = calculatePanelAngleIST(hour, minute);
panelServo.write(panelAngle);
// ---------- Read Sunlight ----------
int potValue = readPotentiometer(); // Slider % sunlight
int solarValue = readSolarSensor(potValue); // Simulated solar sensor %
// ---------- Calculate Current & Power ----------
float current = solarValue * CURRENT_SCALING; // A
float power = VOLTAGE * current; // W
// ---------- Display on OLED ----------
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Smart Solar Tracker");
display.print("Pot: "); display.println(potValue);
display.print("Solar: "); display.println(solarValue);
display.print("Angle: "); display.println(panelAngle);
display.print("Current: "); display.println(current, 2);
display.print("Power: "); display.println(power, 2);
display.display();
delay(500); // Update every 0.5 sec
}