I want to make a solar tracker with Arduino. It is single axis only in azimuth direction - this is my code - when I run the code my DC motor does not start
#include <SD.h>
#include <SPI.h>
#include <RTClib.h>
// Pin connections
#define SD_CS 10
#define MOTOR_IN1 5
#define MOTOR_IN2 6
#define MOTOR_ENA 9
RTC_DS3231 rtc;
File myFile;
void setup() {
Serial.begin(9600);
// Initialize SD card
if (!SD.begin(SD_CS)) {
Serial.println("SD card initialization failed!");
while (1);
}
Serial.println("SD card initialized.");
// Initialize RTC
if (!rtc.begin()) {
Serial.println("RTC initialization failed!");
while (1);
}
Serial.println("RTC initialized.");
// Display current date and time
DateTime now = rtc.now();
Serial.print("Current RTC Date: ");
Serial.print(now.month());
Serial.print("/");
Serial.print(now.day());
Serial.print("/");
Serial.println(now.year());
// Initialize motor pins
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
pinMode(MOTOR_ENA, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
// Format date for CSV comparison: MM/DD/YY
String currentDate = String(now.month()) + "/" + String(now.day()) + "/" + String(now.year() % 100);
Serial.print("Searching for date in CSV: ");
Serial.println(currentDate);
if (readCSVAndControlMotor(currentDate)) {
Serial.println("Motor control completed for today.");
} else {
Serial.println("No matching date found in CSV.");
}
delay(60000); // Recheck every minute
}
bool readCSVAndControlMotor(String currentDate) {
myFile = SD.open("test.csv");
if (!myFile) {
Serial.println("Failed to open CSV file!");
return false;
}
bool dateFound = false;
bool isHeader = true;
while (myFile.available()) {
String line = myFile.readStringUntil('\n');
line.trim();
if (isHeader) {
isHeader = false;
continue;
}
// Split the values using commas
int commaIndex[4];
int indexCounter = 0;
for (int i = 0; i < line.length(); i++) {
if (line[i] == ',') {
if (indexCounter < 4) { // Prevent overflow of the array
commaIndex[indexCounter++] = i;
}
}
}
if (indexCounter != 4) {
Serial.println("Invalid CSV line format: " + line); // Print invalid line
continue;
}
String date = line.substring(0, commaIndex[0]);
String sunrise = line.substring(commaIndex[0] + 1, commaIndex[1]);
String sunset = line.substring(commaIndex[1] + 1, commaIndex[2]);
String delayTimeStr = line.substring(commaIndex[3] + 1);
Serial.print("Comparing RTC date: ");
Serial.println(currentDate);
Serial.print("With CSV date: ");
Serial.println(date);
if (date == currentDate) {
dateFound = true;
Serial.println("Date matched!");
Serial.print("Sunrise: "); Serial.println(sunrise);
Serial.print("Sunset: "); Serial.println(sunset);
Serial.print("Delay Time: "); Serial.println(delayTimeStr);
// Convert delay time to an integer and check its value
long delayTime = delayTimeStr.toInt();
if (delayTime <= 0) {
Serial.println("Invalid delay time, setting to default (1000ms)");
delayTime = 1000; // Default delay value
}
if (delayTime > 0) {
controlMotor(15, delayTime); // 15 is for motor steps
} else {
Serial.println("Invalid DELAY value in CSV.");
}
break;
}
}
myFile.close();
return dateFound;
}
void controlMotor(int numStep, int delayTime) {
Serial.print("Starting motor control. Steps: ");
Serial.print(numStep);
Serial.print(", Delay: ");
Serial.println(delayTime);
for (int i = 0; i < numStep; i++) {
// Motor movement counterclockwise
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
digitalWrite(MOTOR_ENA, HIGH); // Set enable to HIGH
delay(1500); // Run motor for 1500 milliseconds
// Stop motor
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, LOW);
delay(delayTime); // Use delay time from CSV
}
Serial.println("Motor control finished.");
}