Question: My code is working when teensy 3.6 ( similar to Arduino uno ) is connected to PC with a cable, I can see the measurements

My code is working when teensy 3.6 ( similar to Arduino uno ) is connected to PC with a cable, I can see the measurements and I can reach to my sd card with command "r" on the serial monitor. However, when I am using battery the system don't start automatically as soon as I give the power. Every time I check my text file which I am writing it is empty. This code is working, all I want to understand is why not working with the battery and how can I solve that?

/* SD card read/write This example shows how to read and write data to and from an SD card file The circuit: * SD card attached to SPI bus as follows: ** MOSI - pin 11, pin 7 on Teensy with audio board ** MISO - pin 12 ** CLK - pin 13, pin 14 on Teensy with audio board ** CS - pin 4, pin 10 on Teensy with audio board created Nov 2010 by David A. Mellis modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. */

#include #include #include #include #include File myFile; LSM6 imu;

char report[80];

String inputString = ""; // a String to hold incoming data boolean stringRead = false; // whether the string is complete // change this to match your SD shield or module; // Arduino Ethernet shield: pin 4 // Adafruit SD shields and modules: pin 10 // Sparkfun SD shield: pin 8 // Teensy audio board: pin 10 // Teensy 3.5 & 3.6 on-board: BUILTIN_SDCARD // Wiz820+SD board: pin 4 // Teensy 2.0: pin 0 // Teensy++ 2.0: pin 20 const int chipSelect = BUILTIN_SDCARD; int address1 = 40; //(i2c adress teensy) int pin_SDA = 38; //i2c SDA int pin_SCL = 37; //i2c SCL

void setup() { //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD: //SPI.setMOSI(7); // Audio shield has MOSI on pin 7 //SPI.setSCK(14); // Audio shield has SCK on pin 14 inputString.reserve(200); setSyncProvider(getTeensy3Time); Serial.begin(115200); SD.begin(chipSelect); //i2c //Wire.begin(address1); //i2c //Wire.setSDA(38); //i2c //Wire.setSCL(37); //i2c Wire.begin(address1); delay(1000); //i2c if (!imu.init()) { Serial.println("Failed to detect and initialize IMU!"); while (1); } imu.enableDefault(); Serial.println("Begin!"); if (timeStatus()!= timeSet) { Serial.println("Unable to sync with the RTC"); } else { Serial.println("RTC has set the system time"); }

// Open serial communications and wait for port to open: while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only }

// open the file. note that only one file can be open at a time, // so you have to close this one before opening another. myFile = SD.open("test10.txt", FILE_WRITE); } int num=0;

void loop() { imu.read();

snprintf(report, sizeof(report), "A: %6d %6d %6d G: %6d %6d %6d", imu.a.x, imu.a.y, imu.a.z, imu.g.x=0, imu.g.y=0, imu.g.z=0); Serial.println(report);

delay(100); int c; //i2c

Wire.beginTransmission(address1); //i2c Wire.write(0); Wire.endTransmission(); //i2c Wire.requestFrom(address1, 2); //i2c c = Wire.receive(); //i2c Serial.print("Temperature: "); //i2c Serial.println(c); //i2c delay(1000); //i2c myFile=SD.open("test10.txt",FILE_WRITE); if(myFile){ Serial.println(); // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.print("The obstacle is "); Serial.print(sensorValue); Serial.print(" cm away."); Serial.print(" Exact time: "); if (Serial.available()) { time_t t = processSyncMessage(); if (t != 0) { Teensy3Clock.set(t); // set the RTC setTime(t); } } digitalClockDisplay(); delay(1000); myFile.print(sensorValue); myFile.print(","); myFile.print(report); myFile.print(hour()); printDigits2(minute()); printDigits2(second()); myFile.print(" "); myFile.print(day()); myFile.print(" "); myFile.print(month()); myFile.print(" "); myFile.print(year()); myFile.println(); myFile.close(); } if (stringRead) { Serial.println(inputString); // clear the string: myFile = SD.open("test10.txt"); if (myFile) { Serial.println("test10.txt:"); // read from the file until there's nothing else in it: while (myFile.available()) { Serial.write(myFile.read()); } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening test.txt"); } } stringRead=false; }

time_t getTeensy3Time() { return Teensy3Clock.get(); }

/* code to process time sync messages from the serial port */ #define TIME_HEADER "T" // Header tag for serial time sync message

unsigned long processSyncMessage() { unsigned long pctime = 0L; const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013

if(Serial.find(TIME_HEADER)) { pctime = Serial.parseInt(); return pctime; if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013) pctime = 0L; // return 0 to indicate that the time is not valid } } return pctime; }

void digitalClockDisplay() { // digital clock display of the time Serial.print(hour()); printDigits(minute()); printDigits(second()); Serial.print(" "); Serial.print(day()); Serial.print(" "); Serial.print(month()); Serial.print(" "); Serial.print(year()); }

void printDigits2(int digits){ // utility function for digital clock display: prints preceding colon and leading 0 myFile.print(":"); if(digits < 10) myFile.print('0'); myFile.print(digits); } void printDigits(int digits){ // utility function for digital clock display: prints preceding colon and leading 0 Serial.print(":"); if(digits < 10) Serial.print('0'); Serial.print(digits); } void serialEvent() { while (Serial.available()) { // get the new byte: char inChar = Serial.read(); // add it to the inputString: // if the incoming character is a newline, set a flag so the main loop can // do something about it: if (inChar == 'r') { stringRead = true; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!