Dear Sir,
I am testing my NDIR C02 witn UNO R3. When I test the sensor by using analog input of UNO R3, i can get the readings but when I tried to use UART / serial it seem does not work.
The specification of the NDIR C02 as follows:
Inputs and outputs
- Power supply DC5V+/-5%
- Working current 70mA,max 120mA
- Measuring output linearity output
- Output voltage 0.8-4VDC (can be customized)
- Digital output UART Data bit:8; Stop bit:1; Check bit :null
- Standard baud rate 9600bps
The sketch is as follows:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); // RX, TX mySerial(10, 11)
int incomingByte;
int c;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("Goodnight moon!");
// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (Serial.available())
Serial.println("Serial Is Available");
if (mySerial.available()>0)
{
//Serial.write(mySerial.read());
Serial.println("MYSERIAL Is LONG");
incomingByte = mySerial.read();
Serial.println(incomingByte);
}
else
{
Serial.println("MYSERIAL Is ZERO");
incomingByte = mySerial.read();
Serial.println(incomingByte);
}
if (mySerial.isListening()) {
Serial.println("MYSERIAL is listening!");
}
else
{
Serial.println ("MYSERIAL is NOT listening");
}
c = mySerial.read();
Serial.print("c:");
Serial.println (c);
if (mySerial.overflow()) {
Serial.println("SoftwareSerial overflow!");
}
else
{
Serial.println("SoftwareSerial DOES NOT overflow!");
}
//mySerial.write(Serial.read());
delay(3000);
}
The result display on the monitor as follows:
Goodnight moon!
----------first loop ----
MYSERIAL Is ZERO
-1
MYSERIAL is listening!
c:-1
SoftwareSerial DOES NOT overflow!
-------second loop-----
MYSERIAL Is LONG
0
MYSERIAL is listening!
c:-1
SoftwareSerial DOES NOT overflow!
-------- subsequent loops-----
MYSERIAL Is ZERO
-1
MYSERIAL is listening!
c:-1
SoftwareSerial DOES NOT overflow!
My questions are
- Why nothing happens to mySerial.println("Hello, world?");
- Why nothing happens to Serial.println("Serial is Available) in the loop()
- mySerial.available()>0 only hapens once, that is for the second loops, why not for the entire loop?
- How to get the readings from the sensor by using the SofrwareSerial?