I have an Arduino Uno and an Adafruit data logging shield and I have connected a digital pressure sensor that uses serial communication. The sensor requires a character to start transmitting data. I have managed to read the data using this code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8,9);
int depth;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
mySerial.print("C");
}
void loop()
{
if (mySerial.available()) {
depth = mySerial.read();
}
}
This returns the following:
C1000
C1000
C1000
where 1000 is the value.
I have written another code that logs data to the shield and tested with other sensors but I cannot log data from this sensor by modifying the above code and cannot find any relevant examples. If I add something like this it does not return the same value.
logfile.println(depth);
Complete newbie in so any suggestions are welcome.
UPDATE
-
Comments are not for extended discussion; this conversation has been moved to chat.VE7JRO– VE7JRO2019年07月05日 16:53:58 +00:00Commented Jul 5, 2019 at 16:53
1 Answer 1
use the C as the line start indicator. if received char is 'C' then do a file.println();
example with output to Serial
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8,9);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
mySerial.print("C");
}
void loop() {
if (mySerial.available()) {
char c = mySerial.read();
if (c == 'C') {
Serial.println();
}
Serial.write(c);
}
}
-
But this code does not do anything different than the simple code. My problem is not that I am not reading the data or that I do not see the data printed in the serial monitor. I do receive all the data. The problem comes when I try to log the data. I think the data come in so fast from the serial, but when I add delays everything is messed up.Nik_ol– Nik_ol2019年07月05日 10:08:50 +00:00Commented Jul 5, 2019 at 10:08
-
it prints completely different then the code in Question. did you try it?2019年07月05日 10:25:48 +00:00Commented Jul 5, 2019 at 10:25
-
Yes I tried it. It prints the same with a gap between its measurement. Also it uses a lot more of the dynamic variable. But it prints the same with a gap between each C1000Nik_ol– Nik_ol2019年07月05日 11:44:05 +00:00Commented Jul 5, 2019 at 11:44
-
is it not what you want? every set on a new line?2019年07月05日 11:59:10 +00:00Commented Jul 5, 2019 at 11:59
-
No. I want to log the data to an SD card. When I use this code to log I get one number every second. I have added a screen shot in the original post. I want to get all the 4 numbers in one second and then the new 4 numbers in the next second.Nik_ol– Nik_ol2019年07月05日 12:25:20 +00:00Commented Jul 5, 2019 at 12:25