Table of Contents

setled()

Function Definition: setled(x_position = 1, y_position = 1, color = [0, 0 , 0], brightness = 20)

Parameters

NameTypeDescriptionExpected ValuesDefault Value
x_positionintX position of the LED.1, 2, 3, 4, 5, 6, or 71
y_positionintY position of the LED1, 2, 3, 4, or 51
colorlistThe color list with following values R - 0-255, G - 0-255, and B - 0-255[R, G, B][0, 0 , 0]
brightnessintThe brightness value of the LED0 to 10020

Description

The function sets the specified RGB LED of Quarky (specified with X and Y position of the LED) to the specified RGB color and brightness value.

Example

The example demonstrates how to calibrate the IR sensors to detect black lines on the white surface in Python Coding Environment.

Logic

An IR sensor consists of 2 LEDs: one which transmits the IR light and one which receives the IR light. When the IR rays are transmitted, they bounce from the nearest surface and get back to the receiver LED. That’s how an IR sensor detects an object.

But to detect colors, we depend on the number of rays the surface reflects:

  1. The dark surface will absorb more IR rays and as a result, the receiver will get fewer IR rays.
  2. White or shiny objects will absorb fewer IR rays and as a result, the receiver will get more IR rays.

We can get the sensor values in PictoBlox and based on that value we can estimate whether the surface is black or white.

  1. If the sensor detects the black line, its output value is increased. This means that the sensor is active.
  2. If it detects the white area, its output value decreases. This means that the sensor is inactive.

We will call the threshold value above which the sensor detects the black line. If the sensor value is less than the threshold, it means that the sensor hasn’t detected the line yet.

Alert: IR sensors DON’T work in sunlight. IR rays from the sun increase the overall threshold of the sensors therefore they stay active all time. A closed environment or nighttime is the place/time to work with your line following robot.

Code

sprite = Sprite('Tobi')

quarky = Quarky()

quarky.cleardisplay()
quarky.setirthreshold("IRL", 3000)
quarky.setirthreshold("IRR", 3000)

while True:
  if quarky.getirstate("IRL"):
    quarky.setled(1, 1, [0, 255, 0], 20)
  else:
    quarky.setled(1, 1, [255, 0, 0], 20)
  
  if quarky.getirstate("IRR"):
    quarky.setled(7, 1, [0, 255, 0], 20)
  else:
    quarky.setled(7, 1, [255, 0, 0], 20)

 

Calibrating the IR Sensors

Now, run the code by clicking the green flag and bringing the black line of the track close to the IR sensor. One of the following three conditions will happen:

  1. Your calibration value is HIGH: The black region will not be detected in this case. Reduce the calibration value.
  2. Your calibration value is LOW: The white region will not be detected in this case. Therefore, increase the calibration value.
  3. Your calibration value is OK. The white and black regions are detected accurately.

Now modify the script to add the detection value for the right IR sensor.

Output

Read More
The example demonstrates how to create a random colored LED pattern on Quarky in the Python Coding Environment.

Code

sprite = Sprite('Tobi')
quarky = Quarky()
import random

quarky.cleardisplay()

for x in range(1, 8):
  red = random.randrange(0, 255)
  green = random.randrange(0, 255)
  blue = random.randrange(0, 255)
  
  quarky.setled(x, 1, [red, green, blue], 100)

Output

Read More
The example demonstrates how to control the glowing LED using the keyboard keys in the Python Coding Environment.

Code

sprite = Sprite('Tobi')
quarky = Quarky()
import time

curr_x = 4
curr_y = 3
brightness = 50

quarky.cleardisplay()
quarky.setled(curr_x, curr_y, (0, 255, 0), brightness)

while True:
  
  if sprite.iskeypressed("up arrow"):
    curr_y = curr_y - 1
    quarky.setled(curr_x, curr_y + 1, (0, 0, 0), brightness)
    time.sleep(0.2)
    quarky.setled(curr_x, curr_y, (0, 255, 0), brightness)

  if sprite.iskeypressed("down arrow"):
    curr_y = curr_y + 1
    quarky.setled(curr_x, curr_y - 1, (0, 0, 0), brightness)
    time.sleep(0.2)
    quarky.setled(curr_x, curr_y, (0, 255, 0), brightness)
    
  if sprite.iskeypressed("left arrow"):
    curr_x = curr_x - 1
    quarky.setled(curr_x + 1, curr_y, (0, 0, 0), brightness)
    time.sleep(0.2)
    quarky.setled(curr_x, curr_y, (0, 255, 0), brightness)
    
  if sprite.iskeypressed("right arrow"):
    curr_x = curr_x + 1
    quarky.setled(curr_x - 1, curr_y, (0, 0, 0), brightness)
    time.sleep(0.2)
    quarky.setled(curr_x, curr_y, (0, 255, 0), brightness)
  
  time.sleep(0.2)
Read More
The example demonstrates how to control the individual LEDs of the Quarky and run patterns using the loops in Python Coding Environment.

Code

sprite = Sprite('Tobi')
quarky = Quarky()

quarky.cleardisplay()
Y = 1
for i in range(0, 5):
  X = 1
  for i in range(0, Y):
    quarky.setled(X, Y, [101, 255, 0], 100)
    X += 1
  Y += 1

Output

Read More
All articles loaded
No more articles to load