This code is in the loop for all of the IoT projects that involve light detection, and it completely kills my programs:
while (!carrier.Light.colorAvailable()) {
delay(5);
}
If I leave this block inside my loop() function, either:
- No output ever appears on the serial monitor. Arduino shows as offline on the Things setup page.
- Serial outputs 3 loop iterations and then stops. Arduino shows as offline on the Things setup page.
Example of the output from #2:
Sd card not detected
WiFi.status(): 0
Current WiFi Firmware: 1.4.3
Gyroscope: X: 0.73 Y: -2.56 Z: -4.21
Light: 693
PIR State: 0
Gyroscope: X: 0.61 Y: -2.44 Z: -4.21
Light: 601
PIR State: 0
Gyroscope: X: 0.73 Y: -2.44 Z: -4.09
Light: 581
PIR State: 0
After this, the device just stops doing anything. I've tried increasing the delay in the loop to 1000 just to see what happens, and in that case the program only outputs 2 iterations of the loop instead of 3.
If I don't run this loop, the light sensor seems to give me a random nonsense value (either 0 or 252) for the fourth parameter:
carrier.Light.readColor(none, none, none, light);
The "light" variable will just randomly switch between 0 and 252 no matter what the ambient light of the room is.
I've tried just calling carrier.Light.colorAvailable() in the loop without putting it inside the while(), and that lets my programs run but it doesn't fix the output of the sensor being random.
So any ideas? Is my light sensor just faulty? I am having issues with the buttons on this carrier as well (it is constantly detecting Button 0 presses), at this point I am wondering if my unit is defective.
EDIT - So I put in a variable to monitor the return value of the colorAvailable() function, and it seems to only return 1 one time at the start of my program and then returns 0 forever after (see red lines below):
Sd card not detected
WiFi.status(): 0
Current WiFi Firmware: 1.4.3
ColorAvail: 0
Gyroscope: X: 0.67 Y: -2.44 Z: -4.09
Light: 0
PIR State: 0
ColorAvail: 1
Gyroscope: X: 0.61 Y: -2.44 Z: -4.15
Light: 690
PIR State: 0
ColorAvail: 0
Gyroscope: X: 0.73 Y: -2.56 Z: -3.97
Light: 690
PIR State: 0
ColorAvail: 0
Gyroscope: X: 0.73 Y: -2.50 Z: -3.97
Light: 690
PIR State: 0