Go Down

Topic: Serial.print() gone wild? (Read 2057 times) previous topic - next topic

tetwin11

Jan 19, 2011, 09:10 pm Last Edit: Jan 19, 2011, 09:44 pm by tetwin11 Reason: 1
Hello,
I am making a data logger using the ADIS16367 from Analog Devices, and saving to an SD card. I use a timer interrupt to sample the sensor and it creates a String object. In the loop, after a certain number of samples, I convert it to a char array and print to the SD card. However, after the SD card initializes and it begins to read the sensor it keeps printing:

Initializing SD card...Initializing SD card...Initializing SD card...Initializing SD card...Initializing SD card...Initializing SD card...

What's up with that?!
It is supposed to print once at the top, but then it should be gone, so I have no idea what is going on here... any ideas/advice?

Thanks for any help in advance!


Code: [Select]

#include <SD.h>
#include <SPI.h>
#include "TimerOne.h"

#define VERBOSE 1              // 1 to print to serial, 0 to not
#define SAMPLES_PER_WRITE 2    // samples per write to SD card
#define FS 1                  // sample frequency (Hz)
#define TIME 5                 // time to sample (s)

//Sensor's memory register addresses:
const byte SUPPLY_OUT = 0x02;    // Power supply reading
...
const byte SERIAL_NUM = 0x58;    // Serial number

//Sensor commands
const byte READ = 0b00000000;    // ADIS16367 read command (2 bytes)
const byte WRITE = 0b10000000;   // ADIS16367 write command
const byte BURST_READ = 0x3E;    // ADIS16367 burst read command

// pins used for the connections
const int dataReadyPin = 2;
const int ADIScs = 7;
const int SDcs = 8;

volatile byte samplecounter = 0;
volatile unsigned int counter = 0;
String dataString = "time(ms) sensors...\r\n";

void setup()
{
 Serial.begin(115200);
 Serial.print("Initializing SD card...");
 Serial.flush();
 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
 pinMode(10, OUTPUT);
 
 // start communication with SD card
 pinMode(SDcs, OUTPUT);
 if (!SD.begin(SDcs)) {
   Serial.println("Card failed, or not present");
   while(1);
 }
 Serial.println("card initialized.");
 
 // start SPI communication with ADIS16367
 SPI.begin();
 pinMode(ADIScs, OUTPUT);
 digitalWrite(ADIScs, HIGH);
 SPI.setClockDivider(SPI_CLOCK_DIV16);
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE3); // set SPI mode 3

 // start Timer1
 Timer1.initialize((1000000UL/FS)); // Start timer at FS Hz
}

void loop()
{
 Serial.flush();
 Serial.println("Type any character to start");
//  String dataString = 'time(ms) sensors...\r\n';
 while (!Serial.available());
 digitalWrite(SDcs, LOW);

 char filename[] = "datalog.txt";
 if(SD.exists(filename)){SD.remove(filename);Serial.print(filename);Serial.println(" removed...");}

 // open the file. note that only one file can be open at a time,
 // so you have to close this one before opening another.
 File dataFile = SD.open(filename, FILE_WRITE);
 if(!dataFile) { // if the file isn't open, pop up an error
   Serial.println("error opening datalog.txt");
   while(1);
 }
 
 Serial.println("Begin logging...");
 Timer1.attachInterrupt(burstReadString2);  // attaches timer overflow interrupt
 unsigned int start = millis();
 String writeString = " ";
 while( (millis()-start) < (TIME*1000) ){
   if(samplecounter>SAMPLES_PER_WRITE) {
     // Move data to new buffer String and erase old data
     writeString = dataString;
     dataString = " ";
     samplecounter = 0;
     // Convert String to string (char array)
     int strlen = writeString.length();
     char string[strlen+1];
     for(int jj=0 ; jj<strlen ; jj++) {
       string[jj] = writeString.charAt(jj);
     }
     // Make sure the string is ended correctly
     string[strlen] = '\0';
     // Write it to SD card
//      dataFile.write(string);
//      if(VERBOSE) Serial.print(string);
//      Serial.print(string);
   }
 }
 Timer1.detachInterrupt();
 dataFile.close();
 Serial.println("Logging complete");
 Serial.println(millis()-start);
 Serial.println();
 // Finish and don't do anything
 while(1);
}




//Sends a burst read command to ADIS16367
// and create String
void burstReadString2(void) {
//  dataString += "woot!\r\n";
 word data = 0;
 // take the chip select low to select the device:
 digitalWrite(ADIScs, LOW);
 digitalWrite(SDcs,HIGH);
 while(digitalRead(dataReadyPin)); //Wait if the pin is low
 Serial.println(samplecounter,HEX);
 int now = millis()/1000;
 dataString += String(now,HEX)+" ";
 // transfer burst read command
 SPI.transfer(BURST_READ); // tell it to begin burst read
 SPI.transfer(0x00);
 delayMicroseconds(4);
 // Get SUPPLY_OUT reading
 data = SPI.transfer(0x00)<<8;  // read high byte
 data |= SPI.transfer(0x00);    // and add on low byte
 data &= 0b0000111111111111;    // make sure it is only 12 bits
 dataString += String(data,HEX);
 // Get gyro and accelerometer readings
 for( int ii=0 ; ii<6 ; ii++) {   // for every register to read
   data = SPI.transfer(0x00)<<8;  // read high byte
   data |= SPI.transfer(0x00);    // and add on low byte
   data &= 0b0011111111111111;    // make sure it is only 14 bits
   dataString += " ";
   dataString += String(data,HEX);
 }
 // Get temperature
 for( int ii=0 ; ii<3 ; ii++) {   // for every register to read
   data = SPI.transfer(0x00)<<8;  // read high byte
   data |= SPI.transfer(0x00);    // and add on low byte
   data &= 0b0000111111111111;    // make sure it is only 12 bits
   dataString += " ";
   dataString += String(data,HEX);
 }
 // Skip ADC data
 SPI.transfer(0x00);
 SPI.transfer(0x00);
 // take the chip select high to de-select:
 digitalWrite(ADIScs, HIGH);
 digitalWrite(SDcs,LOW);
 dataString += "\r\n";
 samplecounter++;
 counter++;
//  Serial.println(counter,DEC);
}


Sometimes things are as they appear

robtillaart


Try to strip the program to a minimal version that does show this same problem, and post it again.

Other issue that can be is that there is a shortcut somewhere on a shield that connects pin8 (SD card) to the reset pin somehow. Time for a visual inspection of the hardware ?

my 2 cents,
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up