Hi, this is more of a software/Gamebuino/Arduino question but: I created a âgameâ to log data from a i2c sensor to the SDcard using SdFat.h
(i originally tried using tinyFAT.h, however it seemed to hit an error initializing the sdcard and would not print the error to a popup - it was just printing gobbldeygook)
Anyway, it compiles and loads, but crashes as soon as i hit âAâ to launch the game.
Any help gratefully accepted.
Code as below
#include <SPI.h> //imports the SPI library (needed to communicate with Gamebuinoâs screen)
#include <Gamebuino.h> //imports the Gamebuino library
#include <Sodaq_SHT2x.h> // temperature and humidity sensor
#include <Wire.h> // I2C functions for DS1307
#include <string.h>
#include <avr/pgmspace.h>
const int chipSelect = 10; //for sdcard
#include <SdFat.h> // requires sdfat, get it at https://github.com/greiman/SdFat
SdFat sd;
SdFile file;//creates a Gamebuino object named gb
Gamebuino gb;//Variables
byte res;// for tinyfat
float currTemperature;
float currHumidity;
float currDewpoint;
double Twetbulb = 0;
const long shtinterval = 1000; // interval at which to update temperature reading (milliseconds)
unsigned long previousMillis = 0; // will store last time reading was updated
//extern const byte font5x7[]; //a large, comfy font
//extern const byte font3x5[]; //a small but efficient font (default)
const char* LogFileName = âTMPLOG.TXTâ;
char dataString[] = ââ; // holds the data to be written to the SD card// the setup routine runs once when Gamebuino starts up
void setup() {// initialize the Gamebuino object
gb.begin();//gb.display.drawBitmap(0, 12, logo);
gb.display.println(F("\35 Reading SD cardâŠ\n\n"));
gb.display.update();if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
gb.display.println(F(âInsert SD & restartâ));
gb.display.update();
while (1);
}sd.chdir(â/â);
//display the main menu:
gb.titleScreen(F(âTemperature/humidityâ));Wire.begin();
}
// the loop routine runs over and over again forever
void loop() {
unsigned long currentMillis = millis();
//updates the gamebuino (the display, the sound, the auto backlight⊠everything)
//returns true when itâs time to render a new frame (20 times/second)
if (gb.update()) {
//pause the game if C is pressed
if (gb.buttons.pressed(BTN_C)) {
gb.titleScreen(F(âTemperature/humidityâ));
gb.battery.show = false;
gb.display.fontSize = 2;
}
//prints Hello World! on the screen
//gb.display.println(F(âHello World!â));
//declare a variable named count of type integer :
//int count;
//get the number of frames rendered and assign it to the âcountâ variable
//count = gb.frameCount;
//prints the variable âcountâ
//gb.display.println(count);
gb.display.fontSize = 1;
gb.display.println(" Temps / Humidity");
gb.display.println("");
gb.display.fontSize = 2;
//gb.display.setFont(font5x7);
//gb.display.setFont(font3x5); //excluded to save memory
//gb.display.print(â5x7 font\nâ);
gb.display.print("T ");
gb.display.print(currTemperature);
gb.display.println(â°Câ);
gb.display.print(âH â);
gb.display.print(currHumidity);
gb.display.println(â%â);
gb.display.print("D ");
gb.display.print(currDewpoint);
gb.display.println(â°Câ);
gb.display.fontSize = 1;}
if (currentMillis - previousMillis >= shtinterval) {
// save the last time you blinked the LED
previousMillis = currentMillis;// update readings: currTemperature = SHT2x.GetTemperature(); currHumidity = SHT2x.GetHumidity(); //Twetbulb = currTemperature * atan(0.151977 * pow( currHumidity + 8.313659, 0.5)) + atan( currTemperature + currHumidity ) - atan( currHumidity - 1.676331) + 0.00391838 * pow(currHumidity, 1.5) * atan(0.023101 * currHumidity) - 4.686035; currDewpoint = SHT2x.GetDewPoint(); //log readings to sdcard File dataFile = sd.open(LogFileName, FILE_WRITE); // if the file is available, write to it: if (dataFile) { //dataString = String(currTemperature) + "," + String(currHumidity); strcat(dataString, dtostrf(currTemperature, 5, 2, dataString)); strcat(dataString, ","); strcat(dataString, dtostrf(currHumidity, 5, 2, dataString)); dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { gb.popup(F("error opening TMPLOG.TXT"), 20); }
}
}