Arduino Alvik and Micropython Lab

Hi all,

I recently bought an Arduino Alvik robot and did a couple programming using Arduino Lab for Micropython.

Uploaded the sketches to the Alvik and they are working as expected.

Basically, I wrote a code to control the Nano ESP32 on board and Alvik LEDs using Alvik touch buttons. The program (sketch) is working perfectly.

But the sketch is too lengthy with 3 different functions (subroutines).

  1. I would like to know whether I can save one of the functions as a separate file (ex. myfunction.py), as a module, and then import that file into my main sketch? If so, what is the correct procedure to create and import a user define module/file?

  2. Since I have downloaded and using Arduino Lab for Micropython, to program Alvik, do I still need to install Micropython (arduino-alvik-mpy or Arduino MicroPython Installer) ?

Thank you for any feedback!

Best

Yes, you can save Python code, including functions, in a .py file and import them. The easiest thing to do is to save the .py file that you want to import in the same folder as your main Python file. Doing that avoids any complications with file paths

Micropython is already installed on the ESP32 otherwise your LED/Buttons script would not work. There is no need to install anything else.

I briefly used micropython on the Alvik and found Arduino Lab for Micropython quite clumsy and used Thonny instead but your experience may be different

Thank you for your reply.

I did exactly the same way you have mentioned, using Arduino Lab for Micropython.

There is an error saying “ import error” . And then I created a simple file with just two print statements. The file was saved using the name Test.py in the same folder where the main.py is.

And then import it using the command, import Test. Nothing happened. It looks like the main sketch just ignores that file. Any idea?

@jfdofdo you can access your function by importing it from the module.

example with a module named Test.py

Test.py

def myfunction(val1,val2):
    return val1 + val2

use the function in the main module

from Test import myfunction
x=myfunction(2,2)
print(x)

alternatively import all objects from a module
from Test import *

classes are better but that may be further down the road for you yet

@sumguy

Thank you for the reply.

I tried with Arduino Lab for Micropython.

I can import a function (module) as long as there is no argument.

I get the following error when the function is invoked with an argument.

“TypeError: function takes 0 positional arguments but 1 were given”

Is it an issue with Micropython Lab or something is incorrect with the import or definition of the function?

Thank you.

You need to post an example of the function that causes the error and the script that imports and uses it

Hi all,

I did lots of test codes using Arduino Lab for Micropython to program Arduino Alvik during last few days. I used Nano ESP32 onboard Leds (as outputs) along with Alvik touch buttons (as inputs) and Alvik left & right Leds (as outputs) to test.

And then I found the following.

I can import user defined module/s, into a user program (code), which has/have function/s (that take input arguments), and return a value. They are working as expected.

Everything is working fine as long as the Alvik is connected to Arduinos' Micropython Lab using the USB cable.

However, if click the “Reset” button (even with the USB cable is still connected), the program is working well with ESP32, with Alvik touch buttons and ESP32 LEDs.

But after pressing the “Reset” button Alvik Left and Right LEDs are not working. But if I replace the Alvik LEDs with ESP32 LEDs (in that part of the code) they are working.

And then I checked the Alvik firmware version on my robot, using the following code:

from arduino_alvik import ArduinoAlvik
alvik=ArduinoAlvik()
version = alvik.get_version() # Returns the firmware version 
print("Alvik Firmware Version:", version)

It could not find the version and says “None”

And when I click “Reset” button some times I get the following error message too.

“ MPY: soft reboot
:snake: :robot: Could not get FW version
Traceback (most recent call last):
File "main.py", line 2, in
File "alvik_Buttons_LEDs.py", line 13, in
File "/lib/arduino_alvik/arduino_alvik.py", line 292, in begin
Exception:
********** PLEASE UPDATE ALVIK FIRMWARE (required: 1.1.1)! Check documentation ********** ”

I updated the Alvik using online Arduino Alvik Updater.

But the issue still remains the same!

Thank you in advance for any feedback!

Thank you.

Further to my last post about the Alvik, please see below the part of the code that is not working when the Reset button on the Micropython Lab is pressed.

if (alvik.get_touch_right()):
r = 1

g = 0

b =0
alvik_LR_LED(r,g,b)

Hi all again,

I was able to check the Alvik firmware version and it is 1.1.1.

but I have an issue with the part of the code and Micropython lab.

The code is given below.

 if (alvik.get_touch_right()): 
    r = 1
    g = 0
    b = 0
    alvik.left_led.set_color(r, g, b)
    R_b_pressed = True

when upload the program using ‘main.py” it worked. But If I reset by pressing the reset button on Micropython lab the Alvik led does not work. If I replace the alvik.left_led.set_color(r, g, b) , (in the code) with one of the NanoESP32 LEDs it is working even after resetting or without the USb cable connected to the laptop.

Any suggestions or comments for this?

Cheers

Please post the full script that you are testing

Hi @UKHeliBob ,

Sorry for the delay in replying.

Please find the attached two codes.
The primary sketch (main routine with the name “alvik_Test”) and the module that imported to the primary sketch (subroutine with the name “blinkRGBleds”).

I found the reason for not activating Alvik LEDS once the reset button on Micropython lab is pressed. It was because of the missing “Alvik.begin()” in the imported module (“blinkRGBleds”)

alvik_Test:

# Author: H.N.J. Fernando

from arduino_alvik import ArduinoAlvik
from time import sleep
import time
import sys

#To use onboard led
from machine import Pin

alvik = ArduinoAlvik()
alvik.begin()

from arduino_alvik import ArduinoAlvik
from time import sleep  
import time
import sys 

alvik = ArduinoAlvik()
alvik.begin()

#To use onboard leds
from machine import Pin

import blinkRGBleds

start_time_Y = time.ticks_ms()
time_period = 500 #ms

# RGB LED: 
# Green LED:
green_led =  Pin(0, Pin.OUT)

# Yellow LED:
yellow_led = Pin(48, Pin.OUT)

def Blink_Yellow_Led():
  # To toggle the yellow LED on and off:
  yellow_led(not yellow_led.value())

r = 0  # Alvik Red-LED value
g = 0  # Alvik Green-LED value
b = 0  # Alvik Blue-LED value

def alvik_LR_LED(r,g,b):
  alvik.left_led.set_color(r, g, b)
  alvik.right_led.set_color(r, g, b)

# Turn RGB Green-LED off:
green_led.value(1)

Lbutton_pressed = False
Rbutton_pressed = False
LED_select = 0 

while True:

  current_time_Y = time.ticks_ms()
  elapsed_time = time.ticks_diff(current_time_Y, start_time_Y)
  green_led.value(1)

  # Blink ESP32 Yellow-LED:
  if elapsed_time >= time_period:
    Blink_Yellow_Led()
    start_time_Y = current_time_Y

  if (alvik.get_touch_left()):
    LED_select = 2
    Lbutton_pressed = True
  
  # Momentarily Activate ESP-32 green-LED and turn off:
  if Lbutton_pressed :
    blinkRGBleds.blinkRGB_LED(LED_select)
    Lbutton_pressed = False

  if (alvik.get_touch_right()):
    Rbutton_pressed = True

  # Turn on Alvik Blue-LEDs
  if Rbutton_pressed:
    b = 1
    alvik_LR_LED(r,g,b)

  

blinkRGBleds:

# Author: H.N.J. Fernando

from arduino_alvik import ArduinoAlvik
from time import sleep
import time
import sys

#To use onboard led
from machine import Pin

alvik = ArduinoAlvik()
alvik.begin()

# RGB LED: Blue LED:
blue_led = Pin(45, Pin.OUT)
# Red LED:
red_led =  Pin(46, Pin.OUT)
# Green LED:
green_led =  Pin(0, Pin.OUT)

# Blink RGB LEDs
def blinkRGB_LED(counter):
  # Blink red LED: 
  if (counter == 1 ):
     # toggle LED:
     red_led(not red_led())
   
  # Blink green LED: 
  if (counter == 2 ):
     # toggle LED:
     green_led(not green_led())
  
  # Blink blue LED: 
  if (counter == 0 ):
     # toggle LED:
     blue_led(not blue_led())

It is working now after including alvik.begin().
Thank you for all feedbacks!

Cheers!