Go Down

### Topic: Saving variables per scan (Read 1 time)previous topic - next topic

#### theironspud

##### Dec 30, 2011, 08:09 pm
I tried the Search function, but I couldn't find what I was looking for on this...

I'm trying to hold a total, but for some reason it is only giving me instantaneous values.  I'm reading "value" from a sensor over comms.

Code: [Select]
`//Code Beginfloat value_total;//Ommitted comms code- comms is working finevoid setup(){//Omitted comms setup codedelay(1000);}void loop(){delay(1) // 1000 scans per second//Omitted comms code that obtains the variable "value"float value_1 = value_total + (value / 1000);value_total = value_1}`

Shouldn't this give me a totalized amount for value_total?

Thanks!

Matt

#### PaulS

#1
##### Dec 30, 2011, 08:27 pm
Code: [Select]
`//Omitted comms code that obtains the variable "value"`
So, we're supposed to guess what type value is?

Quote
Shouldn't this give me a totalized amount for value_total?

I don't see why.

Code: [Select]
`value_total += value_1;`
would increment the value store in value_total.

Post ALL of your code, not just snippets. How can you tell what the program is doing, with no Serial.print()s?

#### gardner

#2
##### Dec 30, 2011, 11:50 pm

I'm reading "value" from a sensor over comms.
Code: [Select]
`float value_1 = value_total + (value / 1000);`

What is the type of value?  Here (value / 1000) could well be an integer expression, since 1000 is an integer -- that would mean any fractional result is lost.  If you are expecting a float result from  (value / 1000) then best ensure 'value' is a float and divide by 1000.0 (float constant) for good measure.  Assuming that this division is the issue -- which I can't tell for sure since you've left out too much critical code, then something like this could be all you need:

Code: [Select]
`float value_1 = value_total + ( (float) value / 1000.0 );`

#### theironspud

#3
##### Jan 04, 2012, 11:29 pm
Thanks for the replies... all of the code is below (I am using Serial.print()s).  Sorry for the omissions- I was just trying to minimize the amount of code to look through.

Code: [Select]
`//Arduino Uno to ADIS16260 Pin Connections://5 V POWER - VCC//COMMON - GND//D6 - DIO1//D7 - CS//D11 - DIN//D12 - SCLK//D13 - DOUT#include <SPI.h>const int CS = 7;const int Enc_1 = 2;const int Enc_2 = 4;float gyro_total;void setup (){  Serial.begin(9600);  SPI.begin();  pinMode(CS, OUTPUT);  //Encoder Pin 1  pinMode(Enc_1, OUTPUT);  //Encoder Pin 2  pinMode(Enc_2, OUTPUT);  SPI.setBitOrder(MSBFIRST);  SPI.setDataMode(SPI_MODE3);  SPI.setClockDivider(SPI_CLOCK_DIV8);  digitalWrite(CS, HIGH);  delay(1000); }void loop(){digitalWrite(CS, LOW);//Get Gyro readingint gyroH = SPI.transfer(0x04) << 8;int gyroL = SPI.transfer(0x00);int gyroun = (gyroH & (0x3FFF)) + gyroL;float Gyro = (float)gyroun * 0.07326;if (Gyro > 100)  {    float Gyro_n = (-1200.2185 + Gyro);    Gyro = Gyro_n;  }Serial.println("Gyro:");Serial.println(Gyro);digitalWrite(CS,HIGH);delay(1);//End Commsfloat gyro_abs = abs(Gyro);float gyro_zero;if ((gyro_abs < 100.0)&&(gyro_abs > 0.0))  {    float gyro_1 = gyro_total + (Gyro / 1000);    gyro_total = gyro_1;  }if (gyro_total < 0.0)  {    gyro_zero = 0.60 + gyro_total;    gyro_total = gyro_zero;  }  if ((gyro_total >= 0.0)&&(gyro_total < 0.15))  {    digitalWrite(Enc_1, LOW);    digitalWrite(Enc_2, LOW);    Serial.println("Angular Position");    Serial.println("1");  }if ((gyro_total >= 0.15)&&(gyro_total < 0.30))  {    digitalWrite(Enc_1, HIGH);    digitalWrite(Enc_2, LOW);    Serial.println("Angular Position");    Serial.println("2");  }  if ((gyro_total >= 0.30)&&(gyro_total < 0.45))  {    digitalWrite(Enc_1, HIGH);    digitalWrite(Enc_2, HIGH);    Serial.println("Angular Position");    Serial.println("3");  }if ((gyro_total >= 0.45)&&(gyro_total < 0.60))  {    digitalWrite(Enc_1, LOW);    digitalWrite(Enc_2, HIGH);    Serial.println("Angular Position");   Serial.println("4");  } if (gyro_total >= 0.60)  {    digitalWrite(Enc_1, LOW);    digitalWrite(Enc_2, LOW);    gyro_zero = gyro_total - 0.60;    gyro_total = gyro_zero;    Serial.println("Angular Position");    Serial.println("0");  }   }`

All of my live readings for the gyroscope appear to be correct, however- they are not totalizing properly and I can't figure out what I've done wrong.  This code is meant to function as a 600 ppr encoder. Right now, however, it's just acting like a 4-state angular velocity limit switch.

Thanks!

#### PaulS

#4
##### Jan 05, 2012, 11:45 am
Quote
all of the code is below

That's half of what we need to see to help you. Either send us the hardware, now, so we can generate the output that you see, or show us the output. PM me, and I'll give you a mailing address.

#### theironspud

#5
##### Jan 05, 2012, 04:28 pm
Serial.println (Gyro) equals the same value as Serial.println (gyro_total)

#### gardner

#6
##### Jan 05, 2012, 10:17 pm
What are those values?

If Gyro is something like -1200.2185 and then you add something like Gyro += 45 / 1000, you are adding -1200.2185 + .045.
Since the float type only has 5 significant figures of accuracy, the 0.045 is rounding noise and -1200.2185 + .045 is going to basically just yield 1200.2, no matter how many times you add it on.

Is this the kind of effect you're seeing?

#### theironspud

#7
##### Jan 06, 2012, 05:06 pm
That makes a lot of sense.  This helped a lot- I slowed down the communications and watched it update per scan.  As you pointed out- it wasn't the program, it was my math.

Thank you all!

Go Up