Arduino UNO, CHR-6d IMU and SD-card

Hello,

for my application I use

  • Arduino Uno
  • Ethernet Shield
  • CHR 6-d IMU (Pololu company)
  • SD card

I have no problem getting data from sensor to Serial monitor.
Problem occurs when i try to log data on SD card. When I add code for SD card I only get packets 73 60 70 on Serial monitor instead of whole packets.

My code is :

byte c;
#include <SD.h>
#include <SoftwareSerial.h>
const int chipSelect = 4;

SoftwareSerial mySerial(6, 7); // RX, TX
uint8_t SET_SILENT_MODE[] = {
  's', 'n', 'p', 0x83,0x00, 0x01, 0xD4};
uint8_t GET_DATA[] = {
  's', 'n', 'p', 0x01, 0x00, 0x01, 0x52};
uint8_t SET_BROADCAST_MODE[] = {
  's','n','p',0x84,0x01,0x01,0x01,0xD7};
uint8_t SET_ACTIVE_CHANNELS[] = {
  's','n','p',0x82,0x01,0x07,0x01,0xDB};
void setup() {
  Serial.begin(115200);
  mySerial.begin(115200);
  pinMode(4, OUTPUT);
  digitalWrite(10, HIGH);
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  File dataFile = SD.open("meritve3.txt", FILE_WRITE);
  mySerial.write(SET_BROADCAST_MODE,sizeof(SET_BROADCAST_MODE));
}

void loop() {
  byte c=0;
  if (mySerial.available()>0){
    c = mySerial.read();
    Serial.print(c, HEX);
    Serial.print("\n");
    SD_write();
  }
}
void SD_write(){
   File dataFile = SD.open("meritve.txt", FILE_WRITE);
   dataFile.write(c);
   dataFile.close();
}

I am grateful in advance for any help.

Best regards,

  mySerial.begin(115200);

SoftwareSerial can have problems at that baud rate.

    Serial.println("Card failed, or not present");

You should be using the F() macro:
Serial.println(F("Card failed, or not present"));
to keep the string literal out of SRAM.

If adding the SD card stops the program from working, because it uses more than 1/4 of the SRAM on a 328-based machine, it is almost certainly because you have run out of memory.

It isn't clear what the SoftwareSerial instance is talking to. It is unlikely that you have a device called a mySerial attached to the Arduino. Making the instance name match the device would go a long way towards clearing up the confusion.

It isn't clear what you are using the Ethernet shield for. Just for the SD card is what it appears.

Thanks for fast response :slight_smile: I will answer to your message, I hope I will be clear enough.

  1. mySerial baud rate (115200)

It is set so because CHR-6d communicates at that level. I use SoftwareSerial because because i have external hardware(CHR)
and when I delete code concerning SD card it works perfectly(I receive full data packets from sensor)

When I add code for SD I receive only first three bytes from sensor (73 6E 70). I will correct what u wrote and rename mySerial to CHR.

  1. Memory

If I run out of memory, how can I know that and can you help me a little with fixing that?

  1. SD

Yes, I use Ethernet Shield just for SD card

Thanks again,

If I run out of memory, how can I know that

http://playground.arduino.cc/Code/AvailableMemory

and can you help me a little with fixing that?

The F() macro is one way.

const int chipSelect = 4;

Minimizing data types is another. Pin numbers don't really need to be ints, until there is an Arduino with more than 255 of them.

  pinMode(4, OUTPUT);
  digitalWrite(10, HIGH);

Pin 10 needs to be set to output, too. You should not be diddling with the SD card's chip select pin, nor should you be setting the state of pin 10.

    Serial.print(c, HEX);
    Serial.print("\n");

or

    Serial.println(c, HEX);

Opening the file, writing one character, and closing the file is pretty inefficient. Collect a complete packet, and then write to the file.

thank you. When I come home I will fix all the thing and then reply back

Big thanks!

best regards

Hi again,

i corrected my code, and now I don't use SD until I will fix other problems :slight_smile:

#include <SoftwareSerial.h>
SoftwareSerial CHR(6,7); // RX, TX
uint8_t SET_BROADCAST_MODE[] = {'s','n','p',0x84,0x01,0x49,0x02,0x1F};
uint8_t SET_ACTIVE_CHANNELS[] = {'s','n','p',0x82,0x01,0x3F,0x02,0x13};
void setup() {
  Serial.begin(115200);
  CHR.begin(115200);
  CHR.write(SET_BROADCAST_MODE,sizeof(SET_BROADCAST_MODE));
  delay(10);
  CHR.write(SET_ACTIVE_CHANNELS,sizeof(SET_ACTIVE_CHANNELS));
  delay(10);
}
void loop() {
  delay(10);
startofRead:
  if(CHR.available()>0){
    int i=0;
    byte buffer[20]={0};
    buffer[i]=CHR.read(); //1
    if(buffer[i] == 0x73){
      i++;
      buffer[i]=CHR.read(); //2
    }
    else {
      goto startofRead;
    }
    if(buffer[i] == 0x6E){
      i++;
      buffer[i]=CHR.read(); //3
    }
    else {
      goto startofRead;
    }
    if(buffer[i] == 0x70){
      i++;
      buffer[i]=CHR.read(); //4
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
      i++;
      buffer[i]=CHR.read();
    }else {goto startofRead;}
      for(int k=0; k<20;k++)
  {
    Serial.print(buffer[k], HEX);
    Serial.print(" "); 
  }
  Serial.println();
  }
}

Result is

73 6E 70 B7 D 3F E1 1D F8 CC 7C FC 7D F7 2A 16 57 9 7E 6E 
73 6E 70 B7 D 3F F8 96 F8 EF 78 FF FD BC 30 16 59 A CD FF 
73 6E 70 B7 D 3F E1 27 E1 AA F8 FF FA C0 FF 32 86 59 A 1B 
73 6E 70 B7 C3 16 20 E1 B0 E1 F2 F5 C1 FF 66 B1 4D 4C FC FF 
73 6E 70 B7 D 3F F8 64 7C 78 7C FA 7D 78 34 16 AE A 21 FF 
73 6E 70 B7 D 3F E1 28 E1 BB F8 5F C1 FF 35 16 5C A 25 FF 
73 6E 70 B7 C3 16 26 E1 AA E1 FE F5 C4 FF CC 8B AE A 90 FF 
73 6E 70 B7 D 3F E1 28 F8 74 7C C1 FD E4 FF 31 8B 5E 89 28 
73 6E 70 B7 C3 16 21 E1 A9 E2 A F5 B8 FF 98 96 5E 9 1C FF 
73 6E 70 B7 D 3F F8 8F 7C 74 3C 60 7D BB 2E 16 6B 61 C1 FF 
73 6E 70 B7 D 3F E1 2A E1 8F F8 7F 9F F7 30 16 5C 9 FF FF 
73 6E 70 B7 C3 16 2C E1 98 E2 9 F5 BA FF 99 16 62 9 1D FF 
73 6E 70 B7 D 3F E1 23 F8 75 7C 60 DF BB 34 16 60 9 8D FF 
73 6E 70 B7 C3 16 21 E1 AB E1 FA F5 BB FF 9C 16 5B A 15 FF 
73 6E 70 B7 D 3F E1 23 F8 BA 3E F5 BA FF 33 8B 5E A 12 FF 
73 6E 70 B7 D 3F E1 26 E1 A6 F1 80 F5 BC FF 2C 16 5D 9 13 
73 6E 70 B7 C3 16 22 E1 B3 E1 FE F5 C0 FF CC 8B 4D 8C F9 FF 
73 6E 70 B7 D 3F E1 1D F8 DB 7C FE 7D F7 3E 16 5B A 24 FF 
73 6E 70 B7 C3 16 23 E1 B7 E1 F5 F5 BB FF 9D 16 56 A 1B FF

those packets where 5th byte is C3 are wrong ones. And I don't have any idea why.

Can anyone help on that?

Best regards

Can anyone help on that?

Sure. Start by finding all the goto statements. Delete them. Delete the label, too.

Rewrite the code in C. Do NOT try to force C to look like BASIC.

30+ years of writing C and C++ code, and I've never needed to use a goto statement. You don't, either.