Hello everyone. So I'm stuck on a little string/char problem. Essentially I have a Mettler toledo scale which, works on a total of at least 17 characters to transmit some data. (Seen in one of the pictures) Now I'm communicating with the scale and I'm recording all this data to my SD card. All this data I get is going to excel which, is what I want. My problem is, it will transmit all of these decimal readings of the Ascii code in separate cells because I'm not sure how to put them all together. So essentially where the it starts with the ID block and ends with the CR, LF, I want to make it all one reading so it's in one excel block instead of 17 separate ones where I need to go one by one to make sure it's correct. This excel sheet can be seen in the other picture. I highlighted the 17 or so excel blocks which, essentially add to one reading. I would like to know is there anyway I can string these together? So can my code read the ID block and stop at the CR,LF and then convert that to the number (mass) that I wish to see? I'm not sure if this is confusing but if you look at the pictures and the code, could someone give me any ideas?
Thanks so much!
#include <ctype.h>
#include <SPI.h>
#include <SD.h>
File myFile;
#define bit110Delay 9091
#define halfBit110Delay 4545
byte rx = 6;
byte tx = 7;
byte SWval;
void setup() {
Serial.begin(110);
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
return;
}
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
digitalWrite(tx, HIGH);
delay(2);
digitalWrite(13, HIGH); //turn on debugging LED
SWprint('S');
SWprint('I'); //debugging hello
SWprint(13);
SWprint(10); //carriage return
}
void SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx, LOW);
delayMicroseconds(bit110Delay);
for (mask = 0x01; mask > 0; mask <<= 1) {
if (data & mask) { // choose bit
digitalWrite(tx, HIGH); // send 1
}
else {
digitalWrite(tx, LOW); // send 0
}
delayMicroseconds(bit110Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit110Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit110Delay);
for (int offset = 0; offset < 7; offset++) {
delayMicroseconds(bit110Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit110Delay);
delayMicroseconds(bit110Delay);
return val;
}
}
void loop()
{
myFile = SD.open("LOGGER00.csv", FILE_WRITE);
if (myFile) {
SWval = SWread();
myFile.print(SWval);
myFile.print(",");
myFile.println(millis());
myFile.close();
}
}
is going to spin through characters because of the trailing semicolon. I don't think you want that there.
It appears that your message block is a max of 20 characters and is terminated with a line feed ('\n') character. What happens if you modify your read routine to something like:
char dataInput[20];
int index;
while (digitialRead(x)) {
dataInput[index] = digitalRead(x);
if (dataInput[index] == '\n') {
dataInput[index - 1] = '\0'; // Overwrite the CR with a null, throw
break; // away the '\n', making it a string
}
index++;
}
// use strtok() to break out the fields you want from the string
At some point before the data is sent to Excell, it needs to be converted to comma delimited format. In other words, each value needs to have the CR/LF replaced by a ",", comma. Then when you have the number of cells you want for the Excel line, keep the CR/LF and Excel will begin a new line.
Hey everyone. Thanks for your feedback but this is what I have so far, I can get the excel file to have three columns. One is my weight/piece count, one is the character term (what I wanted), and third is my millisecond. (Picture attached of excel sheet)
#include <ctype.h>
#include <SPI.h>
#include <SD.h>
File myFile;
#define bit110Delay 9091
#define halfBit110Delay 4545
byte rx = 6;
byte tx = 7;
byte SWval;
void setup() {
Serial.begin(110);
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
return;
}
pinMode(rx, INPUT);
pinMode(tx, OUTPUT);
digitalWrite(tx, HIGH);
delay(2);
digitalWrite(13, HIGH); //turn on debugging LED
SWprint('S');
SWprint('I'); //debugging hello
SWprint(13);
SWprint(10); //carriage return
}
void SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx, LOW);
delayMicroseconds(bit110Delay);
for (mask = 0x01; mask > 0; mask <<= 1) {
if (data & mask) { // choose bit
digitalWrite(tx, HIGH); // send 1
}
else {
digitalWrite(tx, LOW); // send 0
}
delayMicroseconds(bit110Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit110Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit110Delay);
for (int offset = 0; offset < 7; offset++) {
delayMicroseconds(bit110Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit110Delay);
delayMicroseconds(bit110Delay);
return val;
}
}
void loop()
{
myFile = SD.open("LOGGER00.csv", FILE_WRITE);
if (myFile) {
SWval = SWread();
myFile.print(SWval);
myFile.print(",");
myFile.print(char(SWval));
myFile.print(",");
myFile.println(millis());
myFile.close();
}
}
This is the new code, now my only thing is to string everything together so I only get one reading out, not 17 characters total. Those 17 characters are mostly spaces (32 represents a space) and I'm only interested in the actual numbers that come out.
and is going to sit there and throw away data until there is no more data to read. Is that what you really want to do? If it is, then use the style shown in the second code fragment so we know you meant to write it that way.