I am having a weird issue, may be due to my python code not running in sequence. When I have both a sound sensor and a light sensor connected (A0 and A1), then both report the light sensor value. This happens with the ultrasonic ranger (proximity sensor) as well. The DHT values are fine. Any ideas? Here is the code:
from time import sleep # we need to use the sleep function to delay readings
import datetime # that's for printing the current date
import time
import grovepi
import math
import json
from adxl345 import ADXL345
adxl345 = ADXL345()
dht_sensor = 4
ultrasonic_ranger = 6
light_sensor = 0
sound_sensor = 1
button = 3
grovepi.pinMode(sound_sensor,"INPUT")
grovepi.pinMode(button,"INPUT")
grovepi.pinMode(light_sensor,"INPUT")
data = {
'sound': 0,
'light': 0,
'3axis': 0,
'button': 0,
'temp': 0,
'humidity': 0,
'prox': 0
}
while True:
try:
data['sound'] = grovepi.analogRead(sound_sensor)
data['light'] = grovepi.analogRead(light_sensor)
[data['temp'],data['humidity']] = grovepi.dht(dht_sensor,1)
data['3axis'] = adxl345.getAxes(True)
data['button'] = grovepi.digitalRead(button)
data['prox'] = grovepi.ultrasonicRead(ultrasonic_ranger)
print(json.dumps(data))
print("sleeping")
time.sleep(0.2)
except TypeError:
print ("Error")
except IOError:
print ("Error")
Output:
{“sound”: 356, “temp”: 23.2, “prox”: 356, “light”: 356, “button”: 0, “humidity”: 73.4, “3axis”: {“y”: -0.004, “x”: -0.844, “z”: 0.496}}