Arduino Alvik

How to move Arduino Alvik forward and backward using simple code?

This entry is part 3 of 9 in the series Arduino Alvik

If you are already familiar with Arduino Alvik environment, then you can proceed with the following post. However, if you are new to Arduino Alvik, then we suggest you to read this post on Arduino Alvik basics first!

As we know there are two on board multicolor LEDs on top of the Arduino Alvik. Let us see the following code to blink these LEDs alternately by changing their color.

The following code is freely available for logged in users.

from arduino_alvik import ArduinoAlvik
from time import sleep_ms

# Initialize the Alvik robot
alvik = ArduinoAlvik()
alvik.begin()

# Make the two LEDs off initially
alvik.left_led.set_color(0, 0, 0)
alvik.right_led.set_color(0, 0, 0)

while True:
  # Move forward
  alvik.set_wheels_speed(50, 50)  # Both motors forward at speed 50
  alvik.left_led.set_color(1, 0, 0)  # Red LED glows to indicate forward motion
  alvik.right_led.set_color(1, 0, 0) # Red LED glows to indicate forward motion
  sleep_ms(1000)  # Move forward for 2 seconds

  # Stop for 1 second
  alvik.brake() # note this special function
  alvik.left_led.set_color(0, 0, 0)  # Turn off all the LEDs
  alvik.right_led.set_color(0, 0, 0) # Turn off all the LEDs
  sleep_ms(1000)

  # Move backward
  alvik.set_wheels_speed(-50, -50)  # Both motors backward at speed -50
  alvik.left_led.set_color(0, 1, 0)  # Green LED glows to indicate backward motion
  alvik.right_led.set_color(0, 1, 0) # Green LED glows to indicate backward motion
  sleep_ms(1000)  # Move backward for 1 seconds

  # Stop for 1 second
  alvik.brake()
  alvik.left_led.set_color(0, 0, 0)  # Turn off all the LEDs
  alvik.right_led.set_color(0, 0, 0) # Turn off all the LEDs
  sleep_ms(1000)

Procedure to use the code in Arduino Alvik

If you are new to Arduino Alvik, the read this step-by-step procedure to create new file in Arduino Lab software and then run the code given above.

Share on your network!
Prof. Dattaraj Vidyasagar
Prof. Dattaraj Vidyasagar

Author on this website. He is veteran of Core Electronics since last 36+ years. ATL Mentor of Change, Niti Ayog, Govt. of India, Google Certified Educator, International Robotics Trainer and author of 18 books on electronics, robotics, programming languages and web designing... ➤➤

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x