How to remember value from sensor when button pressed

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ludwik
Posts: 2
Joined: Tue Oct 26, 2021 6:28 pm

How to remember value from sensor when button pressed

Post by ludwik » Tue Oct 26, 2021 6:42 pm

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.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: How to remember value from sensor when button pressed

Post by davef » Tue Oct 26, 2021 9:14 pm

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.

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to remember value from sensor when button pressed

Post by OlivierLenoir » Tue Oct 26, 2021 9:16 pm

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.

ludwik
Posts: 2
Joined: Tue Oct 26, 2021 6:28 pm

Re: How to remember value from sensor when button pressed

Post by ludwik » Wed Oct 27, 2021 9:17 am

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...

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: How to remember value from sensor when button pressed

Post by OlivierLenoir » Wed Oct 27, 2021 3:44 pm

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

Post Reply