artpoz Posted July 31, 2021 Posted July 31, 2021 (edited) I have been playing with the Mindstorms Robot Inventor kit (#51515) for some time. Unfortunately, it does not have a display, but it has 25 LEDs. The battery level can only be viewed on a connected device: laptop or smartphone. I wrote a short Python program that lights up 3,5,10,15,20,25 LEDs depending on the battery level. It can be uploaded let's say as the 19th program and it will stay in the hub. Maybe it will be useful to someone. Any comments and suggestions are welcome. Battery level on Mindstorms Robot Inventor Edited August 1, 2021 by artpoz Quote
Munchkin255 Posted August 1, 2021 Posted August 1, 2021 That is a small and funny application. I have been working on a program that uses the LED to show the distance measured by the distance sensor. Just like in your program I initially though to write a program around a logic: What is done when measured value is X? I came to a solution that approached the problem from another angle. My logic is to look at the individual LEDs in the LED Matrix and for each LED determine if it should be on, dimmed or off. This allowed me to use for loop and define the criteria in a list. Please see program example below. Oh and I put the code inside a function. Please note, that because I am using Spike Prime I haven't been able to test the code - it should work though... Cheers Carsten from mindstorms import MSHub from mindstorms.control import wait_for_seconds, wait_until, Timer import hub # Create your objects here. hub = MSHub() battery = hub.battery.capacity_left() print(battery) def show_bat_level(): led_on_crit_full = [20, 40, 60, 80, 100] # List that defines when each led should turn on. led_on_crit_dimmed = [10, 30, 50, 70, 90] # List that defines when each led should turn on dimmed. for led_y in range (5): #Loop Through the five individual LED in the selected coloumn to check if it should be on, or off. if battery >= led_on_crit_full[led_y]: hub.display.pixel(0,led_y,9) elif battery >= led_on_crit_dimmed[led_y]: hub.display.pixel(0,led_y,5) else: hub.display.pixel(0,led_y,0) wait_for_seconds(3) hub.display.clear() show_bat_level() Quote
artpoz Posted August 5, 2021 Author Posted August 5, 2021 I think you forget to change the first parameter in function pixel(). Check below: for led_y in range (5): for led_x in range (5): if battery >= led_on_crit_full[led_y]: hub.display.pixel(led_x,led_y,9) Best regards artpoz Quote
Munchkin255 Posted August 5, 2021 Posted August 5, 2021 It was actually intentionally that I wrote 0 as the first parameter when calling the function hub.display.pixel(). This does off course only result in a single column of LED showing the battery level. (In this case to the left on the LED Matrix.) Which is different from your program, that uses all 25 LEDs. I have been using the same code to show the distance measured by three distance sensors. There it makes sense to only use one column for each distance measured. Did the code run without fault on the Robotic Inventor Hub? Cheers Carsten Quote
artpoz Posted August 5, 2021 Author Posted August 5, 2021 4 hours ago, Munchkin255 said: Did the code run without fault on the Robotic Inventor Hub? No. There is one error easy to fix: AttributeError: 'MSHub' object has no attribute 'battery' This line should be removed/commented as same as its commented in my code. hub = MSHub() Best regards artpoz Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.