#include <Arduino.h>
#ifndef CONFIG_H
#define CONFIG_H
#define THRESHOLD 20
#define BAUD_RATE 9600
#endif
// ======================================================
// Efficient Solar Lighting System
// ATmega328P (Arduino UNO)
// ======================================================
// ---------------- Pin Definitions ----------------
// Limit Switch Pins (Using INPUT_PULLUP)
#define eastSw 5
#define westSw 6
#define northSw 7
#define southSw 8
// Motor 1 (East-West)
#define m1a 9 // Clockwise
#define m1b 10 // Counterclockwise
// Motor 2 (North-South)
#define m2a 11 // Clockwise
#define m2b 12 // Counterclockwise
// LED Indicator
#define led 13
// ---------------- Global Variables ----------------
int eastRes = 0;
int westRes = 0;
int northRes = 0;
int southRes = 0;
bool swEast = HIGH;
bool swWest = HIGH;
bool swNorth = HIGH;
bool swSouth = HIGH;
// ---------------- Function Prototypes ----------------
void getLdrStatus();
void getSwitchStatus();
void stopMotors();
// ======================================================
// Setup
// ======================================================
void setup() {
Serial.begin(BAUD_RATE);
delay(200);
pinMode(led, OUTPUT);
pinMode(eastSw, INPUT_PULLUP);
pinMode(westSw, INPUT_PULLUP);
pinMode(northSw, INPUT_PULLUP);
pinMode(southSw, INPUT_PULLUP);
pinMode(m1a, OUTPUT);
pinMode(m1b, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2b, OUTPUT);
stopMotors();
Serial.println("Solar Lighting System Started");
}
// ======================================================
// Main Loop
// ======================================================
void loop() {
getLdrStatus();
getSwitchStatus();
// -------- East to West Movement --------
if (eastRes > westRes + THRESHOLD) {
if (swWest == HIGH) {
digitalWrite(m1a, HIGH);
delay(50);
digitalWrite(m1a, LOW);
Serial.println("Moving East to West");
}
}
// -------- West to East Movement --------
if (westRes > eastRes + THRESHOLD) {
if (swEast == HIGH) {
digitalWrite(m1b, HIGH);
delay(50);
digitalWrite(m1b, LOW);
Serial.println("Moving West to East");
}
}
// -------- North to South Movement --------
if (northRes > southRes + THRESHOLD) {
if (swSouth == HIGH) {
digitalWrite(m2a, HIGH);
delay(50);
digitalWrite(m2a, LOW);
Serial.println("Moving North to South");
}
}
// -------- South to North Movement --------
if (southRes > northRes + THRESHOLD) {
if (swNorth == HIGH) {
digitalWrite(m2b, HIGH);
delay(50);
digitalWrite(m2b, LOW);
Serial.println("Moving South to North");
}
}
// LED Heartbeat Indicator
digitalWrite(led, !digitalRead(led));
delay(200);
}
// ======================================================
// Read LDR Values
// ======================================================
void getLdrStatus() {
eastRes = analogRead(A3);
westRes = analogRead(A2);
northRes = analogRead(A1);
southRes = analogRead(A0);
Serial.print("E:");
Serial.print(eastRes);
Serial.print(" W:");
Serial.print(westRes);
Serial.print(" N:");
Serial.print(northRes);
Serial.print(" S:");
Serial.println(southRes);
}
// ======================================================
// Read Switch Status
// ======================================================
void getSwitchStatus() {
swEast = digitalRead(eastSw);
swWest = digitalRead(westSw);
swNorth = digitalRead(northSw);
swSouth = digitalRead(southSw);
Serial.print("SwE:");
Serial.print(swEast);
Serial.print(" SwW:");
Serial.print(swWest);
Serial.print(" SwN:");
Serial.print(swNorth);
Serial.print(" SwS:");
Serial.println(swSouth);
Serial.println();
}
// ======================================================
// Stop Motors
// ======================================================
void stopMotors() {
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}