Coding cyberbrick

I wanted to do some coding on my cyberbirck, but I’m not sure wether or not the code snippets will work. I need to do things like counting button presses, and variables that aren’t within just one code snippet. I was looking into coding it without the app, and got to a point where i can communicate with my board. But I’m not sure how to write files to it and if I would be able to easily share this on MW. Any help is very much appreciated, thank you!

1 Like

I highly suggest going over the CyberBrick Wiki and API documentation.

https://makerworld.com/en/cyberbrick/api-doc/

3 Likes

In addition to the advice above, I recommend doing a short tutorial on Python, running test code on your computer, and, for the issue with how to use variables, look into the ‘Scope of Variables’ which will explain how the scope of variables will affect which variables you can use and when.

2 Likes

I’ve read both the wiki and cyberbrick micropython documentation and can code python just fine. I couldn’t find much on the code snippets, and it’s quite complicated to get new files on the board without the app. I basically just wanted to know if you are able to have global variables that stay troughout all code snippets.

So you can use variables that stay troughout different code snippets? I’m just not sure how they work.

1 Like

Code snippets don’t seem to allow global variables.
I tested it with this, activating the code on button press:

motors = MotorsController()
if ‘time’ not in globals():
global time
time = 0
time += 1
motors.set_speed(1, 2048)
time.sleep(time)
motors.stop(1)

It always just runs 1 second and stops.

1 Like

Yes, you can use Global Variables.

The glib answer is to follow @Cyberbrick’s tutorial on how to do this… but, of course, THAT doesn’t exist. So…

You need to use tools like Thonny or Arduino Labs for Micropython to access the Core’s memory and insert your own python code into the code that is documented in the Wiki mentioned above.

1 Like

Code snippets SHOULD allow you to access globals defined OUTSIDE of the code snippet. Declaring a non-existent variable Global within a code snippet doesn’t do much good. Admittedly I have not used ‘Code Snippets’ so I don’t know all their limitations. I gave up on Cyberbrick’s ‘Infrastructure’ a long time ago.

1 Like

Adafruit has a bunch of information on CircuitPython that is probably directly applicable if folks are having issues finding examples and how-to. Adafruit has a bunch of boards and parts that can be added to CyberBrick too for those with particular needs.

https://learn.adafruit.com/search?q=circuitpython

2 Likes

Thx for all the responses! I already went trough a bit of the process of inserting my own code (I connected to the board with PuTTY as explained on the wiki), but i’m not sure how to actually send files and can’t find anything on how to do so.

1 Like

Wait… Is it as simple as just sending it with something like Arduino Lab?

2 Likes

I don’t have a CyberBrick to know for certain but it looked like the board is similar to a whole raft of ESP32 chip products and there’s a good chance other dev tools will work fine with it. Another issue could be libraries. Adafruit has a bunch but they may not be compatible with the CyberBrick environment.

On the other hand I can tell you from personal experience that you will limit who adopts your models if you go with other (even if better) development environments and code conventions.

The CyberBrick board looks a lot (but not exactly) like the QTPy ESP32 boards at Adafruit and there is a lot of how-to stuff over there. I don’t know it will work but I bet it would but there may be some forks along the way too.

Yeah, the advice on Adafruit relates to OPEN designs, not a closed one like Cyberbrick. Very little relevance and mostly a distraction if you want to stay with Cyberbrick ‘infrastructure’.

I disagree. The python is very similar if not identical. For those wanting to see how to do local vs global variables and such, there’s lots of examples. Just code diving to see how others do stuff doesn’t take installing the code or switching development environments. People here are saying they need references and information and there’s lots available there.

But for those not interested, nothing says you have to go there. It was just a resource. Use it or not as you wish.

Not sure if this is the right forum for this, but wanted to get some comments on this code I have been trying to use. any help or comments are appreciated since I am trying to use Global Variables.

import time

from cyberbrick_core.lib.ServosController import ServosController

from cyberbrick_core.lib import rc_module

servos = ServosController()

# Servo channels

LEFT_SERVO = 1

RIGHT_SERVO = 3

# Servo angles

LEFT_LOCKED = 20

LEFT_UNLOCKED = 100

RIGHT_LOCKED = 160

RIGHT_UNLOCKED = 80

# Unlock code

CODE = [2, 4, 1, 3]

progress = 0

last_buttons = [0, 0, 0, 0]

is_unlocked = False

# ---------- LED EFFECTS ----------

def led_green():

rc_module.set_value("Correct", 1)

def led_red():

rc_module.set_value("Wrong", 1)

# Use this for a quick single green flash after each correct button

def progress_flash():

rc_module.set_value("Correct", 1)

time.sleep_ms(150)

# ---------- LOCK CONTROL ----------

def lock_box():

global is_unlocked

servos.set_angle(LEFT_SERVO, LEFT_LOCKED)

time.sleep_ms(200)

servos.set_angle(RIGHT_SERVO, RIGHT_LOCKED)

is_unlocked = False

def unlock_box():

global is_unlocked

servos.set_angle(LEFT_SERVO, LEFT_UNLOCKED)

time.sleep_ms(200)

servos.set_angle(RIGHT_SERVO, RIGHT_UNLOCKED)

is_unlocked = True

led_green()

# ---------- BUTTON HANDLING ----------

def handle_press(btn):

global progress

if is_unlocked:

    return

if btn == CODE\[progress\]:

    progress += 1

    if progress == len(CODE):

        unlock_box()

        progress = 0

else:

    progress = 0

    led_red()

    if btn == CODE\[0\]:

        progress = 1

# Start locked

lock_box()

while True:

vals = rc_module.get_values()

buttons = vals\[6:10\]  # Button1..Button4

\# Lock again if Button1 + Button4 pressed together

if is_unlocked:

    if buttons\[0\] and buttons\[3\]:

        lock_box()

        time.sleep_ms(300)

\# Detect new presses

for i in range(4):

    if buttons\[i\] and not last_buttons\[i\]:

        handle_press(i + 1)

last_buttons = buttons\[:\]

time.sleep_ms(50)

What is the trigger for this code to run? There’s lots I don’t know about CyberBrick but if you’re staying in the official deployment path, I believe that code is executed in response to an event

The 4 button sequence is the input to unlock the two servos which are locking a container. Pushing he buttons in the right sequence will move the servos to the unlock position. Well that is the intent. A green flash froM the LED if correct button was pouch each time. If a wrong button pushed sequence starts over and red LED flashes.

But doesn’t seem to load. If I just load a test program to use the code to move the two servos, in port 1 & 3 to 90deg and 180 deg respectfully. It works.

I’d love to see if you get this working! I’ve been extremely cornfused with the lack of info regarding Cyberbrick coding.

1 Like

Well so far I have not been successful. I even eliminated the global parameters. It doesn’t seem to recognize the buttons. I can get small programs to work with servos and LED’s but not the buttons.

Very little information on the structure needed. I don’t think they have full python accepted by CyberBrick.

I am still working on it but might take some time.

:frowning:

1 Like

What is the process you are using when you mentioned “load a test program”? I’m just wanting to be sure that I understand how you are approaching it.

2 Likes