I found little bit strange behaviour by Serial.println.
I am developing small measurement device, which is sending test status and values on UART.
In one moment I am sending this:
@el_supremo: No, I am printing from loop(). There are no interrupts in use.
@6v6gt: No, it is 7, see below. But even if it is 0 or 1, it should have no influence. It is Arduino Micro. Pins 0 and 1 are used by Serial1. Or am I wrong?
const int testValveOut = 7; // testing valve output pin
void loop() {
bool readyForTest = false;
bool greenLEDIsOn = false;
int startProbeLastState = HIGH;
int errorLevel = 0;
int testStage = 0;
unsigned long testStartTime = 0;
unsigned long runUpFinishedTime = 0;
unsigned long greenLedBlinkTimer = 0;
if(firstTime){
Serial.println("Nexans autoelectric GmbH. Seal test device.");
Serial.println("Hardware version: 1.0.0.0, 01.01.2017");
Serial.println("Firmware version: 1.0.0.0, 01.01.2017");
Serial.print("Serial number: ");
Serial.println(configuration.serialNumber);
Serial.println(":test:entering");
firstTime = false;
}
while (test){
if(Serial.available()>0) {
test = false;
Serial.println("going_off_line");
break;
}
if(!readyForTest && (testStartTime == 0)) {
Serial.println(":test:ready");
readyForTest = true;
}
if(digitalRead(failIn) == LOW) analogWrite(redLedOut,configuration.brightRed);
else analogWrite(redLedOut,0);
if((digitalRead(releaseButtonIn) == LOW) || //Release button pushed or
(digitalRead(startProbeIn) == HIGH)) { //presence probe not activated?
digitalWrite(presenceOut, LOW); //Switch all off and init
digitalWrite(cpaTestResultOut, LOW);
digitalWrite(sealTestResultOut, LOW);
digitalWrite(catchValveOut, LOW);
digitalWrite(testValveOut, LOW);
analogWrite(greenLedOut, 0);
greenLEDIsOn = false;
analogWrite(yellowLedOut, 0);
analogWrite(blueLedOut, 0);
analogWrite(cpaLightSourceOut,0);
startProbeLastState = HIGH;
errorLevel = 0; //Init test result.
testStage = 0;
testStartTime = 0;
runUpFinishedTime = 0; //Init run-up phase
continue;
}
digitalWrite(catchValveOut, HIGH); //Catch it
digitalWrite(presenceOut, HIGH); //Activate output for tester "connector in adapter"
analogWrite(yellowLedOut,configuration.brightYellow); //Set yellow LED on.
analogWrite(cpaLightSourceOut,configuration.CPAtestSourcePower);
if((configuration.CPASensor1Min < analogRead(cpaInPin1)) //Test CPA
&&(configuration.CPASensor1Max > analogRead(cpaInPin1))
&&(configuration.CPASensor2Min < analogRead(cpaInPin2))
&&(configuration.CPASensor2Max > analogRead(cpaInPin2))) {
analogWrite(blueLedOut,configuration.brightBlue);
digitalWrite(cpaTestResultOut, HIGH);
}
else {
analogWrite(blueLedOut,0);
digitalWrite(cpaTestResultOut, LOW);
}
if(errorLevel < 0) { //on OK
digitalWrite(sealTestResultOut, LOW);
analogWrite(greenLedOut, configuration.brightGreen);
continue;
}
if(errorLevel > 0 ) { //on NOK
digitalWrite(sealTestResultOut, HIGH);
if(greenLedBlinkTimer > millis()) continue; //nothing to change yet.
if(greenLEDIsOn) { //toggle LED
analogWrite(greenLedOut, 0);
greenLEDIsOn = false;
}
else {
analogWrite(greenLedOut, configuration.brightGreen);
greenLEDIsOn = true;
}
if (errorLevel == 1) greenLedBlinkTimer = millis() + 200; //fast blinking (minimum pressure reached too fast)
if (errorLevel == 2) greenLedBlinkTimer = millis() + 400; //slower blinking (minimum pressure not reached)
if (errorLevel == 3) greenLedBlinkTimer = millis() + 800; //slow blinking (error in drop-out phase)
continue;
}
if(startProbeLastState == HIGH) { //Just inserted?
startProbeLastState = LOW; //Actualize old probe status.
testStartTime = lasttime = millis(); //Store time of start of test
readyForTest = false;
Serial.println(":test:start");
}
if (testStartTime == 0) continue; //Test not started yet
if((testStartTime + configuration.startUpDelay) > millis()) continue; //Start-up delay not gone yet
pressure = getPressure(false);
if((pressure > configuration.testPressure)&&(runUpFinishedTime == 0)) { //Minimum pressure just reached.
digitalWrite(testValveOut, LOW); //Close filling valve.
runUpFinishedTime = millis(); //Store run-up phase finished time
Serial.println(":test:level_reached");
if((testStartTime + configuration.startUpDelay + configuration.runUpMinTime) > runUpFinishedTime) { //Minimum pressure reached too fast
errorLevel = 1; //Set error level to 1 (minimum pressure reached too fast)
Serial.println(":test:failed:level_reached_too_fast");
continue;
}
if(measPoints[0].time == 0){ //Just run-up phase is tested.
errorLevel = -1; //Test passed.
Serial.println(":test:passed");
continue;
}
testStage = 1; //Test stage 1
}
if((testStage == 0) && (digitalRead(testValveOut) == LOW)) { //Run-up not finished yet => filling-up
digitalWrite(testValveOut, HIGH);
Serial.println(":test:filling_started");
}
if((lasttime + 10) < millis()) {
lasttime = millis();
Serial.print(":test:pressure:");
Serial.println(pressure);
}
if(((testStartTime + configuration.startUpDelay + configuration.runUpTimeout) < millis())&&(runUpFinishedTime == 0)){ //Time for reach minimum pressure is gone
digitalWrite(testValveOut, LOW); //Close filling valve.
errorLevel = 2; //Set error level to 2 (minimum pressure not reached)
Serial.println(":test:failed:level_not_reached");
continue;
}
if(testStage == 0) continue; //Minimum pressure not reached yet.
if((runUpFinishedTime + measPoints[testStage-1].time) > millis()) continue; //It is not time for measure yet
if((pressure < measPoints[testStage-1].minPressure) || (pressure > measPoints[testStage-1].maxPressure)){ //Test stage failed
Serial.print(":test:stage_");
Serial.print(testStage);
Serial.println("_failed");
Serial.println(":test:failed");
errorLevel = 3;
continue;
}
else {
Serial.print(":test:stage_");
Serial.print(testStage);
Serial.println("_passed");
}
testStage++; //next test stage
if((testStage > 8) || (measPoints[testStage-1].time == 0)){ //All phases passed
errorLevel = -1; //Test passed.
Serial.println(":test:passed");
continue;
}
}