I am trying to troubleshoot a sketch that is supposed to read data that has been saved on an FRAM card and then print it onto the serial monitor. Previously, I wrote a sketch that read some altitude sensor data, and wrote it to the FRAM card, but now I am having trouble reading and printing it out.
I don't have much of any background with C++, but after some reading on these forums, I figured out how to use a union to essentially "convert" between the byte storage on the FRAM and the floating point values I am reading off of the sensor. Essentially it reads the sensor value, stores it to a union that contains both a byte structure and a floating point variable, and then writes each individual byte to the FRAM.
That part is successful, which I can see if I dump all the values in hex onto the serial monitor using sample code that came with the FRAM library. So then I wrote a separate sketch to print all the values to the serial monitor as floating point values by going the opposite way. I read each byte, assigned it to the byte structure in the union, and then attempt to print the equivalent floating point value.
Here is my code:
#include <Wire.h>
#include "Adafruit_FRAM_I2C.h"
Adafruit_FRAM_I2C fram = Adafruit_FRAM_I2C();
int counter = 0; //to count through the bytes of fram
// define a union data type to help store the floating point values in fram
union Utype {
byte b[4];
float f;
};
union Utype memAlt; //create a union of type Utype to store values
void setup(void) {
while (!Serial);
Serial.begin(9600);
if (fram.begin()) { // code from the example library that checks to make sure fram board is hooked up correctly
Serial.println("Found I2C FRAM");
} else {
Serial.println("I2C FRAM not identified ... check your connections?\r\n");
Serial.println("Will continue in case this processor doesn't support repeated start\r\n");
}
// Read the first byte and reset it
uint8_t test = fram.read8(0x0);
Serial.print("Restarted "); Serial.print(test); Serial.println(" times");
// Test write ++
fram.write8(0x0, test + 1);
}
//Loop throught the fram memory and read the values, then print to monitor
void loop() {
//read the value from the fram to the union byte values
if (counter < 32000) { //32000 used because fram is 32kb)
memAlt.b[1] = fram.read8(counter + 1); memAlt.b[2] = fram.read8(counter + 2);
memAlt.b[3] = fram.read8(counter + 3); memAlt.b[4] = fram.read8(counter + 4);
}
//print the equivalent floating point number now stored in the union
Serial.println(memAlt.f);
counter = counter + 4; //increment your counter to jump to the next set of bytes
delay(100);
}
After doing some troubleshooting, it starts out ok, gets into the main loop one time, successfully prints "memAlt.f" one time as a floating point, and then on the next time around, it freezes (I guess you call this a hang?) when it enters the if statement for a second time.
At one point during troubleshooting it seemed to almost work when I had a bunch of extra Serial.println statements but I don't understand why that would possibly make a difference.
I am using the following hardware:
Computer: 2012 Mac
FRAM: Adafruit 32kb I2C non-volatile FRAM Adafruit I2C Non-Volatile FRAM Breakout - 256Kbit / 32KByte : ID 1895 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits
Board: Adafruit Trinket M0
Thanks for any help