setultrasonicpins()

Function Definition: setultrasonicpins(sensor_number = 1, trig_pin = "D1", echo_pin = "D2")

Description

The function initializes the ultrasonic sensor with specified echo and trig pins.

Parameters

NameTypeDescriptionExpected ValuesDefault Value
sensor_numberintThe specific ultrasonic sensor label.1 or 21
trig_pinstringThe specific GPIO pin where the trig pin of the ultrasonic sensor is connected."D1","D2","D3","A1","A2", "S1", or "S2""D1"
echo_pinstringThe specific GPIO pin where the trig pin of the ultrasonic sensor is connected."D1","D2","D3","A1","A2", "S1", or "S2""D2"

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

The example demonstrates the various animation of the Quarky LED display in the Python Coding Environment.

Code

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

quarky.showemotion('happy')
time.sleep(1)
quarky.showemotion('angry')
time.sleep(1)
quarky.showemotion('crying')
time.sleep(1)
quarky.showemotion('super angry')
time.sleep(1)
quarky.showemotion('surprise')
time.sleep(1)
quarky.showemotion('basic')
time.sleep(1)
quarky.showemotion('love')
time.sleep(1)
quarky.showemotion('nerd')
time.sleep(1)
quarky.showemotion('reject')
time.sleep(1)
quarky.showemotion('wave')
time.sleep(1)
quarky.showemotion('thinking')
time.sleep(1)
quarky.showemotion('giggle')
time.sleep(1)
quarky.showemotion('disco')
time.sleep(1)

Output

The example demonstrates the various animation of the Quarky LED display in the Python Coding Environment.

Code

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

quarky.showanimation('happy')
quarky.showanimation('happy')

quarky.showanimation('nerdy')
quarky.showanimation('nerdy')

quarky.showanimation('thinking')
quarky.showanimation('thinking')

quarky.showanimation('angry')
quarky.showanimation('angry')

quarky.showanimation('contempt')
quarky.showanimation('contempt')

quarky.showanimation('blink')
quarky.showanimation('blink')

quarky.showanimation('fear')
quarky.showanimation('fear')

quarky.showanimation('surprise')
quarky.showanimation('surprise')

quarky.showanimation('wink')
quarky.showanimation('wink')

quarky.showanimation('wave')
quarky.showanimation('wave')

quarky.showanimation('crying')
quarky.showanimation('crying')

Output

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

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)
The example displays how we can display a custom pattern on the matrix by making a script to display a Traffic Light in the Python Coding Environment.

Code

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

import time

quarky.setbrightness(15)

while True:
	quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
	time.sleep(5)
	quarky.showtext("2", [255,233,0])
	time.sleep(1)
	quarky.showtext("1", [255,233,0])
	time.sleep(1)
	quarky.drawpattern("ccccccccccccccccccccccccccccccccccc")
	time.sleep(5)
	quarky.showtext("2", [255,233,0])
	time.sleep(1)
	quarky.showtext("1", [255,233,0])
	time.sleep(1)

Output

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

The example demonstrates how to display scrolling text to make a name badge in the Python Coding Environment.

Code

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

while True:
	quarky.showanimation("blink")
	quarky.showanimation("blink")
	quarky.showscrollingtext("Quarky", 2, [0, 255, 0])
	quarky.showscrollingtext("Robotics", 2, [0, 0, 255])

Output

The example demonstrates how to control the motion of the robot using keyboard keys.

Script

Output

Wirelessly-Controlled-Robot-1

Make-a-Square (1)
The example demonstrates how to make a square with Quarky robot.

Script

Note: You have to change the left turn time to make sure that the robot turns 90 degrees.

Output

Make-a-Square

The example demonstrates how to make the robot go in a circle with different motor speeds.

Script

Output

Robot Forward
The example demonstrates how the robot move forward, backward, left and right using motor direction control.

Forward

Robot Forward

Backward

Robot Backward

Left

Left Robot

Right

Right Robot

The example demonstrates how to make a vertical robot pet that senses the hand on the IR sensor and acts accordingly.

Script

Alert: You need to calibrate the IR sensor values to make this program run perfectly.

Output

The example demonstrates how to make a line follower robot with Quarky.

Logic

Script

Alert: You need to calibrate the IR sensor to get the best line detection by the robot. Also, you need to calibrate the speeds to make the robot follow the line correctly.

Output

The example demonstrates how to make a simplified line following a robot with Quarky.

Script

Alert: You need to calibrate the IR sensor to get the best line detection by the robot. Also, you need to calibrate the speeds to make the F, T1 and T2 speeds for the robot to follow the line correctly.

Output

The example demonstrates how to make a delivery robot that follows the line and stops when it reaches checkpoint 1.

Script

Alert: You need to calibrate the IR sensor to get the best line detection by the robot. Also, you need to calibrate the speeds to make the robot follow the line correctly.

Output

 

The example demonstrates how to calibrate the servo motor with Quarky.

The purpose of servo motor calibration is to align the angle of your servo motor properly.

Connecting Servo to Quarky

The Servo motor will be connected to the Quarky Servo Connector. There are two servo ports on Quarky. Always make sure that brown wire is on your left side.

Script

Put the Ultrasonic Assembly on the servo shaft.

The example demonstrates how to code the Quarky to make the servo sweep the perimeter.

Script

Output

The example demonstrates how to make a vertical robot pet that senses the hand on the IR sensor and acts accordingly in the Python Coding Environment.

Code

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

quarky.setorientation(2)
quarky.setirthreshold("IRL", 3000)
quarky.setirthreshold("IRR", 3000)

while True:
  if quarky.getirstate("IRL"):
    if quarky.getirstate("IRR"):
      quarky.cleardisplay()

    else:
      pass
      quarky.runrobot("LEFT", 100)
      time.sleep(0.3)
      quarky.stoprobot()

  else:
    pass
    if quarky.getirstate("IRR"):
      quarky.runrobot("RIGHT", 100)
      time.sleep(0.3)
      quarky.stoprobot()

    else:
      pass
      quarky.showemotion("happy")
      quarky.playsounduntildone("QuarkyIntro")

Output

The example demonstrates how to make a delivery robot that follows the line and stops when it reaches checkpoint 1 in the Python Coding Mode.

Code

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

cards.video("on", 0)
cards.enablebox()
cards.setthreshold(0.5)

quarky.setirthreshold("IRL", 3000)
quarky.setirthreshold("IRR", 3000)
quarky.initializelinefollower(35, 40, 10)

quarky.drawpattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

while True:
  if not (quarky.getirstate(35) and quarky.getirstate(34)):
    quarky.dolinefollowing()

  else:
    quarky.stoprobot()
    cards.analysecamera()

    if cards.isnumberdetected(1):
      quarky.drawpattern("ccccccccccccccccccccccccccccccccccc")
      break

    quarky.runrobot("FORWARD", 40)

Output

The example demonstrates how to code the Quarky to make the servo sweep the perimeter in the Python Coding Environment.

Code

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

Angle = 0
while True:
  for i in range(0, 18):
    Angle += 10
    moveservo("Servo 1", Angle)
    time.sleep(0.01)

  for i in range(0, 18):
    Angle += -10
    moveservo("Servo 1", Angle)
    time.sleep(0.01)

Output

The example demonstrates how to make a line follower robot with Quarky in the Python Coding Environment.

Code

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

# User Defined Functions
def LED_Feedback():
  if quarky.getirstate("IRL"):
    quarky.setled(1, 1, [42, 255, 0], 100)

  else:
    pass
    quarky.setled(1, 1, [255, 0, 0], 100)

  if quarky.getirstate("IRR"):
    quarky.setled(7, 1, [42, 255, 0], 100)

  else:
    pass
    quarky.setled(7, 1, [255, 0, 0], 100)


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

while True:
  LED_Feedback()
  if (quarky.getirstate("IRL") and quarky.getirstate("IRR")):
    quarky.stoprobot()

  else:
    pass
    if quarky.getirstate("IRL"):
      quarky.runmotor("R", "FORWARD", 40)
      quarky.runmotor("L", "BACKWARD", 25)

    else:
      pass
      if quarky.getirstate("IRR"):
        quarky.runmotor("L", "FORWARD", 40)
        quarky.runmotor("R", "BACKWARD", 25)

      else:
        pass
        quarky.runrobot("FORWARD", 40)

Output

The example demonstrates how to make a simplified line following a robot with Quarky in the Python Coding Environment.

Code

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

quarky.setirthreshold("IRL", 3000)
quarky.setirthreshold("IRR", 3000)
quarky.initializelinefollower(35, 40, 10)

while True:
  if not (quarky.getirstate(35) and quarky.getirstate(34)):
    quarky.dolinefollowing()

  else:
    quarky.stoprobot()

Output

The example demonstrates how to make the robot go in a circle with different motor speeds in the Python Coding Environment.

Code

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

quarky.runmotor("L", "FORWARD", 20)
quarky.runmotor("R", "FORWARD", 100)

Output

The example demonstrates how to calibrate the servo motor with Quarky in Python Coding Environment.

The purpose of servo motor calibration is to align the angle of your servo motor properly.

Connecting Servo to Quarky

The Servo motor will be connected to the Quarky Servo Connector. There are two servo ports on Quarky. Always make sure that brown wire is on your left side.

Code

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

quarky.moveservo("Servo 1", 90)

 

Put the Ultrasonic Assembly on the servo shaft.

The example demonstrates how to control the motion of the robot using keyboard keys in the Python Coding Environment.

Code

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

while True:
  
  if sprite.iskeypressed("up arrow"):
    quarky.runrobot("FORWARD", 50)
  elif sprite.iskeypressed("down arrow"):
    quarky.runrobot("BACKWARD", 50)
  elif sprite.iskeypressed("left arrow"):
    quarky.runrobot("LEFT", 50)
  elif sprite.iskeypressed("right arrow"):
    quarky.runrobot("RIGHT", 50)
  else:
    quarky.stoprobot()

Output

Robot Forward
The example demonstrates how the robot moves forward, backward, left, and right using motor direction control in the Python Coding Environment.

Code

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

# imported modules
import time

# User Defined Functions
def Forward():
  quarky.runmotor("L", "FORWARD", 100)
  quarky.runmotor("R", "FORWARD", 100)
  time.sleep(1)
  quarky.stopmotor("L")
  quarky.stopmotor("R")

def Backward():
  quarky.runmotor("L", "BACKWARD", 100)
  quarky.runmotor("R", "BACKWARD", 100)
  time.sleep(1)
  quarky.stopmotor("L")
  quarky.stopmotor("R")

def Left():
  quarky.runmotor("L", "BACKWARD", 100)
  quarky.runmotor("R", "FORWARD", 100)
  time.sleep(1)
  quarky.stopmotor("L")
  quarky.stopmotor("R")

def Right():
  quarky.runmotor("L", "FORWARD", 100)
  quarky.runmotor("R", "BACKWARD", 100)
  time.sleep(1)
  quarky.stopmotor("L")
  quarky.stopmotor("R")

Forward()
Backward()
Left()
Right()

Output

Robot Forward

Robot BackwardLeft RobotRight Robot

Make-a-Square (1)
The example demonstrates how to make a square with Quarky robot.

Code

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

# imported modules
import time

for i in range(0, 4):
  quarky.runrobot("FORWARD", 100)
  time.sleep(1)
  quarky.stoprobot()
  quarky.runrobot("LEFT", 100)
  time.sleep(1.2)
  quarky.stoprobot()

Output

Make a Square

Buttons
The example demonstrates how to make the sprite movement with Quarky buttons.

Script

Output

Buttons

Buttons
The example demonstrates using the Quarky button to control the sprite using the hat block.

Script

Output

Buttons

All articles loaded
No more articles to load
Table of Contents
[PythonExtension]

Get in Touch!