Firstly...I know this question has been answered many times with regards to millis() and micros() regarding rollover of long variables to do with counting, but I am still having trouble understanding.
I am working on a test rig to test impact resistance of light fittings.
This jig has a swinging arm with a large hammer at the end that will be used to smash the front face of the fitting. In order to monitor the amount of force behind the hammer, we have calculated the weight and the travel distance etc and come up with a table showing the angles we need to set the hammer to at the start of the test to give us different ratings
Im using an arduino Mega with a Bourns 1024 absolute encoder which provides a binary output giving the absolute co-ordinates. My software can read the data from the encoder, convert it to decimal and then convert to a float to give the exact angle. I then multiply the float and convert to a long value in order to divide and modulo each digit to send one at a time to a touch screen display. All of this is working perfectly.
I am struggling with the next part, which is to create an offset angle, so that regardless of the absolute position of the encoder, we have a button that will create a 0 offset. I then want this offset to plus or minus as the encoder moves, the same way as before, only this time to either add or subtract the offset value.
I am using long variables to give me 3 digits and 5 decimal places, which is a total of 8 digits. In order to fit this large number in, I have to use an unsigned long with a maximum of 2^16-1 or 4294967295.
The angle position will give a range of 0 - 35999999.
Due to the rollover, when my offset value is larger than the absolute value I am subtracting from, the long goes into the 4000000000 range and therefore messes up my calculations.
I have attached snippets from my code below, which hopefully someone will spot the errors instantly. lol.
Please can anybody help me out? Any help would be greatly appreciated. If I need to post more info etc, just let me know.
byte Encoder_Position[10] = {0,0,0,0,0,0,0,0,0,0};
unsigned int Encoder_Dec = 0;
float Scale = 0.3515625, Angle = 000.00000;
unsigned long Angle_Integer = 0, Offset_Angle_Integer = 0;
byte LCD_Angle[8] = {0,0,0,0,0,0,0,0}, LCD_Offset_Angle[8] = {0,0,0,0,0,0,0,0};
void EncoderConvert(){
Encoder_Dec = 0;
Encoder_Dec += ((Encoder_Position[0]) * (pow(2, 9)));
Encoder_Dec += ((Encoder_Position[1]) * (pow(2, 8)));
Encoder_Dec += ((Encoder_Position[2]) * (pow(2, 7)));
Encoder_Dec += ((Encoder_Position[3]) * (pow(2, 6)));
Encoder_Dec += ((Encoder_Position[4]) * (pow(2, 5)));
Encoder_Dec += ((Encoder_Position[5]) * (pow(2, 4)));
Encoder_Dec += ((Encoder_Position[6]) * (pow(2, 3)));
Encoder_Dec += ((Encoder_Position[7]) * (pow(2, 2)));
Encoder_Dec += ((Encoder_Position[8]) * (pow(2, 1)));
Encoder_Dec += ((Encoder_Position[9]) * (pow(2, 0)));
Angle = Scale * Encoder_Dec;
Angle_Integer = Angle * 100000;
LCD_Angle[0] = (Angle_Integer / 10000000);
LCD_Angle[1] = (Angle_Integer % 10000000) / 1000000;
LCD_Angle[2] = (Angle_Integer % 1000000) / 100000;
LCD_Angle[3] = (Angle_Integer % 100000) / 10000;
LCD_Angle[4] = (Angle_Integer % 10000) / 1000;
LCD_Angle[5] = (Angle_Integer % 1000) / 100;
LCD_Angle[6] = (Angle_Integer % 100) / 10;
LCD_Angle[7] = (Angle_Integer % 10);
}
void OffsetConvert(){
LCD_Offset_Angle[0] = (Angle_Integer - Offset_Angle_Integer) / 10000000;
LCD_Offset_Angle[1] = ((Angle_Integer - Offset_Angle_Integer) % 10000000) / 1000000;
LCD_Offset_Angle[2] = ((Angle_Integer - Offset_Angle_Integer) % 1000000) / 100000;
LCD_Offset_Angle[3] = ((Angle_Integer - Offset_Angle_Integer) % 100000) / 10000;
LCD_Offset_Angle[4] = ((Angle_Integer - Offset_Angle_Integer) % 10000) / 1000;
LCD_Offset_Angle[5] = ((Angle_Integer - Offset_Angle_Integer) % 1000) / 100;
LCD_Offset_Angle[6] = ((Angle_Integer - Offset_Angle_Integer) % 100) / 10;
LCD_Offset_Angle[7] = (Angle_Integer - Offset_Angle_Integer) % 10;