Coding ArtBots in Python vs Javascript

One of the challenges (and also benefits) of this project is that we had the option to code our ArtBots using either python, or javascript/blocks. We chose to use javascript/blocks in the makeCode editor because some of our group mates have interest in coding for web and javascript is a commonly used language for this purpose. However, we still took a look at how the python programming works and made some comparisons between the languages. 

For example, we wanted to make our ArtBot to slowly make a simple wavy line pattern. In python that might look like this:

from gigglebot import *
#this will set the speed of the motors to slow
set_speed(25,25)

#this loops through the code forever
while True:
  turn(LEFT,1000)
  turn(RIGHT,1000)

immediately we noticed some difference between this and the python we’re used to. We talked about how in JES (pretending there’s a gigglebot library) it might look more like this:

from gigglebot import*

def patternMaker():
  turn(LEFT,1000)
  turn(RIGHT,1000)

def setup():
  set_speed(25,25)
  patternMaker()

While our pseudocode might not be correct, we thought the organizational differences were interesting. To us it would make sense to create some sort of setup function that calls all of your pattern making functions and that sets an initial speed/direction for the ArtBot. And it makes sense to create those pattern making commands in their own function. We talked about how maybe a button press would look something like this:

from gigglebot import*

def patternMaker():
  turn(LEFT,1000)
  turn(RIGHT,1000)

def buttonPressedPattern():
  spin(RIGHT,500)
  drive(FORWARD,1000)

def buttonPress():
  #some sort of code taking information from the micro:bit
  #so that it knows when this button has been pressed
  #and then it calls buttonPressedPattern to draw the pattern
  #maybe it looks something like:
  #if button = pressed:
  #  buttonPressedPattern()
  
def setup():
  set_speed(25,25)
  patternMaker()
  buttonPress()
    

Again, this is just our groups version of pseudocode but thinking about the organization of code made us wonder how the organizational structure of javascript compares to both of these. Making a slow wavy line in Javascript (like our fist example)  would look like this:

basic.forever(function () {
    gigglebot.turnMillisec(gigglebotWhichTurnDirection.Right, 1000)
    gigglebot.turnMillisec(gigglebotWhichTurnDirection.Left, 1000)
})

We weren’t quite sure what the parameter “function()” meant but otherwise this felt similar to python and the organizational structure made just as much sense to us. We then tried adding a button press to this code. 

input.onButtonPressed(Button.A, function () {
    gigglebot.turnMillisec(gigglebotWhichTurnDirection.Right, 1000)
    gigglebot.turnMillisec(gigglebotWhichTurnDirection.Left, 1000)
})

The primary changes that happened were that the ” basic” line was changed to input which makes sense because we were switching from basic commands to using micro:bit inputs. Additionally, another parameter was added and this time it made sense. Button.A is an argument for the onButtonPressed function. You have to specify somehow which button is being pressed. 

As a group we agreed that having a base in python through this class made understanding javascript, block based coding, and micropython much easier. We could easily transition between the languages with few issues aside from small syntax errors like placing parentheses in the wrong places. However, we really thought the block based coding was fun and having big blocks of color/shapes behind the various parts of our code helped us understand how our code was organized and what the function of each piece was. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.