Next Iteration Script and Test
Ask for Script Tuning
- Examine the conversation below about the way the AI was instructed to tune the script.
- Enter the new script below into the micro:bit Python Editor.
- Use the Send to micro:bit button to load the script into the micro:bit.
User
Nice work. I think it just needs a little tuning now. Specifically, it points at 6 anywhere between 7 and 6. It should really point at 6 while it’s anywhere from half way between 5 and 6, and half way between 6 and 7.
ChatGPT
Certainly, we can add a slight adjustment to the hour position calculation to achieve the desired behavior. The updated script below includes this adjustment:
from microbit import *
from math import atan2
def get_rotation_angle():
# Get the accelerometer readings
x, y, z = accelerometer.get_values()
# Calculate the rotation angle with an offset
angle = atan2(y, x) * (180 / 3.14159) + 90
# Ensure the angle is positive
angle = (angle + 360) % 360
return angle
def display_clock_hand(angle):
# Calculate the adjusted hour position
adjusted_hour_position = round(angle / 30)
# Map the adjusted hour position to the range [0, 11]
hour_position = adjusted_hour_position % 12
# Display the clock hand
display.show(Image.ALL_CLOCKS[hour_position])
while True:
# Get the rotation angle
rotation_angle = get_rotation_angle()
# Display the downward direction as a clock hand
display_clock_hand(rotation_angle)
# Optional: You can add a sleep time to avoid rapid updates
sleep(100)
In this version, the adjusted_hour_position is calculated by rounding the angle divided by 30. This adjustment should make the pointer point at 6 while it’s halfway between 5 and 6, and halfway between 6 and 7. Please give it a try, and let me know if it meets your expectations!
Tests
- Run the script and tilt the micro:bit.
- Compare the way this script behaves against the one from the previous Try an Existing Tilt Compass App.
- Do they match functionally?
- Do they match statement-by-statement, and does that matter?
How This Iteration Works
The intended solution added 15 degrees to the measurement with needle = ( angle + 90 + 15 ) // 30. The AI used a different approach. It used Python’s round function to achieve the same goal.