Arduino Alvik

How to write code for alternate Blinking LEDs with different colors in Arduino Alvik?

This entry is part 2 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):
  # Blink the two LEDs in Red for 5 times
  for _ in range(5):
      alvik.left_led.set_color(1, 0, 0)  # Turn on LEDs (Red)
      alvik.right_led.set_color(1, 0, 0)
      sleep_ms(500)  # Wait for 500 ms
      alvik.left_led.set_color(0, 0, 0)  # Turn off LEDs
      alvik.right_led.set_color(0, 0, 0)
      sleep_ms(500)  # Wait for 500 ms
  sleep_ms(1000)
  
  # Blink the two LEDs in Green for 5 times
  for _ in range(5):
      alvik.left_led.set_color(0, 1, 0)  # Turn on LEDs (Red)
      alvik.right_led.set_color(0, 1, 0)
      sleep_ms(500)  # Wait for 500 ms
      alvik.left_led.set_color(0, 0, 0)  # Turn off LEDs
      alvik.right_led.set_color(0, 0, 0)
      sleep_ms(500)  # Wait for 500 ms
  sleep_ms(1000)
  
  # Blink the two LEDs in Blue for 5 times
  for _ in range(5):
      alvik.left_led.set_color(0, 0, 1)  # Turn on LEDs (Red)
      alvik.right_led.set_color(0, 0, 1)
      sleep_ms(500)  # Wait for 500 ms
      alvik.left_led.set_color(0, 0, 0)  # Turn off LEDs
      alvik.right_led.set_color(0, 0, 0)
      sleep_ms(500)  # Wait for 500 ms
  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.

Series Navigation<< Arduino Alvik: How to write the blinking LED code?How to move Arduino Alvik forward and backward using simple code? >>
Dr. Dattaraj Vidyasagar
Dr. Dattaraj Vidyasagar

M.S. Electronics & Telecomm. (Cleveland Institute of Electronics, Ohio), Associate Member (IETE, Kolkata), Panelist on Dr. Homi Bhabha Foundation, Google certified educator (Level-1), Mentor of Change (MoC-1619) Niti Ayog, Government of India, International Robotics Trainer, Veteran of Applied Electronics since 35+ years.

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