Lesson 4: Fun with Lights

Lesson Description

In this lesson, learn how to use Quarky’s tactile switches to create a lamp & traffic light. You will understand the basics of the forever block and conditional statements and use them to write your own code.

Quarky Tactile Switch

Quarky has 2 tactile switches or buttons which allow Quarky to take input from the user.

You can get the state of the button in PictoBlox using is button () pressed? block. The block reports 1 when the switch is pressed and 0 when the switch is not pressed.

Forever Block

In the forever block, the blocks held inside will be in a loop — just like the repeat () block, except that the loop never ends (unless the stop sign is clicked, the Stop All block is activated, or the stop script block is activated within the loop). Due to this infinite loop, the block has no bump at the bottom; having a bump would be pointless, as the blocks below it would never be activated.

Condition Statement Blocks

Conditional statements allow the program to check the conditions by testing a variable against a value and acting accordingly. A program that has conditional statements is called a Conditional Program, and the process is known as Conditional Programming.

  • if () then blockThe if () then block will check whether the specified condition is true or not. If it is true, the blocks inside it will run, and then the script involved will continue. If the condition is false, the code inside the block will be ignored and the script will move on. The condition is checked only once.
  • The if () then else block will check whether the specified condition is true or not. If the condition is true, the code held inside the first C (below the if arm) will run. Then, the script will continue; if the condition is false, the code inside the second C (below the else arm) will run. (Unlike the if () then block).

Activity 1: Lamp with Quarky

Let’s make a lamp with Quarky. The logic is simple – if we press the left switch, L, the lamp should turn OFF. If we press the right switch, R, the lamp should turn ON.

 

Coding Steps

Follow the steps below:

  1. Open PictoBlox App, go to My Space, and click on + button to create a new file.
  2. Connect Quarky to PictoBlox.
  3. Go to the Sensors palette and add a is button () pressed? block into the scripting area. The button, L, is set as the default option.
  4. To check if Lis pressed, we’ll use an if block. Go to the Control palette and drop an if block into the scripting area. Drop the is button () pressed? block in the white space of the if block.
  5. If Lis pressed, then, according to our logic, the LEDs should turn OFF. Therefore, go to the Display palette and drop a clear screen block inside the if.
  6. However, if R is pressed, then the LED should turn ON. Therefore, add another if block below the first one and drop a is button () pressed. block in the white space.
  7. Then, go to the Display palette and drop display matrix as () block inside the if block, and set all the LEDs as white.
  8. Add a forever block around the entire script.
  9. To add the finishing touch, add a when flag clicked block from the Events palette above the forever block.
  10. Finally, click the green flag to run the script. Press the L switch to turn ON the lights and R to turn OFF the lights.
  11. Save the file as Lamp.

Activity 1: Output

Activity 2: Traffic Light with Quarky

In this activity, we’re going to see how we can display a custom pattern on the matrix by making a script to display a Traffic Light.

Coding Steps

Follow the steps below:

  1. Open PictoBlox App, go to My Space and click on + button to create a new file.
  2. Connect Quarky to PictoBlox.
  3. Drag and drop a when flag clicked block into the scripting area from the Events palette. 
  4. Below the when flag clicked block, add the set display brightness to () block. In the space write 15.
  5. Add a forever block from the Control palette below the set display brightness to () block.
  6. Inside the forever block, add a display matrix as () block from the Display palette. Fill the matrix with red color.
  7. Add wait () seconds block under the display matrix as () block and set the waiting time as 5 seconds.
  8. Go to the Display extension and add a display () with () block into the scripting area. In the space, enter 2. Then, set the color of the LED to yellow-color (you can always choose the color of your choice) from the drop-down and this will display number 2 in yellow color.
  9. Add another wait () seconds block under the display () with () block and set the waiting time as 1 seconds.
  10. Now add another display () with () block below the wait () seconds In the space, enter 1. Then, set the color of the LED to yellow-color from the drop-down and this will display number 1 in yellow color.
  11. Add another wait () seconds block under the display () with () block and set the waiting time as 1 seconds.
  12. Right-click on the display matrix as () block and duplicate the code. Set the color as Green in display matrix as () block.
  13. The code is complete. Click on the green flag and test the code. You will see Quarky running like a Traffic Light.
  14. Save the file as Traffic Light.

Activity 2: Output

Make sure you finish and submit the assignment, as well as take the quizbefore moving on to the next lesson.

Table of Contents