dimanche 31 mai 2015

IF Statement manipulates variable without writing to it

I hope the Arduino Experts can help me with my Problem.

I have a Gesture, Color, Proximity and Light Sensor (All in One) connected to an Arduino UNO. I am trying to change the Gain of Gesture Sensing depending on the Proximity Value. Everything works perfectly fine if i don't use the if statement which checks and compare the current proximity value stored in a variable. The If Statement is never supposed to write or anyhow change the variable as it ONLY reads from it but exactly that seems to happen here.

This is the complete Code:

#include <I2C.h>

uint8_t prox;
uint8_t gain;

void setup() {
  Serial.begin(9600);
  I2c.begin();
  I2c.setSpeed(1);
  I2c.pullup(1);
  tmginit();
}

void tmginit() {
  prox = 0;
  gain = 1;
  I2c.write(57, 128, 69);  //0x80 Gesture, Proximity and Power enabled
  I2c.write(57, 171, 3);   //0xAB Hardware interrupt and gmode enabled
  I2c.write(57, 142, 224); //0x8E PPLEN 32µs
  I2c.write(57, 143, 0);   //0x8F LDRIVE 100% Mode
  I2c.write(57, 144, 16);  //0x90 LEDBOOST 300% and 2x prox gain
  I2c.write(57, 160, 0);   //0xA0 Gesture Proximity Entry Threshold set to 0
  I2c.write(57, 161, 255); //0xA1 Gesture Exit Threshold set to maximum
  I2c.write(57, 163, 0);   //0xA3 Gain set to 4x
  I2c.write(57, 164, 2);   //0xA4 Gesture North Offset
  I2c.write(57, 165, 1);   //0xA5 Gesture South Offset
  I2c.write(57, 166, 224); //0xA6 Gesture PWM Length set to 32µs
  I2c.write(57, 167, 1);   //0xA7 Gesture West Offset
  I2c.write(57, 169, 2);   //0xA9 Gesture East Offset
}

void loop() {

  prox = 0;
  Serial.println();
  I2c.read(57, 156, 1); // Proximity Value
  prox = I2c.receive();
  Serial.println("PROX:");
  Serial.println(prox);

  if ((prox >= 225) && (gain != 1))
  {

    I2c.write(57, 163, 0);
    gain = 1;

  }

  else if ((prox >= 100) && (prox < 225) && (gain != 2))
  {

    I2c.write(57, 163, 32);
    gain = 2;

  }

  else if ((prox >= 30) && (prox < 100) && (gain != 4))
  {

    I2c.write(57, 163, 64);
    gain = 4;

  }

  else if ((prox >= 0) && (prox < 30) && (gain != 8))
  {

    I2c.write(57, 163, 96);
    gain = 8;

  }

  Serial.println();
  Serial.println("GAIN:");
  Serial.println(gain);
  Serial.println();


  I2c.read(57, 252, 1); //FIFO N

  Serial.println(I2c.receive(), DEC);
  I2c.read(57, 253, 1); //FIFO S

  Serial.println(I2c.receive(), DEC);
  I2c.read(57, 254, 1); //FIFO W

  Serial.println(I2c.receive(), DEC);
  I2c.read(57, 255, 1); //FIFO E

  Serial.println(I2c.receive(), DEC);

  delay(500);


}

The Proximity Value outputs really strange values if i have the if and else if block actively running. But when I comment the complete if and else if Block out, everything runs perfectly. The Proximity Value changes when i come near the sensor or when i go away from the sensor.

Example:

I am very close to the Sensor. The Proximity Value is somewhere between 200 and 255 (255 is max). This is the correct behaviour.

Now i activate the if and else if block and do the same thing again

I am very close to the Sensor. The Proximity Value is somewhere between 5 and 10 (255 is max)

Only the very first read WITH if and else is correct. From then on all this weird stuff starts to happen.

Please help!

Thank You.

Aucun commentaire:

Enregistrer un commentaire