Go Down

Topic: Bike interface OBD (Read 15475 times) previous topic - next topic

o5i_

#45
Jun 11, 2014, 09:51 pm Last Edit: Jun 11, 2014, 09:59 pm by o5i_ Reason: 1
I think i need to explain the problem.
If i use this code:
Code: [Select]

void setup() {
 Serial.begin(9600);    
 Serial1.begin(9600);
}
void loop() {
 if (Serial1.available()) {
   Serial.println(Serial1.read());
 }
}

i get:
Code: [Select]

103
217
123
229
103
217
123
229
103
217
123
229
103
217
123
235
103
217
123
0
103
217
123
0
103
217
123
235
103
217


But if i connect the wideband directly to the pc and printing values using Putty i get this:
Code: [Select]

14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8
14.8


If i do:
Code: [Select]
void setup() {
  Serial.begin(9600);   
  Serial1.begin(9600);
}
void loop() {
  if (Serial1.available()) {
    Serial.write(Serial1.read());
  }
}



i recieve this:
Code: [Select]
{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{åëgÙ{


Why putty shows me the right value but arduino does not??? Its just a serial connection... Is there a option for Serial read that can do that?

robtillaart

void setup()
{
 Serial.begin(9600);    
 Serial1.begin(9600);
}
void loop()
{
 if (Serial1.available())
 {
   Serial.print(Serial1.read(), DEC); //  print as decimal might help
 }
}
Rob Tillaart

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

o5i_

Hi, i tried, but its just the same...
I think something in reading function is wrong because if i do serial read  to serial write it should be the same as it shows if i connect it directly but it isnt.....

robtillaart

the reading function returns an int  (2 bytes) and the write() wants a byte as param
That is why I proposed to use print () iso write() as print() can print int's
Rob Tillaart

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

o5i_

dont know whats wrong... is it some ascii thing?

o5i_

#50
Jun 28, 2014, 07:38 pm Last Edit: Jun 28, 2014, 07:40 pm by o5i_ Reason: 1
Hi, im back from Holidays...
I added the cardslot to the board and tried if the sketch work but it didnt work, dont know why... If i try the sd example it works verry well and it is just the same... If i put everything in setup section it works but if i do the writing section to the loop it doesnt work...
I tested the circuit whit the z-diode but it is not the best choice, i think an opto-coupler work better...
http://fish.x64.me/doc/sdslogger01.ino

o5i_

#51
Dec 08, 2014, 02:03 pm Last Edit: Dec 08, 2014, 03:19 pm by o5i_
Hi, i redo the sketch, now its working, it writes to sd...
But i have a problem to write the byte i read as BIN to String on the SD... It just writes in DEC...
Sometimes if it writes to sd it slows down... Why? I need to buy faster sd or is it because a buffer overflows...



Code: [Select]
#define TX 14
#define console Serial
#define cbaud 115200
#define sds Serial3
#define sdsbaud 50000
#define LED 9
#include <SPI.h>
#include <SD.h>

int const debug = 1;
int const send_delay = 1;
int const fastinitwait = 3000;
int const fastinitdelay = 24;
int const recive_send_delay = 25;
int const timeout = 50;
int loging = 0;
const int chipSelect = 53;

String datarec;
byte const source = 0xF1;
byte const target = 0x12;
long const baud[2] = {
  50000, 10400};
byte const dataSequence[2][7] = {
  {0x81, target, source, 0x81, 0x05},
  {0x80, target, source, 0x02, 0x21, 0x08, 0xAE},
};
int const dataSize[2] = {
  5,7};

int datalenght;
int mode;
int startup;
int baudrate;
unsigned long time;
unsigned long lasttime = 0;
unsigned long cycletime = 0;
unsigned long time1000;
word milliseconds;
byte seconds;
byte minutes;
byte hours;
byte days;
byte inByte[99] = {
};

void setup() {
  pinMode(LED, OUTPUT);
  console.begin(cbaud);
  if (debug) console.print("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);   
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    if (debug) console.println("Card failed, or not present");
    return;
  }
  if (debug) console.println("card initialized.");
  loging = 1;
}

void loop() {
  fastinit();
  if (mode) {
    timestamp();
  }
  talkserial();
  createString();
  writetosd();
}

void fastinit(){
  if (!startup) {
    mode = 0;
    sds.end();                                                     
    pinMode(TX, OUTPUT);
    if (debug) console.println("Starting comunication (fastinit)");
    digitalWrite (TX, HIGH);                                       
    if (debug) console.println("Set K-Line High");
    delay(fastinitwait);                                                             
    digitalWrite (TX, LOW);                                                   
    if (debug) console.println("Set K-Line Low");
    delay(fastinitdelay);
    digitalWrite (TX, HIGH);                                                     
    if (debug) console.println("Set K-Line High");
    delay(fastinitdelay);   
    sds.begin(baud[baudrate]);                                                     
    if (debug) console.print("Starting Serial Comunication @ ");
    if (debug) console.println(baud[baudrate]);
    startup++;
  }
}

void talkserial() {
  datalenght = 0;
  int data = 0;

  time = millis();
  while (time < cycletime+recive_send_delay) {
    time = millis();
  };
  for (int x = 0;x < dataSize[mode];x++) {
    sds.write(dataSequence[mode][x]);
    delay (send_delay);
  }
  lasttime = millis();
  while (!data) {
    time = millis();
    if (time-lasttime > timeout) {
      startup = 0;
      baudrate++;
      if (baudrate >= 2) {
        baudrate = 0;
      }
      break;
    }
    if (sds.available()) {
      inByte[datalenght] = sds.read();
      //      if (inByte[datalenght] < 0x10) {
      //        if (debug)  console.print("0");
      //      }
      //      if (debug) console.print(inByte[datalenght],HEX);
      if (datalenght == dataSize[mode]+0x04+inByte[0x03+dataSize[mode]]) {
        cycletime = millis();
        if (!mode) mode++;
        break;
      }
      datalenght++;
    }
    digitalWrite(LED, HIGH);
  }
  digitalWrite(LED, LOW);
}


void timestamp() {
  time = millis();
  milliseconds = time-time1000;
  if (milliseconds >= 999) {
    time1000 = time;
    seconds++;
  }
  if (seconds >= 0x3F) {
    seconds = 0;
    minutes++;
  }
  if (minutes >= 0x3F) {
    minutes = 0;
    hours++;
  }
  if (hours >= 0x3F) {
    hours = 0;
    days++;
  }
  //  if (debug) {
  //    if (days < 0x10) {
  //      console.print("0");
  //    }
  //    console.print(days,HEX);
  //    if (hours < 0x10) {
  //      console.print("0");
  //    }
  //    console.print(hours,HEX);
  //    if (minutes < 0x10) {
  //      console.print("0");
  //    }
  //    console.print(minutes,HEX);
  //
  //    if (seconds < 0x10) {
  //      console.print("0");
  //    }
  //    console.print(seconds,HEX);
  //    if (milliseconds < 10) {
  //      console.print("0");
  //    }
  //    if (milliseconds < 100) {
  //      console.print("0");
  //    }
  //    if (milliseconds < 1000) {
  //      console.print("0");
  //    }
  //    console.print(milliseconds);
  //  }
}

void createString() {
  datarec = "";
  datarec += days,HEX;
  datarec += hours,HEX;
  datarec += minutes,HEX;
  datarec += seconds,HEX;
  datarec += milliseconds,HEX;
  for (int i = 0; i <= datalenght; i++) {
    datarec += inByte[i];
  }
  if (debug) console.println(datarec);
}


void writetosd() {
  if (loging) {
    File dataFile = SD.open("LOGFILE", FILE_WRITE);
    if (dataFile) {
      if (debug) console.println("writing to LOGFILE");
      dataFile.print(datarec);
      dataFile.close();
    }
    else {
      if (debug) console.println("error opening LOGFILE");
      loging = 0;
    }
  }
}

cyclegadget

 
 For saving to the SD card, I recommend using SDFFat. https://github.com/greiman/SdFat

The SD library you are using is the old one and I have heard it has problems.

I will have to have more time to look for other issues in your program.


Good work so far,
Mark
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

cyclegadget

#53
Dec 09, 2014, 12:36 pm Last Edit: Dec 09, 2014, 03:56 pm by cyclegadget
Post deleted. I miss read part of the code. Too early in the morning I guess. )
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

cyclegadget

 If you are having the most trouble during "debug" mode, it is probably because you are printing to the serial monitor a lot. If possible, I recommend using blinking leds for status, like one red blink per SD card write, one blue blink for a complete Obd2 message, and green for ready to start for example. Make sure to blink without delay of course. Then, review your SD card after your tests.



Your code has a lot of good things, keep it up!

Mark
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

o5i_

Hi.. thanx for the infos, i try the sd library first i can.. The code is verry simple, i think i can use functionpointers and some for to make it more complicated :D I need a lot of learning about that...
I switched back to the k5 ecu and 11400 baud now, on that i can adjust logging speed to about 135ms.. on the k7 ecu and 50000 baud are 55ms...

o5i_

#56
Dec 13, 2014, 12:08 am Last Edit: Dec 13, 2014, 12:09 am by o5i_
Hi, i updated the timestamp section which was wrong, now it works..
Code: [Select]
#define WB 0
#define TX 14
#define console Serial
#define c_baud 115200
#define sds Serial3
#define sds_baud 10400
#define LED 9
#include <SD.h>

// Konstant Values
boolean const debug = 1;
boolean const wideband = true;
int const send_delay = 1;
int const wait_before_ = 6000;
int const fast_init_delay = 25;
int const recive_send_delay = 43;
int const timeout = 150;
int const chipSelect = 53;
byte const dataSequence[2][7] = {
  {
    0x81, 0x12, 0xF1, 0x81, 0x05    }
  ,
  {
    0x80, 0x12, 0xF1, 0x02, 0x21, 0x08, 0xAE    }
  ,
};
int const dataSize[2] = {
  5,7};

// Dynamic
boolean loging = false;
boolean startup = false;
int mode = 0;
int datalenght = 0;
String datarec_csv = "";
String datarec_bin = "";
unsigned long lasttime = 0;
unsigned long cycletime = 0;
word milliseconds = 0;
byte seconds = 0;
byte minutes = 0;
byte hours = 0;
byte days = 0;
int afr = 0;
byte inByte[99] = {
};

void setup() {
  pinMode(LED, OUTPUT);
  console.begin(c_baud);
  if (debug >= 1) console.print("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);   
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    if (debug >= 1) console.println("Card failed, or not present");
    return;
  }
  if (debug >= 1) console.println("card initialized.");
  loging = true;
}

void loop() {
  fastinit();
  talkserial();
  if (mode) {
    read_wideband();
    timestamp();
    createString();
    writetosd();
    while (console.available()) {
      console.read();
      startup = false;
      mode = false;
    }
  }
}

void fastinit(){
  if (!startup) {
    mode = 0;
    milliseconds = 0;
    seconds = 0;
    minutes = 0;
    hours = 0;
    days = 0;
    afr = 0;
    while(sds.available()) {
      sds.read();
    }
    sds.end();
    pinMode(TX, OUTPUT);
    if (debug >= 1) console.println("Starting comunication (fastinit)");
    digitalWrite (TX, HIGH);                                       
    if (debug >= 1) console.println("Set K-Line High");
    delay(wait_before_);                                                             
    digitalWrite (TX, LOW);                                                   
    if (debug >= 1) console.println("Set K-Line Low");
    delay(fast_init_delay);
    digitalWrite (TX, HIGH);                                                     
    if (debug >= 1) console.println("Set K-Line High");
    delay(fast_init_delay);   
    sds.begin(sds_baud);                                                     
    if (debug >= 1) console.print("Starting Serial Comunication @ ");
    if (debug >= 1) console.println(sds_baud);
    startup++;
  }
}

void talkserial() {
  datalenght = 0;
  unsigned long time = millis();
  unsigned long timediff = millis();

  while (time < cycletime+recive_send_delay) {
    if (debug >= 2) console.println("waiting");
    time = millis();
  }
  if (debug >= 2) console.println("sending some bytes...");
  for (int x = 0;x < dataSize[mode];x++) {
    sds.write(dataSequence[mode][x]);
    if (debug >= 2) {
      if (dataSequence[mode][x] < 0x10) {
        console.print("0");
      }
      console.print(dataSequence[mode][x],HEX);
    }
    delay (send_delay);
  }
  if (debug >= 2) console.println("recieving some bytes...");
  while (startup) {
    time = millis();
    if (time-timediff > timeout) {
      startup = 0;
      mode = 0;
      break;
    }
    if (sds.available()) {
      inByte[datalenght] = sds.read();
      if (debug >= 2) {
        if (inByte[datalenght] < 0x10) {
          console.print("0");
        }
        console.print(inByte[datalenght],HEX);
        console.print(",");
      }
      if (datalenght == dataSize[mode]+0x04+inByte[0x03+dataSize[mode]]) {
        cycletime = millis();
        if (!mode) mode++;
        break;
      }
      datalenght++;
    }
    digitalWrite(LED, HIGH);
  }
  digitalWrite(LED, LOW);
}

void read_wideband() {
  if (debug >= 2) console.println("reading analog Inputs");
  afr = analogRead(WB);
  if (debug >= 2) console.println(afr,HEX);
}

void timestamp() {
  unsigned long time = millis();
  if (!lasttime) lasttime = millis();
  unsigned long timediff = time - lasttime;
  lasttime = millis();
  milliseconds = milliseconds + timediff;
  if (milliseconds > 999) {
    int multi = milliseconds/1000;
    milliseconds =  milliseconds - multi*1000;
    seconds++;
  }
  if (seconds >= 0x3C) {
    seconds = 0;
    minutes++;
  }
  if (minutes >= 0x3C) {
    minutes = 0;
    hours++;
  }
  if (hours >= 0x3C) {
    hours = 0;
    days++;
  }
  if (debug >= 2) {
    console.println("create Time String");
    if (days < 0x10) {
      console.print("0");
    }
    console.print(days,HEX);
    console.print(":");
    if (hours < 0x10) {
      console.print("0");
    }
    console.print(hours,HEX);
    console.print(":");
    if (minutes < 0x10) {
      console.print("0");
    }
    console.print(minutes,HEX);
    console.print(":");
    if (seconds < 0x10) {
      console.print("0");
    }
    console.print(seconds,HEX);
    console.print(":");
    if (milliseconds < 10) {
      console.print("0");
    }
    if (milliseconds < 100) {
      console.print("0");
    }
    if (milliseconds < 1000) {
      console.print("0");
    }
    console.println(milliseconds);
  }
}



void createString() {
  if (debug >= 2) console.println("Create Datastring");
  datarec_csv = "";
  datarec_bin = "";

  if (days < 10) {
    datarec_csv += 0;
  }
  datarec_csv += days;
  datarec_csv += ":";
  if (hours < 10) {
    datarec_csv += 0;
  }
  datarec_csv += hours;
  datarec_csv += ":";
  if (minutes < 10) {
    datarec_csv += 0;
  }
  datarec_csv += minutes;
  datarec_csv += ":";
  if (seconds < 10) {
    datarec_csv += 0;
  }
  datarec_csv += seconds;
  datarec_csv += ":";
  if (milliseconds < 10) {
    datarec_csv += 0;
  }
  if (milliseconds < 100) {
    datarec_csv += 0;
  }
  datarec_csv += milliseconds;
  datarec_csv += ",";

  if (afr < 10) {
    datarec_csv += 0;
  }
  if (afr < 100) {
    datarec_csv += 0;
  }
  if (afr < 1000) {
    datarec_csv += 0;
  }
  datarec_csv += afr;  //AIR FUEL RATIO - From analog Pin 0
  datarec_csv += ",";
  /*
 
   /// remember HEX to DEC conversion forex. 0x14 = 20 starting from 0!!!!
   
   datarec_csv += inByte[dataSize[mode]+0x20];  //TPS
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x21];  //IAP
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x22];  //ECT
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x23];  //IAT
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x26];  //GEAR
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x47];  //STP
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x53];  //CLUTCH
   datarec_csv += ",";
   
   datarec_csv += inByte[dataSize[mode]+0x54];  //NEUTRAL
   datarec_csv += ",";
   */
  datarec_bin += days;
  datarec_bin += hours;
  datarec_bin += minutes;
  datarec_bin += seconds;
  datarec_bin += milliseconds;

  for (int i = 0; i <= datalenght; i++) {
    datarec_bin += inByte[i];
  }
  for (int i = 0; i <= datalenght; i++) {
    datarec_csv += inByte[i];
    datarec_csv += ",";
  }
  if (debug == 1) {
    console.println(datarec_csv);
 //   console.println(datarec_bin);
  }
}

void writetosd() {
  if (loging) {
    if (debug >= 2) console.println("Start opening SD");
    File dataFile = SD.open("LOG.csv", FILE_WRITE);
    if (dataFile) {
      if (debug >= 1) console.println("Writing to LOG.csv");
      dataFile.println(datarec_csv);
      dataFile.close();
    }
    else {
      if (debug >= 1) console.println("error opening LOG.csv");
      loging = false;
    }
  }
}

But i have a question..
How can i create a sting whit hex values and print than on file as binary or maybee print that directly to bin whitout converting it to string? for ex..i have 0x00 0x00 0x01 0x01 0x01 0x02 0x02 0x80 0x12 0xF1... i want to print as bin to logfile and create a string like 0:0:1:1:122,80,12,F1...

cyclegadget

Your question about saving your results was a little confusing to me but, I will tell you what I am doing.
I receive the data and save it as a byte in an array using this snippet of code.
Code: [Select]
byte inByte = Serial1.read();
      buf[counter] = inByte;


 Then, I save to my SD card the variable that I want. They will always be in the same positions in the string that comes from the ECU. I shared those positions with you months ago when you started this project. When I look at my SD card I find decimal numbers that are easily readable.

I was looking over your code that you shared a few days ago, and I have a great time stamp function that I use for the SD card. At the moment, I am using a buffer for storing the values. Using SDFAT, I use....  sprintf(buffer, "%02d:%02d:%02d.%03d", h, m, s, ms);  to buffer time string.

Code: [Select]
void displayResult()
{
  int h,s,m,ms;              //hour, second, minute, milliseconds
  unsigned long over;
  elapsed = finished-start;
  h = int(elapsed/3600000);  //calculate the hour
  over = elapsed%3600000;
  m = int(over/60000);       //calculate the minute
  over = over%60000;
  s = int(over/1000);       //calculate the second
  ms = over%1000;           //calculate the millisecond

  char buffer[14];
  sprintf(buffer, "%02d:%02d:%02d.%03d", h, m, s, ms);  //buffer time string
  //file.print(buffer);        //print the time
  // log data and flush to SD
  logfile << buffer << flush;
  //Serial.println(" the header" );
}



Here is how I make my header for the SD file the first time that I save to the card.

Code: [Select]
void SDHEADER(){
 
  // use buffer stream to format line

  obufstream bout(SDbuf, sizeof(SDbuf));
  //  format data for sending to sd card

  bout << ("Log Time") << ',' << ("RPM") << ',' << ("TPS") << ',' << ("IAP") << ',' << ("HO2") << ',' << ("HO2 Wideband") << ',' << ("IGN") << ',' << ("STP")
    << ',' << ("GEAR") << ',' << ("CLUTCH") << ',' << ("NT") << ',' << ("BOOST") << ',' << ("IP") << ',' << ("AP") << ',' << ("CLT") << ',' << ("IAT")
      << ',' << ("BATT") << ',' << ("PAIR") << ',' << ("FUEL1") << ',' << ("FUEL2") << ',' << ("FUEL3") << ',' << ("FUEL4") << ',' << ("HOX_ON") << ',' << ("MTS AFR")
        << ',' << ("New Data Rate %");
  bout << endl;

  // log data and flush to SD
  logfile << SDbuf << flush;
  }


 Here is how I save my variables to the SD card. Notice that I am writing a CSV file so, I have "," commas saved between each variable saved.
Code: [Select]
void SDDATASAVES(){
  // use buffer stream to format line
  obufstream bout(SDbuf, sizeof(SDbuf));

  displayResult(); // save the timestamp first
  //  format data for sending to sd card
  bout  << ',' << (Rpm) << ',' << (tps) << ',' << (Iap) << ',' << (Ho2) << ',' << (HO2_Wideband)<< ',' << (ign) << ',' << (Stp) << ',' << (Gear)
    << ',' << (szClutch) << ',' << (szNeutral) << ',' << (Boost) << ',' << (Ip) << ',' << (Ap) << ',' << (Clt) << ',' << (Iat) << ',' << (Battery) << ','
      << (Pair) << ',' << (Fuel1) << ',' << (Fuel2) << ',' << (Fuel3) << ',' << (Fuel4) << ',' << (Hox_On) << ',' << (Mts_Afr) << ','
      << (New_Data_Rate);
  bout << endl;

  // log data and flush to SD
  logfile << SDbuf << flush;
  }

 
 I also wanted to let you know that this bit of your code is a complicated way of saying delay();, because nothing can happen during this time except this while loop.
Code: [Select]
while (time < cycletime+recive_send_delay) {
    if (debug >= 2) console.println("waiting");
    time = millis();
  }
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

cyclegadget


 These libraries and code pieces have to be in the begging of the code.

Code: [Select]
#include <stdio.h>;  // this is for the timestamp counter
#include <SdFat.h>
// file system
// file system object
SdFat sd;

// text file for logging
ofstream logfile;
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))

// buffer to format data - makes it eaiser to echo to Serial
char SDbuf[512];
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

cyclegadget

#59
Dec 14, 2014, 01:21 am Last Edit: Dec 14, 2014, 01:22 am by cyclegadget
 I wrote a sketch to help you understand how to send Serial and read Serial without blocking. In the sketch I use a "static int" to help me send "HELLO WORLD" one character at a time slowly. The LED 13 blinks at a separate rate during the whole sketch. Then, the user response with < 30 characters followed by "." and the Arduino will repeat the response. The Arduino then begins the process again, by saying HELLO WORLD again.

Code: [Select]
char request[11] = {
  'H','E','L','L','O',' ','W','O','R','L','D'};
char buf[30];  // buffer for received characters
int index = 0; // counter for received characters in buf

unsigned long currentMillis = 0;         // to save current millis()
unsigned long previousMillis = 0;        // will store delay time for EACH char of HELLO
long interval = 700;             // interval at which to transmitt
unsigned long callpause = 45;    // interval at which to transmitt
byte mode = 1;  //variable to shift between operations

//////////////////////////LED VARIABLES///////////////////////
int ledState = LOW;                // ledState used to set the LED
unsigned long blinkrate = 350;     // interval of blinks in millis
unsigned long previousblink = 0;   //last blink time
int ledPin = 13;                   // Uno onboard LED

void setup()
{
  pinMode(ledPin,OUTPUT);        //LAST SET TO 13
  digitalWrite(ledPin,LOW);
  Serial.begin(115200);
  delay(2000);            //Allow the user to get the serial monitor open
  Serial.println("The Arduino is going to say HELLO WORLD one Character at a time with 700 Millis between Characters.");
  Serial.println("Respond with less that 30 characters followed with a . period.");
  Serial.println("The Arduino will repeat what you have typed before saying HELLO again");
  Serial.println("LED 13 will blink at a rate of 350 Millis without being affected by other operations.");
}

void loop()
{
  ///////////////////////////////////////mark the time in millis//////////////////////
  currentMillis = millis();
  /////////////////////////////////////call heartbeat to blink LED13///////////////////////////////////////
  if(currentMillis - previousblink > blinkrate)
  {
    heartbeat();
    previousblink = currentMillis;
  }

  /////////////////////////////////////////HELLO MODE//////////////////////////
  if (mode == 1 && (currentMillis - previousMillis) >= interval)  //delay between transmits 700 Millis
  {

    previousMillis = currentMillis;
    //send "HELLO WORLD" one character at a time with out loosing place
    static int i = 0;
    Serial.print(request[i]);
    i++;

    if (i == 11) // if we sent the 11 Character HELLO WORLD lets move on
    {
      mode = 2;
      i = 0; // reset our static int i to 0 for use when we want to say HELLO again.
      Serial.println();
      Serial.print(" Respond to me with < 30 Characters followed by a .");
      Serial.println();
    }
  }


  /////////////////////READ A RESPONSE ENDING WITH A PERIOD/////////////////////
   //check the variable mode to see if it is time to check for a response
  if (Serial.available()&& mode == 2) {
    char inByte = Serial.read();   // read from Serial and store the characters to inByte
    buf[index] = inByte;
    index++;

    if(inByte == '.')
    {
      for (int i = 0; i < (index - 1); i++){ // print the respone you made
        Serial.print(buf[i]);
      }
      Serial.println();
      mode = 1;           //return to printing HELLO
      index = 0;          //reset buf index
    }
  }
}


/////////////////HEARTBEAT///////////////
void heartbeat()
{
  if (ledState == LOW)
    ledState = HIGH;
  else
    ledState = LOW;
  digitalWrite(ledPin, ledState);
}
















This sketch is performs in a similar manner at which your sketch needs to work.
You want to:
1. Send a request
2. Listen for a response
3. Save the data
4. blink LEDs, check buttons and update screens.
All of this without blocking. It is awesome when you get it to work!

Try the sketch, I had fun writing it.
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

Go Up