Go Down

Topic: Adafruit 8833 transferring for loops to a class (Read 115 times) previous topic - next topic

femaledroid

Dec 07, 2017, 03:09 pm Last Edit: Dec 07, 2017, 05:27 pm by femaledroid Reason: Adding my code
Hi,

I'm fairly new to arduino, and I'm working on a robotic project that requires multiple sensors to work simultaneously together. In my main code, I've had luck with changing my individual sensor codes to classes, and calling them all in my main loop. I'm struggling to do the same thing for the grideye adafruit 8833 thermal camera, which displays an 8x8 grid of temperatures. The code requires multiple "for" loops which complicates things. I tried doing "goto" and putting a label. I then transfer the code into processing to display my little grid of temperatures. But right now, the class is just giving me "NaN" values. The other arduino script I attached, "grideye", works. And the processing script also works. Can anyone see any obvious problems in my arduino code? Perhaps my label is in the wrong place? Thanks!

EDIT: Here is the code that works:
Code: [Select]

#include <Wire.h>

byte pixelTempL;
byte pixelTempH;
char addr = 0x69;
float celsius;

void setup() {
Wire.begin();
Serial.begin(9600); //change back to 115200 if not working
}
void loop()
{
//First two data registers for the pixel temp data are 0x80 and 0x81
pixelTempL=0x80;
pixelTempH=0x81;
//Get Temperature Data for each pixel in the 8x8 array. Will loop 64 times.
for(int pixel = 0; pixel <= 63; pixel++)
   {
//Get lower level pixel temp byte
Wire.beginTransmission(addr);
Wire.write(pixelTempL);
Wire.endTransmission();
Wire.requestFrom(addr,1);
byte lowerLevel = Wire.read(); //
//Get upper level pixel temp byte
Wire.beginTransmission(addr);
Wire.write(pixelTempH);
Wire.endTransmission();
Wire.requestFrom(addr,1);
byte upperLevel = Wire.read();
//Combine the two bytes together to complete the 12-bit temp reading
int temperature = ((upperLevel << 8) | lowerLevel);
//Temperature data is in two's compliment, do conversion.
if (upperLevel != 0)
  {
  temperature = -(2048 - temperature);
  }
celsius = temperature*0.25;
pixelTempL=pixelTempL+2;
pixelTempH=pixelTempH+2;
Serial.print(celsius,DEC);
Serial.println(",");
 }
} //end loop


And here is the class that I'm trying to change the above code to:
Code: [Select]

#include <Wire.h>

class Infrared
{
  int pixel;
  byte pixelTempL;
  byte pixelTempH;
  char addr = 0x69;
  float celsius;
  // These maintain the current state
  int increment;          // ledState used to set the LED
  int updateInterval;
  unsigned long previousMillis;   // will store last time LED was updated
 
  // Constructor - creates a Flasher
  // and initializes the member variables and state
  public:
  Infrared(int interval)
  {
  Serial.begin(9600); 
  Wire.begin(); 
  updateInterval = interval;
  increment=1;
  previousMillis = 0;
  }

  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
     
    if((pixel < 63) && (currentMillis - previousMillis >= updateInterval))
    {
      label:
      previousMillis = currentMillis;  // Remember the time
      pixel += increment;
      Wire.beginTransmission(addr);
      Wire.write(pixelTempL);
      Wire.endTransmission();
      Wire.requestFrom(addr,1);
      byte lowerLevel = Wire.read(); //
      //Get upper level pixel temp byte
      Wire.beginTransmission(addr);
      Wire.write(pixelTempH);
      Wire.endTransmission();
      Wire.requestFrom(addr,1);
      byte upperLevel = Wire.read();
      //Combine the two bytes together to complete the 12-bit temp reading
      int temperature = ((upperLevel << 8) | lowerLevel);
      if (upperLevel != 0)
        {
          temperature = -(2048 - temperature);
        }
      celsius = temperature*0.25;
      pixelTempL=pixelTempL+2;
      pixelTempH=pixelTempH+2;
      Serial.print(celsius,DEC);
      Serial.println(",");
     
    }
    else if ((pixel >= 63) && (currentMillis - previousMillis >= updateInterval))
    {
      pixel=0;
      goto label;
      previousMillis = currentMillis;   // Remember the time
    }
  }
};

Infrared ir_sensor(100);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
 
  ir_sensor.Update();
}

Go Up