Python Forum
MicroPython ESP32 color changing from temps
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MicroPython ESP32 color changing from temps
#1
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:

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.
Reply


Messages In This Thread
MicroPython ESP32 color changing from temps - by Sim00778 - Apr-15-2021, 09:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Object Detection with ESP32 CAM. MateoG 0 593 Oct-11-2023, 01:44 PM
Last Post: MateoG
  [MicroPython] Beginner, program with lists Hellon 3 2,114 Nov-10-2019, 02:27 AM
Last Post: ichabod801
  working with Micropython - which ecosystem to choose - ESP 8266 or ESP 32? apollo 1 2,547 Aug-11-2019, 12:05 PM
Last Post: apollo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020