Table of Contents

runmotor()

Function Definition: runmotor(motor = "L", direction = "FORWARD", speed = 100)

Parameters

NameTypeDescriptionExpected ValuesDefault Value
motorstringThe motor which you want to control."L" or "R""L"
directionstringThe direction in which you want to make the motor rotate."FORWARD" or "BACKWARD""FORWARD"
speedintThe speed of the motor.0 to 100100

Description

The function sets the specified motor of the Quarky robot to the specified direction (“FORWARD” or “BACKWARD”) and specified speed.

Example

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

Read More
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

Read More
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

Read More
Learn how to set up a flame sensor with Quarky, calibrate it, and create a fire alarm system with a motor fan, door servo motor, and IFTTT webhooks. Get step-by-step instructions, circuit diagrams, python code, and more.

This example demonstrates how to set up the flame sensor with Quarky to detect heat or flame nearby. Later, we create an alarm system triggered with the flame sensor.

Flame Sensor Connection to Quarky

Flame sensors have 4 pins: GND, VCC, DO, and AO. You have to connect the following 3 pins to the Quarky Expansion Board:

  1. GND to Ground Pin of Quarky Expansion Board
  2. VCC to 3.3V or VCC Pin of Quarky Expansion Board
  3. DO to the D3 (Digital Pin) of the Quarky Expansion Board

Calibrating Flame Sensor

The sensor also has 2 LEDs – Power and Detection. The desired calibration is achieved when the sensor is inactive when there is no heat or flame nearby and active when the flame is nearby. It is visible on the detection LED.

To calibrate the flame sensor:

  1. Turn the power on for the sensor.
  2. Place the sensor close to the heat or flame. You should see the detection LED turn on.
  3. If the LED is Off, adjust the potentiometer until the detection LED turns on.
  4. Move the sensor away from the heat or flame. The detection LED should turn off.
  5. If the detection LED does not turn off, continue to adjust the potentiometer until it does.

Project: Flame-based Alarm System

In the project, when heat or flame is detected, the alarm system starts with

  1. The fan turned ON.
  2. Quakry beeping with lights.
  3. The door is open for urgent evacuation.

The alarm system will be on until the flame sensor stops detecting the fire.

Circuit

Connect the following modules to the Quarky Expansion Board:

  1. Flame Sensor
    1. GND to Ground Pin of Quarky Expansion Board
    2. VCC to 3.3V or VCC Pin of Quarky Expansion Board
    3. DO to the D3 (Digital Pin) of the Quarky Expansion Board
  2. Motor Fan: Connect the motor to the Motor Port 1 of the Quarky Expansion Board.
  3. Door Servo Motor: Connect the servo motor to the servo port 5 of the Quarky Expansion Board.

Python Code

  1. The code first creates objects of the Quarky, Expansion, and IoTHouse classes.
  2. It then moves the servo connected to pin 5 to 100 degrees and stops the motor connected to pin 1.
  3. Then it defines two functions, fireDetectedSequence() and fireStopSequence().
    1. When the fire is detected, it moves the servo connected to pin 5 to 0 degrees, runs the motor connected to pin 1 in clockwise direction with speed 100, plays a tone at C4 pitch for 8 beats, and draws a red pattern on the Quarky display.
    2. When the fire is no longer detected, it stops the motor connected to pin 1, clears the display of the Quarky robot, and moves the servo connected to pin 5 to 100 degrees.
  4. In the while loop, the code keeps on checking the flame status of pin D3 in the house.
    1. If the flame is detected, it waits for two seconds and again checks the flame status. If the flame is still detected, it runs the fireDetectedSequence() and then runs the fireStopSequence().
# The following code is written to detect a fire in a house using a Quarky robot and Expansion board.
# quarky is an instance of the Quarky class which has functionalities like playing a tone and drawing a pattern on LED Screen
quarky = Quarky()
# quarkyexpansion is an instance of the Expansion class which has functionalities like moving a servo and running a motor
quarkyexpansion = Expansion()
# house is an instance of the IoTHouse class which has functionalities like checking the flame status
house = IoTHouse()
# import time library which has functionalities like sleeping for a certain amount of time
import time

# move the servo connected to pin 5 to 100 degrees
quarkyexpansion.moveservo(5, 100)
# stop the motor connected to pin 1
quarkyexpansion.stopmotor(1)


# define a function which initiate instructions when the flame is detected
def fireDetectedSequence():
  # move the servo connected to pin 5 to 0 degrees
  quarkyexpansion.moveservo(5, 0)
  # run the motor connected to pin 1 in clockwise direction with speed 100
  quarkyexpansion.runmotor(1, 1, 100)
  # keep on checking the flame status of pin D3 in the house, until it is no longer in flame
  while not (house.flamestatus("D3")):
    # clear the display of the Quarky robot
    quarky.cleardisplay()
    # play a tone at C4 pitch for 8 beats
    quarky.playtone("C4", 8)
    # draw red pattern on the Quarky display
    quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
    time.sleep(0.7)


# define a function which is initiated when no flame is detected
def fireStopSequence():
  # move the servo connected to pin 5 to 100 degrees
  quarkyexpansion.moveservo(5, 100)
  # stop the motor connected to pin 1
  quarkyexpansion.stopmotor(1)
  # clear the display of the Quarky robot
  quarky.cleardisplay()


while True:
  # keep on checking the flame status of pin D3 in the house
  if not (house.flamestatus("D3")):
    time.sleep(2)
    # again check the flame status of pin D3 in the house
    if not (house.flamestatus("D3")):
      # if flame is detected, run the fireDetectedSequence()
      fireDetectedSequence()
      # and then run the fireStopSequence()
      fireStopSequence()

Adding IoT in Fire Alarm System

As an advanced system, we can also send the fire detection alert to the users using IFTTT. For that, we will use IFTTT webhooks.

The following IFTTT sequence is to be created:

You can learn in detail how to create an IFTTT applet here: https://ai.thestempedia.com/extension/ifttt-webhooks/

Code

This code is the continuation of the past code:

  1. The code will continuously check the flame status of pin D3 in the house.
  2. If the flame is detected, it will initiate the fireDetectedSequence() which will send an event to IFTTT Webhooks, move the servo connected to pin 5 to 0 degrees, run the motor connected to pin 1 in the clockwise direction with speed 100, clear the display of the Quarky robot, and play a tone at C4 pitch for 8 beats.
  3. Once the flame status is no longer detected, it will initiate the fireStopSequence() which will send an event to IFTTT Webhooks, move the servo connected to pin 5 to 100 degrees, stop the motor connected to pin 1, and clear the display of the Quarky robot.
# The following code is written to detect a fire in a house using a Quarky robot and Expansion board.
# quarky is an instance of the Quarky class which has functionalities like playing a tone and drawing a pattern on LED Screen
quarky = Quarky()
# quarkyexpansion is an instance of the Expansion class which has functionalities like moving a servo and running a motor
quarkyexpansion = Expansion()
# house is an instance of the IoTHouse class which has functionalities like checking the flame status
house = IoTHouse()
# import time library which has functionalities like sleeping for a certain amount of time
import time
#Create an instance of the IFTTTWebhooks library
ifttt = IFTTTWebhooks()

# move the servo connected to pin 5 to 100 degrees
quarkyexpansion.moveservo(5, 100)
# stop the motor connected to pin 1
quarkyexpansion.stopmotor(1)

#Set the webhook key and event name
ifttt.setifttt("Flame_Detected", "iNyFg77wDLYV-V9UtdXVtmeebiOw_72LjxZud084ybr")

# define a function which initiate instructions when the flame is detected
def fireDetectedSequence():
  #Set the message and priority
  ifttt.setvalues("Fire Started! Evacuation Started", 1)
  ifttt.triggerevent() #Send the event
  # move the servo connected to pin 5 to 0 degrees
  quarkyexpansion.moveservo(5, 0)
  # run the motor connected to pin 1 in clockwise direction with speed 100
  quarkyexpansion.runmotor(1, 1, 100)
  # keep on checking the flame status of pin D3 in the house, until it is no longer in flame
  while not (house.flamestatus("D3")):
    # clear the display of the Quarky robot
    quarky.cleardisplay()
    # play a tone at C4 pitch for 8 beats
    quarky.playtone("C4", 8)
    # draw red pattern on the Quarky display
    quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
    time.sleep(0.7)


# define a function which is initiated when no flame is detected
def fireStopSequence():
  #Set the message and priority
  ifttt.setvalues("Fire Stopped", 1)
  ifttt.triggerevent() #Send the event
  # move the servo connected to pin 5 to 100 degrees
  quarkyexpansion.moveservo(5, 100)
  # stop the motor connected to pin 1
  quarkyexpansion.stopmotor(1)
  # clear the display of the Quarky robot
  quarky.cleardisplay()


while True:
  # keep on checking the flame status of pin D3 in the house
  if not (house.flamestatus("D3")):
    time.sleep(2)
    # again check the flame status of pin D3 in the house
    if not (house.flamestatus("D3")):
      # if flame is detected, run the fireDetectedSequence()
      fireDetectedSequence()
      # and then run the fireStopSequence()
      fireStopSequence()
Read More
All articles loaded
No more articles to load