For a class I'm in I'm making a coaster that lights up differently depending on how cold or hot the drink sitting on it is.
Connected to the ESP32 board are the ds18b20 thermometer and a LED ring of 24 Neopixels. I've managed to make both of them work seperately just fine, but linking the RGB values to the temperature measurements is turning out to be very tricky.
The following is what I have now:
The thermometer needs the following:
A Neopixel can be controlled as follows:
As you can see I've tried to put the LED's code in the Thermometer's loop, but it only gives temperature when it worked.
Is there a better way to go about it? How can I best link these parameters? Or any general tips will help a lot.
Connected to the ESP32 board are the ds18b20 thermometer and a LED ring of 24 Neopixels. I've managed to make both of them work seperately just fine, but linking the RGB values to the temperature measurements is turning out to be very tricky.
The following is what I have now:
import machine, neopixel, onewire, ds18x20, time np = neopixel.NeoPixel(machine.Pin(5), 24) ds_pin = machine.Pin(4) ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin)) roms = ds_sensor.scan() r = 0 g = 0 b = 0 grens = 15 temp = (ds_sensor.read_temp(rom)) while True: ds_sensor.convert_temp() time.sleep_ms(750) for rom in roms: #print(rom) print(ds_sensor.read_temp(rom)) def color(r, g, b): if (ds_sensor.read_temp(rom)) > grens: np[0] = color((temp//2), 0, 0) np.write else: np[0] = color(0, 0, (20-temp)) np.write time.sleep(1) print "done"'grens' means limit/threshold and is there to be able to set greater/lesser than parameters.
The thermometer needs the following:
while True: ds_sensor.convert_temp() time.sleep_ms(750) for rom in roms: #print(rom) print(ds_sensor.read_temp(rom)) time.sleep(1)This made it print the temperature correctly every second.
A Neopixel can be controlled as follows:
r = 0 g = 0 b = 10 np[0] = (r, g, b) np.write()Running this will make the 1st LED light up blue. ((0, 0, 0) gives no light/off)
As you can see I've tried to put the LED's code in the Thermometer's loop, but it only gives temperature when it worked.
Is there a better way to go about it? How can I best link these parameters? Or any general tips will help a lot.
buran write Apr-15-2021, 09:53 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.