Bourns ace 128 values resetting

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
}

This forum is not a free coding service. Unlike for you, for us there is no "need full".

We will help you fix your code, but we will not do it for you.

If you can offer payment for this service, we can move your post to a forum section where someone may fix it for you. But take great care who you trust.

How much do you expect?

I have no idea, sorry.

You must write a homing program to make it go to your "actual value" position.

$250 per hour plus expenses.

You need to get EEPROM working, it's a fairly simple UI, what board are you using?
BTW, it's normal to use a light detector to track, time can be used as a gross position until you have the sun in the middle of your view. Lot's of examples of that on the net.

I tried with eeprom too..im using an arduino uno..i did it with light sensors too that worked but wont face sun when its cloudy,instead it reverses where there more light.

Wbat im doing is tracking to defined path of the sun.

I would really apprectiate if you help.how do i do it?

You will see many homing programs do the same thing... one example...

Thanks a lot for this..but i have home position set in my command..problem is if my system is shut off at position 18 ( take for example) and when it restarts the position 18 is considered new 0..so all other calculations to move forward and backwaed is disturbed and it goes to wrong positions..

What does the encoder read when the panel is pointed at the meridian?

Depends upon the equinox

Use the EEPROM, post that code and lets see what might be wrong.

I see. Good luck.

I see you're using a RTC, so you already have the time.
You can calculate solar angle on bootup (or periodically) from UTC time and your GPS coordinates with this library. This line will calculate sun azimuth (and elevation).
calcHorizontalCoordinates(now, latitude, longitude, az, el);

Hint: right-click your location on Google maps to see you lat/long,
click on the numbers to save to clipboard.
Leo..

You do not have a homing mechanism. You have code that "remembers" where zero is. What use is that if it can't find home?

Why would an "absolute" encoder need homing?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.