Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • AI for Simple Python Scripts

AI for Simple Python Scripts

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.

 


Printer-friendly version
Development Iterations with AI
Prev
Condensed Code
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress