I'm new to micropython. Trying to translate program I made for arduino into micropython. It's a compass based steering of small boat.
I'm stuck.
I have BNO055 that showing me heading value. In my original program when a button is pressed then variable 'h' remembers current heading value at the time of pressing the button. later on program compares 'h' with current heading and turns on one of motors (left or right) if current heading is smaller or larger. turning boar left or right to follow set direction.
I didnt found any good guide how to set value to the variable when button is pressed. Any tips?
I would be very grateful for help.
How to remember value from sensor when button pressed
Re: How to remember value from sensor when button pressed
You will have the button connected to a GPIO, say pin 27
Code: Select all
from machine import Pin
button_pin = Pin(27, Pin.IN, Pin.PULL_UP)
if (pin.value() == 0):
# read the BNO055 and save it's value
Last edited by davef on Tue Oct 26, 2021 9:28 pm, edited 1 time in total.
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: How to remember value from sensor when button pressed
I suppose you get heading value from the BNO055 with I2C.
Can you share a link to your current Arduino code? It will be easier to try to translate your code in MicroPython.
Can you share a link to your current Arduino code? It will be easier to try to translate your code in MicroPython.
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
Re: How to remember value from sensor when button pressed
Thank you.
I can do "translation" with some small help. it will be a good way to learn micropython and how to use my knowledge of arduino in here...
just python is totally different... have to look for everything - like how to remember sensor value... in arduino you just declare variable and use it later...
I can do "translation" with some small help. it will be a good way to learn micropython and how to use my knowledge of arduino in here...
just python is totally different... have to look for everything - like how to remember sensor value... in arduino you just declare variable and use it later...
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: How to remember value from sensor when button pressed
In MicroPython, no variable declaration, you just need to use one.
Code: Select all
from machine import Pin
def get_heading():
# Your code to get heading
pass
def move_right():
# Your code to move right
pass
def move_left():
# Your code to move left
pass
button_pin = Pin(27, Pin.IN, Pin.PULL_UP)
# Get first heading value
h = get_heading()
# Probably need to loop over here bellow code
if button_pin.value():
h = get_heading()
# Your code ...
# Heading variation
h_d = get_heading() - h
# Based on h_d do you motor action
if h_d > 0:
move_right()
elif h_d < 0:
move_left()
else:
pass
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir