Go Down

Topic: Saving variables per scan (Read 629 times) previous topic - next topic

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 Begin
float value_total;
//Ommitted comms code- comms is working fine
void setup()
{
//Omitted comms setup code
delay(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

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


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 );

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 reading
int 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 Comms
float 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

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.

Serial.println (Gyro) equals the same value as Serial.println (gyro_total)


gardner

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?

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