Hi all,
I'm relatively new to Arduino and I'm working on a project with solar energy. I'm trying to create a single axis solar tracker using a DC motor and LDRs. The way my code works is by comparing the voltages of the LDRs and the motor follows the LDR with more light. As of now, the DC motor follows the LDR with more light hitting on it, but I would like the motor to rotate at certain angles instead of continuously rotate and I don't know how to achieve this. Please help if you can!
Here is the code:
#define E1 10 // Enable Pin for motor 1
#define I1 8 // Control pin 1 for motor 1
#define I2 9 // Control pin 2 for motor 1
void setup() {
//set the pin mode for the motor to be an output
pinMode(E1, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
// Convert the analog value into voltage
float volt1=sensorValue*(5.0/1023.0);
float volt2=sensorValue1*(5.0/1023.0);
if (volt1 > volt2){
digitalWrite(E1, 1);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
}
if (volt2 > volt1){
digitalWrite(E1, LOW);
digitalWrite(E1, 1);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
}
if (volt2 == volt1) {
digitalWrite(E1, LOW);
}
//print voltages
Serial.print("sensorValue: ");
Serial.println(volt1);
Serial.print(' ');
Serial.print("sensorValue1: ");
Serial.println(volt2);
Serial.print('\n');
delay (1000);
}
Attached is the picture of the circuit I'm using.