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

How it Doesn’t Work

User
When I tested this script, the micro:bit reported this exception: NameError: name ’atan2’ isn’t defined.

ChatGPT
I apologize for the oversight. It seems I missed importing the atan2 function from the math module. Please modify the script to include the necessary import statement:

from math import atan2

Try This: Add the import atan2 Statement

Alright, let’s try the correction the AI suggested.

  • Add the statement from math import atan2 right below the from microbit import * statement.
  • Send the modified script to your micro:bit.

Does it the LED display point downward as it should yet?


Did You Know:  AI Doesn’t Know Everything

An AI is not an oracle that answers every question correctly.  Sometimes it guesses or makes incorrect assumptions.  That’s why the micro:bit LED display points sideways instead of downward in this example. So, it is important to evaluate everything a chatbot provides to you before you make use of it.


Your Turn: Try the Correction

  • Test the modified script below.  Does it work correctly?
  • Read the conversation below, and then answer these questions.
    • How would you change the explanation from the user to make it less vague for a better chance of success?
    • How would you compare this script to the one in Intended Solution – A Tilt Compass?  Hints:
      • In addition to the number of lines in the script, the conditions for updating the direction the micro:bit points is slightly different.  
      • If you are in a classroom, pair up with another student.  Make one mirco:bit run the script from Intended Solution – A Tilt Compass and the other run the script below.  Watch carefully how each one updates the direction it points as you hold them vertically and gradually rotate them.  

User
Okay, that resolved the exception, but the pointer is off by 90 degrees.
NOTE: With these vague instructions, the AI has a 50/50 chance of changing the script to make it point up instead of down!

ChatGPT
I appreciate your feedback. It seems there might be an issue with the initial orientation of the micro:bit. To correct this, you can add an offset to the initial angle calculation. Here’s an updated version of the script:

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 hour position
    hour_position = int(angle / 30)

    # 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)

This modification adds a 90-degree offset to the initial angle calculation, which should align the pointer with the downward direction when the micro:bit is lying flat. Feel free to test it, and let me know if you encounter any further issues!

 


Printer-friendly version
AI-generated Tilt Compass Script
Prev
Development Iterations with AI
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress