Hello there,
Can any one help me in codeing for one of my project as im new to arduino and coding .im using this bourns encoder ace128 to get positions for single axis solar tracking.
The problem im facing is while i upload the code it works great but when the system is restarted the position is set to 0 again when if its actual value is different..tried using EEPROM but in vein.
Here im giving the code. Please do the need full, thanks in advance
#include <Wire.h>
#include <ACE128.h>
#include <ACE128map12345678.h>
#include <RTClib.h> // Include the RTClib library for DS3231 RTC
#define bourns_addr 0x20 // I2C address of ACE128 encoder
ACE128 myACE((uint8_t)bourns_addr, (uint8_t*)encoderMap_12345678); // Initialize encoder
RTC_DS3231 rtc; // Initialize DS3231 RTC
int16_t multiturn_encoder_value = 0; // Tracks overall position
int8_t last_position = 0; // Stores the last encoder reading
// Define pins for BTS7960 motor driver
const int RPWM = 5; // Right PWM
const int LPWM = 6; // Left PWM
const int R_EN = 7; // Right Enable
const int L_EN = 8; // Left Enable
void setup() {
Serial.begin(9600);
Wire.begin();
myACE.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1); // Stop execution if RTC is not found
}
// Set the date and time (uncomment this line for the initial setup, then comment it out)
rtc.adjust(DateTime(2024, 11, 14, 9, 0, 0)); // Year, Month, Day, Hour, Minute, Second
// Initialize the encoder's position tracking
last_position = myACE.pos();
// Initialize motor control pins
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
pinMode(R_EN, OUTPUT);
pinMode(L_EN, OUTPUT);
digitalWrite(R_EN, HIGH); // Enable Right motor control
digitalWrite(L_EN, HIGH); // Enable Left motor control
}
void loop() {
int8_t current_position = myACE.pos(); // Get current position (0 to 127)
// Check for clockwise or counterclockwise movement
int8_t delta = current_position - last_position;
// Account for wrap-around at 127 to 0 (or 0 to 127)
if (delta > 64) delta -= 128; // Adjust for overflow clockwise
else if (delta < -64) delta += 128; // Adjust for overflow counterclockwise
// Update multiturn encoder value
multiturn_encoder_value += delta;
// Only print if the encoder value has changed
if (delta != 0) {
Serial.print("Multiturn Encoder Value: ");
Serial.println(multiturn_encoder_value);
}
// Update last position
last_position = current_position;
// Get current time from DS3231
DateTime now = rtc.now();
Serial.print("Current Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
// Print current encoder position
Serial.print("Current Encoder Position: ");
Serial.println(current_position);
// Check for the specific times and move motor to target positions
if (now.hour() == 9 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 4) {
Serial.println("Moving motor to position 4");
moveForward();
} else if (now.hour() == 10 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 6) {
Serial.println("Moving motor to position 6");
moveForward();
} else if (now.hour() == 10 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 8) {
Serial.println("Moving motor to position 8");
moveForward();
} else if (now.hour() == 11 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 10) {
Serial.println("Moving motor to position 10");
moveForward();
} else if (now.hour() ==11 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 12) {
Serial.println("Moving motor to position 12");
moveForward();
} else if (now.hour() == 12 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 14) {
Serial.println("Moving motor to position 14");
moveForward();
} else if (now.hour() == 12 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 17) {
Serial.println("Moving motor to position 17");
moveForward();
} else if (now.hour() == 13 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 20) {
Serial.println("Moving motor to position 20");
moveForward();
} else if (now.hour() == 13 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 22) {
Serial.println("Moving motor to position 22");
moveForward();
} else if (now.hour() == 14 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 24) {
Serial.println("Moving motor to position 24");
moveForward();
} else if (now.hour() == 14 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 26) {
Serial.println("Moving motor to position 26");
moveForward();
} else if (now.hour() == 15 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 30) {
Serial.println("Moving motor to position 30");
moveForward();
} else if (now.hour() == 15 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 34) {
Serial.println("Moving motor to position 34");
moveForward();
} else if (now.hour() == 16 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value < 38) {
Serial.println("Moving motor to position 38");
moveForward();
} else if (now.hour() == 16 && now.minute() == 30 && now.second() >= 0 && multiturn_encoder_value < 41) {
Serial.println("Moving motor to position 41");
moveForward();
} else if (now.hour() == 19 && now.minute() == 0 && now.second() >= 0 && multiturn_encoder_value >= 19) {
// Example of moving backward to position 19
Serial.println("Moving motor backward to position 19");
moveBackward();
} else if (now.hour() == 19 && now.minute() == 10 && now.second() >= 0 && multiturn_encoder_value >= 3) {
// Example of moving backward to position 3
Serial.println("Moving motor backward to position 3");
moveBackward();
} else if (now.hour() == 19 && now.minute() == 20 && now.second() >= 0 && multiturn_encoder_value >= 0) {
// Example of moving backward to position 0
Serial.println("Moving motor backward to position 0");
moveBackward();
} else {
stopMotor(); // Stop the motor if the target position is reached or time condition is not met
}
delay(500); // Delay for readability
}
void moveForward() {
analogWrite(RPWM, 255); // Set Right PWM to full speed
analogWrite(LPWM, 0); // Left PWM off
}
void moveBackward() {
analogWrite(RPWM, 0); // Right PWM off
analogWrite(LPWM, 255); // Set Left PWM to full speed (reverse direction)
}
void stopMotor() {
analogWrite(RPWM, 0); // Turn off Right PWM
analogWrite(LPWM, 0); // Turn off Left PWM
}