Some python coding help

import time
import rainbowhat as rh

A = 0
B = 1
C = 2


mode = C

pressure = rh.weather.pressure() / 100

@rh.touch.press()
def touch_press(channel):
	global mode
	mode = channel

while True:
	rh.display.clear()

	if mode == A:
		rh.lights.rgb(1,0,0)
		temperature = rh.weather.temperature()
		rh.display.print_float(temperature)
				
	if mode == B:
		rh.lights.rgb(0,1,0)
		pressure = rh.weather.pressure() / 100
		rh.display.print_float(pressure)
				
	if mode == C:
		rh.lights.rgb(0,0,1)
		
	if pressure >= float(1030):
		pass
		rh.rainbow.set_pixel(0, 128, 0, 0)
		rh.rainbow.show()
		
	elif pressure >= float(1020) <= float(1029):
		pass
		rh.rainbow.set_pixel(1, 128,32, 0)
		rh.rainbow.show()
		
		
	elif pressure >= float(1010) <= float(1019):
		pass
		rh.rainbow.set_pixel(2, 0, 128, 0)
		rh.rainbow.show()
		
	elif pressure >= float(1000) <= float(1009):
		pass
		rh.rainbow.set_pixel(3, 32, 64, 0)
		rh.rainbow.show()
	
	elif pressure >= float(990) <= float(999):
		pass
		rh.rainbow.set_pixel(4, 32, 0, 32)
		rh.rainbow.show()
	
	elif pressure >= float(980) <= float(989):
		pass
		rh.rainbow.set_pixel(5, 0, 0, 128)
		rh.rainbow.show()

	elif pressure <= float(979):
		pass
		rh.rainbow.set_pixel(6, 128, 128, 128)
		rh.rainbow.show()

	rh.display.show()
				
	time.sleep(0.01)

Here is the latest code Dean (above) I’ve tested by forcing a change to the temp values and the correct lights now work with the colours I’ve setup.

Im not sure why evertime I post the code not all of it looks like code, ie, th first couple of sections, perhaps a moderator / admin can help? I’ve tried the 4 x backtick as previously suggested???

Let me know what you think Dean

ps, if you want to view the rainbow colors

import rainbowhat as rh
import time

rh.rainbow.set_pixel(0, 128, 0, 0)
rh.rainbow.set_pixel(1, 128, 32, 0)
rh.rainbow.set_pixel(2, 0, 128, 0)
rh.rainbow.set_pixel(3, 32, 64, 0)
rh.rainbow.set_pixel(4, 32, 0, 32)
rh.rainbow.set_pixel(5, 0, 0, 128)
rh.rainbow.set_pixel(6, 128, 128, 128)
rh.rainbow.show()
time.sleep(20.0)

it is 3 backsticks, not 4:

```python
your code
```
1 Like

Thanks Rogueā€¦šŸ˜€ wondered why it wasn’t working!

Thanks a lot for the help with that Scott. I’ve got it going pretty good now. Just got to figure out the temperature alteration. I’ve also been playing around with the buzzer. Looking at getting it to buzz when the pressure changes or play a little tune.

Took this from the blog it you hadn’t seen it. I’ll link some code when I have a bit completed for it.

https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-rainbow-hat-in-python

Piezo buzzer
The piezo buzzer allows you to play notes of specific frequencies, or midi note numbers. Let’s begin by playing a note of a specific frequency, middle C which has a frequency of 261Hz. The buzzer methods are under the buzzer module, rh.buzzer, and the note method takes a frequency in Hz and a note length in seconds.

Type the following:

rh.buzzer.note(261, 1)
That should play middle C for 1 second. Next, we’ll do that same but with the midi_note function that allows you to use midi note numbers. Middle C is midi note 60, so we’ll play that now.

Type the following:

rh.buzzer.midi_note(60, 1)
To play a tune, you could make a list of the notes, and then loop through them, with a short pause after each one.

Type the following:

song = [68, 68, 68, 69, 70, 70, 69, 70, 71, 72]

for note in song:
rh.buzzer.midi_note(note, 0.5)
time.sleep(1)
It’s worth noting that if you want a half second note and then a half second pause, you’ll add your note length to the length of your pause, e.g. 0.5 + 0.5 = 1 in the example above.

Also, the notes we’ve used are all of the same length and ideally you’d want to vary the note length to make the tune sound correct.

Did you guess what song that was?

Hi Dean, Glad it’s working!

Have you looked at Pimoronid github examples?

Might help you with the sounds?

I need to figure out how to clear the rainbow hat using the c key as it’s leaving one led lit but clearing the 7 seg display correctly.

I’m going to have a play with that myself I believe it might be to do with rh.rainbow.clear() probably in the Section For button C when pressed

Thanks for the sound info I just added some buzzer sounds when A B C pushed. I’ll log in some new code by weekend should fix the light parts up.

Are you going to make a case for yours or mount somewhere for leave as it is? I have a cable to seperate hat now so I can get more accurate temp readings.

Hi Dean,

I tried the rh.rainbow.clear() in the C button section but it doesn’t do anything. It’s like the the rainbow is being displayed continuously. I’ll play and figure it out.

Good to hear you got the sounds going.

I have a Pibow case but at the moment I’m not using because of the temp issue so I just have it running off a GPIO cable on the Mini black hat.

I might make one later but for now am happy to play outside a case.

cheers

Scott

Ooohhh it’s Rainy nearly Stormy!

Cool pic that don’t happen much where I live lol. Sometimes rarely. How did you go with the code getting the lights to clear? I was going to have a go tonight. I noticed if you leave B option on and the pressure changes the previous light stays on at the moment also if you get what I mean.

didn’t manage to get the clear thing going not had much time today will try this weekend though.

Think we have a few more bugs to iron out!

cheers

scott

Hey Scott,

I’ve found a fix for the lights staying on when there is a pressure change I believe. I have yet to find a solution for the clearing when C is pushed but must be close. I have also fixed some of the pressure ranges which when I re-looked at I figured were wrong. I’ve extended change range as it was further towards fair than rain. Change is generally a large scale on pressure gauges.

Anyway here is my up to date code:

import time
import os
import rainbowhat as rh

A = 0
B = 1
C = 2

mode = C

pressure = rh.weather.pressure() / 100

@rh.touch.press()
def touch_press(channel):
global mode
mode = channel

while True:
rh.display.clear()

if mode == A:
    rh.lights.rgb(0,0,0)
    temperature = rh.weather.temperature()
    rh.display.print_float(temperature)
    
if mode == B:
    rh.lights.rgb(0,0,0)
    pressure = rh.weather.pressure() / 100
    rh.display.print_float(pressure)
            
if mode == C:
    rh.lights.rgb(0,0,0)
     
if pressure >= float(1030):
    pass
    rh.rainbow.set_pixel(0, 128, 0, 0)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(0, 0, 0, 0)
            rh.rainbow.show()
elif pressure >= float(1020) <= float(1029):
    pass
    rh.rainbow.set_pixel(1, 128,32, 0)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(1, 0, 0, 0)
            rh.rainbow.show()
    
elif pressure >= float(1010) <= float(1019):
    pass
    rh.rainbow.set_pixel(2, 0, 128, 0)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(2, 0, 0, 0)
            rh.rainbow.show()
    
elif pressure >= float(991) <= float(1009):
    pass
    rh.rainbow.set_pixel(3, 32, 64, 0)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(3, 0, 0, 0)
            rh.rainbow.show()

elif pressure >= float(981) <= float(990):
    pass
    rh.rainbow.set_pixel(4, 32, 0, 32)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(4, 0, 0, 0)
            rh.rainbow.show()

elif pressure >= float(971) <= float(980):
    pass
    rh.rainbow.set_pixel(5, 0, 0, 128)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(5, 0, 0, 0)
            rh.rainbow.show()
elif pressure <= float(970):
    pass
    rh.rainbow.set_pixel(6, 128, 128, 128)
    rh.rainbow.show()
    while False:
        for pixel in range (7):
            rh.rainbow.clear()
            rh.rainbow.set_pixel(6, 0, 0, 0)
            rh.rainbow.show()
rh.display.show()
            
time.sleep(0.01)