Protocol for posting extensive questions

I have an ongoing problem with a micropython programme running on a Plasma2040 connected via I2C to a RTC board and 4 digit display. To properly explain my problem will involve posting my programme (>100 lines) listing and the output from a Thonny screen but I am not sure about the protocols and limits for doing this on this forum. Can you advise me please?

I’ve posted walls of code here. It’s not a big deal if you use code tags.
Three ` above your first line of code, and three more under your last line of code.
On my English US layout keyboard, its the key to the left of the 1 and under the Esc key.
It has the ~ on it.

I’m with Alphanumeric, unless you have a Github account (as I’ve seen quite a few postings with a link pointing there for the code), then yeah use the three ` (top and bottom of code) method.

We’d be most happy to help you.

Let’s hear it 👏👏 hope we can help

Thank you gents. I am tied up with a domestic problem at present but I shall pursue this problem next week. Thanks again for your prompt responses.

Just a quick couple of supplementary question. What version of the “uf2” file should I be using with the hardware as described above and what interpreter should Thonny be using?

You can run it off Circuit Python or MicroPython. If micropython should be using this one

https://github.com/pimoroni/pimoroni-pico/releases/download/v1.19.12/pimoroni-pico-v1.19.12-micropython.uf2

Thonny, I would try micropython raspberry PI pico interpreter.

Here are the details of my Plasma2040 problem. I have a Plasma2040 driving a string of LEDs and a PCF8523RTC and a 4digit display both via I2C from the QW/ST port. This currently controlled from Thonny so that I can experiment. The intention is to have the LEDs change colour every hour determined by the RTC time which is initially set. I have this set-up basically working with the programme below but the problem is that the “while” loop performs 8 complete iterations and then “stops” although when I try to stop and restart the programme in Thonny it reports that it cannot interrupt the current process indicating that the programme is still running in some form.

Micropython Programme PEversion_clean_test.py

Import plasma
from plasma import plasma2040
import time
from pimoroni import RGBLED, Button
from ht16k33segment14 import HT16K33Segment14
from pimoroni_i2c import PimoroniI2C
from pcf8523 import PCF8523
import time


#The following line sets the pin numbers for the I2C Data and Clock lines for the plasma2040
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}

NUM_LEDS = 72
#The following lines import the libraries to support I2C and the 4 digit display

#The following lines define 24 RGB values, one for each hour of the day.

Data_Array = []

for i in range (0,24):
    Data_Array.append([0,0,0,0])

Data_Array[0][1] = 0
Data_Array[0][2] = 0
Data_Array[0][3] = 0
Data_Array[1][1] = 0
Data_Array[1][2] = 0
Data_Array[1][3] = 0
Data_Array[2][1] = 0
Data_Array[2][2] = 0
Data_Array[2][3] = 0
Data_Array[3][1] = 32
Data_Array[3][2] = 28
Data_Array[3][3] = 20
Data_Array[4][1] = 37
Data_Array[4][2] = 33
Data_Array[4][3] = 25
Data_Array[5][1] = 64
Data_Array[5][2] = 56
Data_Array[5][3] = 40
Data_Array[6][1] = 128
Data_Array[6][2] = 112
Data_Array[6][3] = 80
Data_Array[7][1] = 174
Data_Array[7][2] = 150
Data_Array[7][3] = 84
Data_Array[8][1] = 207
Data_Array[8][2] = 183
Data_Array[8][3] = 144
Data_Array[9][1] = 183
Data_Array[9][2] = 174
Data_Array[9][3] = 176
Data_Array[10][1] = 159
Data_Array[10][2] = 187
Data_Array[10][3] = 208
Data_Array[11][1] = 165
Data_Array[11][2] = 165
Data_Array[11][3] = 182
Data_Array[12][1] = 170
Data_Array[12][2] = 132
Data_Array[12][3] = 157
Data_Array[13][1] = 176
Data_Array[13][2] = 106
Data_Array[13][3] = 131
Data_Array[14][1] = 181
Data_Array[14][2] = 85
Data_Array[14][3] = 106
Data_Array[15][1] = 187
Data_Array[15][2] = 78
Data_Array[15][3] = 80
Data_Array[16][1] = 94
Data_Array[16][2] = 39
Data_Array[16][3] = 40
Data_Array[17][1] = 36
Data_Array[17][2] = 0
Data_Array[17][3] = 0
Data_Array[18][1] = 34
Data_Array[18][2] = 0
Data_Array[18][3] = 0
Data_Array[19][1] = 30
Data_Array[19][2] = 0
Data_Array[19][3] = 0
Data_Array[20][1] = 28
Data_Array[20][2] = 0
Data_Array[20][3] = 0
Data_Array[21][1] = 26
Data_Array[21][2] = 0
Data_Array[21][3] = 0
Data_Array[22][1] = 24
Data_Array[22][2] = 0
Data_Array[22][3] = 0
Data_Array[23][1] = 22
Data_Array[23][2] = 0
Data_Array[23][3] = 0

#This is the main body of the programme which gets time from the RTC as hours and minutes.
#The numeric values of hours and minutes are converted to strings so that values can be "padded" with
# zeroes to allow the numeric displays to show leading zeroes for values less tha 9.

# This is the main while loop which refreshes the 4 digital displays and sets the cpour of the led array
# to conform to the hour value set by the RTC.
Count = 0
while True:
    
    #from ht16k33segment14 import HT16K33Segment14
    #from pimoroni_i2c import PimoroniI2C
    #from pcf8523 import PCF8523
    #import time

    i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
    rtc = PCF8523( i2c )
    print ("i2c and rtc call")

    _time = rtc.datetime
    days = ['monday','tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
    weekday = time.localtime(_time) [6]
    print( 'Day of week: %s' % days[weekday] )
    Hours = time.localtime(_time) [3]
    print ( "RTC Hours = " , Hours)
    Minutes = time.localtime(_time) [4]
    print ( "RTC Minutes = ", Minutes)

    hour = str(Hours)
    #print ("Line 120 ", hour)
    if len(hour) < 2:
        hour = "0" + hour
    print ("hour padding ")
        
    minute = str(Minutes)
    
    if len(minute) < 2:
        minute = "0" + minute
    print ("minute padding ")

    i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
    led = HT16K33Segment14(i2c)
    led.set_brightness(1)
    led.set_colon().draw()
    led.set_character(hour[0], 0).set_character(hour[1], 1)
    led.set_character(minute[0], 2).set_character(minute[1], 3).draw()
    time.sleep(5)
    print ("Write to display")
    # import plasma
    # from plasma import plasma2040
    # import time
    # from pimoroni import RGBLED, Button 
    # NUM_LEDS = 40
    print ("led_on_board")
    led_On_Board = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
    led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
    led_strip.start()
    print ("LED strip define")
    Red = (Data_Array[Hours][1])
    Green = (Data_Array[Hours][2])
    Blue = (Data_Array[Hours][3])

    print ("Set RGB colours from array")
        
    for i in range (0,(NUM_LEDS)):
        led_strip.set_rgb(i,int(Red),int(Green),int(Blue))
        led_On_Board.set_rgb(Red, Green, Blue)
    print ("End of While loop")
    Count += 1
    print ("Count = ", Count)

The Plasma2040 has the following programmes/libraries installed:
ht16k33.py
ht16k33segment14.py
pcf8523.py
PEversion_clean_test.py ( my micropython programme)
test_getdate.py
test_setdate.py

The Thonny Interpreter is set to “Micropython (Raspberry Pi Pico)”
The “uf2” file is “Pimoroni-pico-version1.19.12-micropython.uf2”

For completeness, this is the output from the Thonny Shell window when the programme runs:-

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 15

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 1

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 15

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 2

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 15

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 3

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 15

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 4

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 15

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 5

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 16

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 6

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 16

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 7

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 16

hour padding

minute padding

Write to display

led_on_board

LED strip define

Set RGB colours from array

End of While loop

Count = 8

i2c and rtc call

Day of week: tuesday

RTC Hours = 20

RTC Minutes = 16

hour padding

minute padding

Write to display

As I say, the programme “nearly” works; I just can’t figure out why it should halt after 8 complete iterations of the “while loop”! Any insights would be very gratefully received. If I have presented this information in the “wrong” format, please let me know so that I won’t make the same mistake again.

So in a nutshell, it completes 8th full cycles and on the 9th:

Count = 8
i2c and rtc call
Day of week: tuesday
RTC Hours = 20
RTC Minutes = 16
hour padding
minute padding
Write to display <---- dies right here??

Does not output the very next command, print statement?: → “led_on_board”

Got it in one, Mad Monk ! Just one observation which may or may not be relevant,. When I run the programme in Thonny and watch the Shell output, there is a blank line between the final “minute padding” and the “Write to display” which doe snot appear in any of the previous 8 iterations. Intriguing ha?

Curious, if you could get a print out of the values defined for “hour” and “minute” in the above 2 statements…

Hi Mad Monk. I ran the programme at 22:35 tonight with the programme modified as:

hour = str(Hours)
#print ("Line 120 ", hour)
if len(hour) < 2:
hour = “0” + hour
print ("hour padding ")
print (hour)

minute = str(Minutes)

if len(minute) < 2:
    minute = "0" + minute
print ("minute padding ")
print (minute)

and the outputs were:

i2c and rtc call
Day of week: wednesday
RTC Hours = 22
RTC Minutes = 35
hour padding
22
minute padding
35
Write to display
led_on_board

Hope this helps.

Just a question that popped into my head overnight. At the line “While True” what is it that micropython evaluates to determine if this condition is met or not ?

The only thing that springs immediately to mind is that you’re (repeatedly) initialising PimoroniI2C (and others) in every loop, rather than once at the start of the program.

It’s possible that might be slowly causing you problems (although theoretically it shouldn’t, it’s just inefficient)

Thank you for your comment Ahnlak and I am sure you are correct. All I can say is that, at my present state of programming knowledge, “inefficient” is a very apt description but I am trying ! I can’t help but think that the fact that the programme does precisely 8 iterations of the While loop before stopping must be relevant and I have tried altering timing with different “sleep” values but to no effect. I am confident the answer is out there somewhere and when it appears my response will be along the lines of “of course!!!”

Self taught Python / Micro Python amateur here. I’m pretty sure

    i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
    rtc = PCF8523( i2c )
    print ("i2c and rtc call")

can go above the While true:
You have i2c = PimoroniI2C(**PINS_PICO_EXPLORER) in there twice. This can likely also go above the while true.

    i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
    led = HT16K33Segment14(i2c)
    led.set_brightness(1)

and

    led_On_Board = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
    led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
    led_strip.start()

import time is listed twice, not sure if that matters but can’t hurt to remove duplicate entries, just in case its using up you memory area?

What happens if you remark out the “Count” code? It starts at 0 so its actually Count = 9 where its failing?

Thank you alphanumeric, I am in your debt !! Moving the last three lines (led_On_Board = …, led_strip = … and led_strip.start() ) to before the While statement solved the problem (although it would be nice to know why). Let’s not be ungrateful, it works !!!

I’ve had my fair share of situations like that. When it fails in an IDE you usually get a hint at what happens. When it fails running headless its guess what happened time.

I did try de-bugging in Thonny but my version at least doesn’t support de-bugging for anything other than Python 3. Your comment about “Micropython amateur” is spot on which is why I am grateful that there are people like yourself around willing to give a helping hand. Once again, thank you.

1 Like