Hardware Reference
In-Depth Information
The current statue of the system is reported on by using the serial output to show the status of the system in
JSON format.
Listing 9-9. The main loop reports on the status of the system in a JSON format via serial.
if(emitter1State) {
prevEmitter1State = emitter1State;
Serial.print(" ,\"Obj\": \"Right\"");
}
if (prevMeasureA == measureA) {
if (e1detected && e2detected)
{
Serial.print(" ,\"Obj\": \"Front\"");
}
}
if(emitter2State) {
prevEmitter2State = emitter2State;
Serial.print(" ,\"Obj\": \"Left\"");
}
Serial.println("}");
prevMeasureA = measureA;
prevMeasureB = measureB;
prevPhaseA = phaseA;
detected = false;
}
}
Listing 9-10 shows the detection logic. If only emitter1 is detected in a measure there is an object on the left.
If only emitter2 is detected in a measure then an object on the right is detected. If in the measure both emitter1, and
emmiter2 are detected there is an object in front of the device.
Listing 9-10. The detection logic is defined by what is detected in a single measure.
void readIRSensor() {
digitalWrite(PIN_LED1, HIGH);
if(emitter1State) {
emit1Count++;
detectCount++;
detected = true;
e1detected = true;
}
else if (emitter2State) {
emit2Count++;++;
detectCount++;
detected = true;
e2detected = true;
}
}
Search WWH ::




Custom Search