I have the following configuration;
Uno with xbee shield hooked up to a gas sensor (Dräger 0-20 ma) via an ads1115.
Another xbee is connected to a pc via an usb xbee adapter.
When i use Serial.print(waarde) the values show up on the pc.
When i use Serial.println(waarde) nothing shows up.
I can't figure out what the problem is.
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <Time.h>
Adafruit_ADS1115 ads;
const int numReadings = 10;
int readings[numReadings];
int index = 0;
float total = 0;
float average = 0;
float waarde2;
String unitId = "Desin2"; // desinfectie sensor 2 hoog
long waarde;
unsigned long previousMillis = 0;
const long interval = 4000;
long calibratie = 5333; // nulpunt sensor, range = 5
String inputString = ""; // inkomende data
boolean stringComplete = false; // data compleet
boolean ZendPPM = true;
void setup() {
Serial.begin(9600);
inputString.reserve(200);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
ads.begin();
ads.setGain(GAIN_TWOTHIRDS); // gain one = 6.144 V
}
void loop() {
concentratie();
communicatie();
tijd();
}
void communicatie()
{
if (stringComplete) {
if (inputString.substring(0, 4) == "ZeId") {
Serial.println(unitId);
}
if (inputString.substring(0, 4) == "StDc") { // verstuur geen ppm
ZendPPM = false;
Serial.println("StDcOk");
}
if (inputString.substring(0, 4) == "ZeDc") { //verstuur ppm
ZendPPM = true;
Serial.println("ZeDcOk");
}
if (inputString.substring(0, 4) == "NulC") {
String NulPunt = (inputString.substring(4, 8));
long Tussenstap = NulPunt.toInt();
calibratie = Tussenstap;
}
inputString = "";
stringComplete = false;
}
}
void concentratie()
{
int16_t adc0, adc1, adc2, adc3;
adc0 = ads.readADC_SingleEnded(0);
total = total - readings[index];
readings[index] = adc0;
total = total + readings[index];
index = index + 1;
if (index >= numReadings) {
index = 0;
}
average = total / numReadings;
waarde = map(average, calibratie, 26665, 0, 50);
delay(1);
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}
void tijd() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ZendPPM == true) {
Serial.print("DataDc");
Serial.println(waarde);
}
}
}
Sorry, cannot give any more advice that what I have done. I am not surprised that you are having problems because of all the "String" manipulation that is being done. Perhaps that is resulting in program code corruption. Can you reprogram to use "string" instead of "String"? A "string" will be NULL terminated instead of CRLF, and will not be using the Arduino stack, etc. behind you back.
Paul
Your serialEvent () function only checks for the presence of the '\n' byte. Check the commands you are sending from the port monitor of PC (Line Ending Parameter). They can contain '\n', '\n\r', or nothing at all. If you send, for example, a string with the line ending Both NL & CR, in the input buffer of your Arduino Serial a '\r' is left from the previous command, you can get a similar effect.
In such case I use Only New Line setting, or I also check for presence of '\ r' in the input buffer of Serial and discard it.