I uploaded the following to my UNO with WiFi:
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs; // The air quality sensor
String Dataline = "";
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("SET UP");
}
void loop() {
// wait a second between measurements.
delay(1000);
Dataline = "";
Dataline = Dataline + getTime();
Dataline = Dataline + AIRSensor();
Dataline = Dataline + "...";
Serial.println(Dataline);
}
String getTime() {
String OUT = "Tim::";
OUT += String(millis());
return OUT;
}
String AIRSensor() {
if (ccs.available()) {
if (!ccs.readData()) {
float temp = ccs.calculateTemperature();
String CO2 = (String)ccs.geteCO2();
return "CO2::" + CO2 +"TAQ::" + (String)temp;
}
} else {
return "CO2::-999,TAQ::-999";
}
}
The output is simply "SET UP" repeated:
SET UP
SET UP
SET UP
...
It never enters the loop or prints anything else out. Does this mean the UNO is resetting? How do I troubleshoot?
1 Answer 1
You can tell that something is going wrong between your println
. Some advice in this case: eliminate the String
entirely and replace the println
with a series of print
. This would let you narrow down which function is causing the problem. It also can save some memory, which is a nice bonus in embedded programming.
I suspect in this case the problem is your use of C-style casts to String
in AIRSensor()
. (String)whatever
and String(whatever)
do very different things, and the former is not something you want here, or something you should be using very often in C++ code.
The code in loop
is running, but it is crashing and causing the hardware to reset. Since it is crashing before it reaches the first visible behavior in loop
, it just looks like setup
is running over and over.
-
Thanks! Changing from '(String)' to 'String()' made a massive difference. It now runs for about 6 loops, and then stops for a while. In the IDE, I get a 'Error while setting serial port parameters: 115,200 N 8 1' message. It then restarts again. Is there any way to stop that?user1505631– user15056312022年07月21日 23:42:36 +00:00Commented Jul 21, 2022 at 23:42
-
'twas an issue with me not using 'ccs.begin()'. Thank you for your help!!user1505631– user15056312022年07月22日 01:28:55 +00:00Commented Jul 22, 2022 at 1:28
loop()
? ... you have no code that confirms that