Archives

News

BirdBlox Maze Game

Hummingbird Setup

  1. Remove the circular plastic horns from each servo. Replace one with the large red flower-shaped horn and the other with the white “+” shaped horn. Do not replace the screws, as we may need to readjust the horns, and the friction between the horn and the servo should be enough for our purposes.
  2. Create a base for your robot by attaching a block to a flat surface or board. The block will hold up the servos, which will tilt the maze.
  3. Attach the two servos together at a right angle as shown above using rubber bands, tape, or hot glue.
  4. Mount the servos on the block so that both servos can turn freely.
  5. Construct a simple maze out of paper and cardboard for the ball to move through. There will be some lag when controlling the robot, so don’t make the maze too difficult.
  6. Place an upward-facing distance sensor at the start of the maze to detect the ball.
  7. Likewise, add a light sensor to the end of your maze. You could instead make a receiving container with the light sensor inside it as shown below:
  8. Attach the maze to the second servo. You will need a right-angled piece of cardboard/plastic to do this. You may find it easier to remove the horn from the servo and attach that to the maze.
  9. Connect the servo with the “+” shaped horn to servo port 1 and the servo with the flower-shaped horn to servo port 2.
  10. Attach the distance sensor to sensor port 1.
  11. Attach the light sensor to sensor port 2.
  12. Decorate the maze with 2 tri-color LEDs, which should be attached to tri-color ports 1 and 2.Now you are ready to begin programming!

Programming

NOTE: While programming, you should detach the “+” horn (and the maze attached to it) from its servo. This will prevent the maze from being damaged if the servo moves too far.

  1. Turn on Bluetooth if it is off.
  2. Open the BirdBlox app.
  3. Connect to the Hummingbird.
  4. Start your program with a when flag clicked block and a repeat forever loop, both of which are from the Control category.
  5. We will be using the tablet’s accelerometer to control the two servos. Start by placing a Hummingbird Servo block within the forever loop.
  6. Acceleration values can be anywhere from around -10 to 10. However, servos do not accept negative values. We can resolve this by adding 90 to the value before we send it to the servo. Head to the Operators category and place an addition operator in the second slot of the servo block. Then type 90 into the second slot of the addition block. 90 gives the servo the maximum amount of room to move in each direction.
  7. A change from -10 to 10 (80 to 100 after adding 90) is small and will move the servo only a little. To resolve this, we will multiply the acceleration value by 3. Add a multiplication block (from Operators) to the first slot of the addition block. Then type 3 into the first slot of the multiplication block. Place a Device Acceleration block (Tablet menu) into the second slot of the multiplication block.
  8. If you have not already, lock your device’s orientation. The will prevent the screen from turning when you tilt the tablet.
  9. Run your program by tapping the green flag. When you tilt your tablet back and forth, the upper servo (the one normally attached to the maze) should move accordingly. If it moves in the opposite direction to the tablet, change the 3 in the multiplication block to -3. Stop your program when you are done testing by tapping the red stop sign.
  10. Now we just need to link the second servo to the y acceleration. Duplicate the Hummingbird Servo block by tapping and holding. Attach the copy below the original servo block. Change the port number from 1 to 2 and use the drop-down menu to replace the “X” with a “Y.”
  11. Test the program again. Tilt your tablet back, forth, left, or right and the servos should move accordingly. Again, you may have to reverse one of the servos by changing the corresponding multiplication block from 3 to -3.
  12. You maze is now playable! However, there are still more features we could add.Let’s make the robot blink the LEDs when we successfully move the ball to the end of the maze and cover the light sensor. We will repeatedly move the servos to match the tablet’s orientation just like we did before, but only until the light sensor is covered. Then we will blink the LEDs.
  13. Place a repeat until block from the Control category so that it is within the forever loop but above the first servo block. Then drag both servo blocks into the repeat until loop.
  14. The loop should repeat until the light sensor is covered and falls below a certain value. Drag a less than block from Operators into the slot on the repeat until block. Then place a Hummingbird Light block into the left slot of the less than block. Change the port of the light sensor block to port 2. Then type 50 into the right side of the less than block. You may have to adjust this value based on your lighting conditions.
  15. When the sensor is covered, the robot will put on a light show! To make the lights blink many times without using too many blocks, we will use the repeat block from the Controlcategory. Place this loop directly below the repeat until block. 10 is a little much, so change the value to 5.
  16. Add HB TRI-LED blocks to light up your robot. Be sure to use both LEDs.Here is the pattern we used:First we made LED 1 green and LED 2 blue.
    Then we reversed the colors, making LED 1 blue and LED 2 green.
    NOTE: If you are wondering why we did not add wait blocks in between the LED blocks, we made the program this way because the BirdBlox app automatically has a small amount of delay in between Hummingbird blocks. This delay is caused by the time needed to send commands via bluetooth.
  17. Once the pattern is done, the program should wait for the ball to be placed at the start of the maze before activating the servos again. For this purpose, add a wait until block from the Control category above the repeat until block.
  18. We want the program to wait until the distance sensor sees the ball (or if the ball is too small, until it sees a hand holding the ball). Add the blocks Hummingbird Distance CM < 20to the wait until block.
  19. Once the maze is activated and the game begins, we can change the color of the LEDs to alert the user that the game has started. We chose to make both LEDs red. These LED blocks should be inserted right below the wait until block.
  20. Finally, when the robot is idle and waiting for the ball to be placed at the start of the maze, the board should be level and the LEDs should be a different color to reflect the idle state. Place two LED blocks and two servo blocks (one for each port) above the wait until block to do this. Set the servos to 90 degrees, which should be level.
  21. It is finally time to reattach the maze to the robot! Tap the green flag and place the iPad level on a table. This will set both servos to 90 degrees. Then attach the maze and adjust the horns as needed so that the maze platform becomes level.
  22. Have fun playing your maze!
News

Snap! Combination Lock

Hummingbird setup

  1. Print out the dial template or design your own.
  2. Cut out the dial and glue or staple it to a piece of cardboard.
  3. Cut a small slit below the 5 that lines up with the bump on the rotation sensor.
  4. Attach the dial to the rotation sensor.
  5. Twist the rotation sensor counter-clockwise as far as it will go.
  6. Cut a small arrow shape out of cardboard and wedge it into the slit in the rotation sensor so that it points toward the word “Reset”.
  7. Connect the rotation sensor to sensor port 1 of the Hummingbird.
  8. Add a tri-color LED to port 1.
  9. Connect a servo to servo port 1.
  10. Plug the Hummingbird into a wall socket and into your computer.

Measurements

  1. Select a three-digit combination to use with the lock. We chose 4-8-1.We want to detect when the arrow is pointed toward the correct number, but we also want to leave a little room for error. We need some way of measuring the rotation sensor’s output when the arrow is in the correct position.
  2. Drag a Hummingbird knob block on to the screen and set it to port 1.
  3. Twist the dial to the first digit of your combination (1 in our case).
  4. Click the block and the current value for the rotation sensor will appear.
  5. Write down an upper and lower bound for each position to give some margin for error. If the value of the rotation sensor is within the two numbers, it will be considered correct.
  6. After you have recorded the upper and lower bounds for each of the 3 digits, delete the block (right-click, delete).

Programming steps

We need a way to see if the dial is currently twisted to a certain position. We will be performing this check three times, so it would be nice if there was a way to make the code once but use it multiple times. To do this, we will define a new block.

  1. Click “Variables” and select “Make a block”.A custom block groups many blocks into one block. It can take inputs and do something with them, just like any other block. This block will check to see if the dial position is within a set of upper and lower bounds.
  2. Name this block “checkDialPosition”.
  3. Select “Sensing” as the category for this block, as we will be using many sensor blocks. Then click “OK”.
  4. We want the checkDialPosition block to have two inputs. Click the “+” to the right of the blue block and make an input named “lowerBound”. Then do the same for a second input named “upperBound”. Then click “OK”.
  5. We want the program to remember how many numbers were entered correctly. To remember this, we need to add a variable. Click “Variables” and “Make a Variable”.
  6. Name the variable “numberCorrect” and make it accessible to all sprites.
  7. Go to the “Sensing” category and find the “checkDialPosition” block you just made. Right-click it and choose “edit”.
  8. Now we want the checkDialPosition block to increase the numberCorrect only if the rotation sensor value is greater than or equal to the lower bound and less than or equal to the upperBound. Arrange an if statement, an “And” block (in the Operators category), a “<” block, a “>” block, and two knob sensor blocks in the following configuration:
  9. Drag the lowerBound and upperBound inputs into the correct places.
  10. Now we want to add one to the numberCorrect. Go to the “Variables” category and use a “change by 1” block to accomplish this.
    The program will first wait for the dial to be moved to the reset position. It will then read the dial three times with a two second delay between each reading (to give time for the user to turn the dial). Finally, the program will check the numberCorrect and turn the servo if it equals three. We need a block that waits for the user to turn the dial to reset and then resets the servo position and turns off the LED.
  11. Go to the “Variables” category, Make the a block, and name it “reset”.
  12. The reset block should go under the “Sensing” category.
  13. We want the program to wait until the dial is at reset (100). We can use a wait until block in the Control category to do this. Add the wait until block, an equals block, and a knob sensor block to the reset block’s definition like this:
  14. When it does reach 100, we want to reset the servo (set it to 0) and turn off the LED. It should also set numberCorrect to 0.
  15. Now that the dial is reset, we want to wait until the dial is moved a significant amount before taking the first reading. Make a block “waitForMovement” that waits until the rotation sensor value is less than 100. It should also go under the “Sensing” category.
  16. We should give the user some feedback when the dial is read so that he or she knows to move to the next dial position. We will do this by flashing the LED blue for 0.2 seconds. Make a procedure named flashLED that does this. This time, assign the block to the “Looks” category.
    We are now ready to make the main piece of code! We want this to run when the space bar is pressed and loop forever.
  17. First, let’s reset the lock and then wait for it to be turned. The reset and waitForMovement procedures should do this. Drag them in from the “Sensing” category.
  18. Now, let’s wait 2 seconds to give the user time to move the dial to the first position. Then we will read to see if it is correct and flash the blue LED. Use delay, checkDialPosition, and flashLED to do this.
  19. Add the upper and lower bounds for the first digit of your combination.
  20. Do the same for the second and third digits. Don’t flash the LED for the last digit.
  21. Finally, if the numberCorrect = 3, turn the servo to 90 and make the LED green. Else, make the LED red.
  22. Now run the code! Twist the dial to reset. Then enter the combination, waiting for the light to blink after each number. If the combination is correct, the servo should turn. You can hide the numberCorrect variable by unchecking the checkbox in the “Variables” category.

Graphics

The Hummingbird part of your program is complete! Now let’s add some graphics to indicate how many numbers the user should enter and show when the measurements are being taken.

  1. Rename the current sprite so that its name is “Text”. This sprite will be responsible for the text that will appear on the screen.
  2. Design three costumes for the Text sprite: one showing that the servo is in the locked position, one stating that the combination was incorrect, and one showing that it is unlocked. Name the costumes accordingly.
    Locked:
    Incorrect:
    Unlocked:
  3. Add “switch costume” blocks into the reset block’s definition and the if/else sections of the forever loop.
  4. Run the project by pressing the space bar. The text on the screen should now indicate the current state of the lock.
  5. Now let’s make some sprites that show how many digits have been entered. Make a new sprite with the paint brush button.
  6. Give the sprite two costumes: one for when the digit it represents has not been entered and one for when it has.Not entered:
    Entered:
  7. Name the sprite “Digit1”.
  8. Drag this sprite to an appropriate location on the screen in the corner (called the stage). Be sure to leave room for other sprites.
  9. This sprite represents the first digit. We will make two more copies of it for the other digits. But before we do that, let’s add some scripts to this sprite. Select the “Scripts” tab.The page will now be blank. This is because each sprite has its own set of scripts. These scripts control the properties of that specific sprite. For example, placing a switch costume block in the scripts of the sprite named “Text” (like we just did) will cause that sprite to change its costume. Putting the same block in the scripts of the Digit1 sprite would cause Digit1 to change its costume. The scripts tab only shows the scripts of the sprite that is currently selected. Scripts from different sprites can communicate with each other using “broadcasts”.
  10. Edit the reset block and add a broadcast block from the Control category to the reset block’s definition. Click the dropdown on the broadcast block and click “new”. Call it “reset”. This will tell other sprites to reset their positions.
  11. Add another broadcast block named “beginCombination” at the end of the waitForMovement block’s definition.
  12. Select the Text Sprite and add a broadcast block after each checkDialPosition block in the forever loop. Name them “firstNumber”, “secondNumber”, and “thirdNumber”, respectively.
  13. Now switch back to the Digit1 sprite and add two broadcast received blocks from the “Control” category and connect a switch costume block to each. Click the drop down on each broadcast received block and select the correct broadcast. If you don’t see the one one you are looking for, click “new” and type its name.
  14. Duplicate the Digit1 sprite twice and change the broadcasts to receive secondNumber and thirdNumber.Digit2 sprite:
    Digit3 sprite:
  15. Position the sprites in a line.
  16. Run the project and the digits will enter themselves as the dial is turned.We could stop here, but the user still does not know how much time he/she has before a reading will be taken. We can fix this by adding an indicator that shows when a reading will be taken.
  17. Create a new sprite with the “New turtle sprite” button.
  18. Click the “Motion” category and select “0” from the drop down on the “point in direction” block. Then click the block. The arrow should now point up.
  19. Drag the arrow sprite so that it is below the first digit.
  20. Under the scripts tab, add a broadcast receive block for the beginCombination message.
  21. In the “Motion” category, check the boxes next to the x and y position blocks. You will then be able to see the coordinates of the arrow.
  22. Attach a “go to” block from the “Motion” category to the broadcast block. Enter the sprite’s coordinates into the block.
  23. Move the sprite so that it is touching the first digit.
  24. Add a “glide to” block below the “go to” block and enter the new coordinates.
  25. Move the sprite so that it is touching the last digit.
  26. Add another “glide to” block with coordinates.
  27. Place a “show” block (from the “Looks” category) above the first block and a “hide” block below the last one.
  28. The arrow should touch each digit as that digit is read. To time this correctly, the first glide block should be set to 2 seconds, and the second one should be 4.6 seconds.
    Your code is complete! The arrow should point to each digit as it is recorded. Great job!
News

Snap! Sprite Sizer

Hummingbird setup

  1. Connect the distance sensor to sensor port 1.
  2. Plug the Hummingbird into your computer.

Programming steps

  1. We are going to make the value of the distance sensor determine the size of the turtle (arrow) sprite. To change the sprite’s size, go to the “Looks” category and move a “set size to” block to your screen.
  2. Next, attach a distance sensor block to the size block.
  3. This resizing should occur repeatedly, so place a forever loop around the size block.
  4. Run the code by clicking the loop.
    The code works, but the arrow gets smaller when your hand is closer to the sensor. Real objects get larger when they are closer, so it would make more sense if we made the arrow get bigger when the distance sensor detected that the object was closer.
  5. Add a subtraction block that calculates 105 minus the current sensor reading. 5 is the minimum value the distance sensor can produce, so 105 minus the sensor’s value will produce a size of at most 100%.
  6. Run the code again. This time the arrow should get larger when the object is closer.
  7. Now let’s make the size change more noticeably by making the arrow three times larger. Add a multiplication block to do this.
  8. Run the program and the arrow should be much larger when your hand is close.
News

Snap! Rainbow LED

Hummingbird setup

  1. Connect the tri-color LED to port 1.
  2. (optional) Cover the LED with Styrofoam (or another translucent substance) to diffuse the light.

Programming steps

  1. Let’s begin with a “when space key pressed” block to launch our program. (It is in the “Control” category.) Drag the block to the middle of the screen.
  2. To make our program repeat, use a forever loop from the “Control” category.
  3. We need to keep track of how far along we are in the fading process as we fade the LED from one color to another. A variable will allow our program to do this. Click the “Variables” category and click “Make a Variable”.
  4. Name the variable “Count”.
  5. The count variable will start at 0 and increase as the LED fades. Add a “set variable to 0” block to the loop to do this. Then select “Count” from the drop down list.
  6. Add a repeat loop (from the “Control” category) under the set block and make it repeat 100 times.
  7. To know how many times the loop has repeated, add a “change Count by 1” block (from the “Variables” category) inside the repeat loop.
  8. Now we are ready to add a tri-color LED block from the “Looks” category. Place it above the “change Count by 1” block.

    We will start by fading from red to green. This means fading green on while fading red off.
  9. To fade the green LED on, the green part of the tri-color LED should start at 0 and increase to 100. This can be achieved by dragging a “Count” block (from the “Variables” category) into the tri-color LED block’s green section.
  10. Now to make the red LED fade off, set the red component to 100 minus the current value of the Count variable. Use a “-” block from the “Operators” category to do this.
  11. To go from green to blue, duplicate the set and repeat blocks.
  12. Rearrange the blocks to make the green fade out while the blue fades in. Set red to 0.
  13. Now do the same to fade from blue back to red.
  14. Press the space bar and the LED will fade through the spectrum!
News

Snap! Burglar Alarm

Hummingbird setup

  1. Connect the yellow LED to port 1 and the red LED to port 2.
  2. Connect the vibration motor to port 1.
  3. Connect the distance sensor to port 1.
  4. Plug your Hummingbird into a wall socket using the power adaptor.
  5. Connect the Hummingbird to your computer to begin programming.

Programming steps

  1. Let’s start with a “when space key pressed” block to launch our program. Drag the block to the middle of the screen. (It is in the “Control” category.)
  2. We need the Hummingbird to repeatedly check the distance sensor. To do this, we will use a loop. Attach a forever loop from the “Control” category to the when key pressed block.

    Now let’s use the distance sensor to detect when someone is getting close. If they are close, we want to turn the red light on. To perform this check, we will need to use an if statement.
  3. Click “Control” and place an if…else… statement inside of the loop.
  4. We want the light to turn on if the distance is less than a certain amount. Click “Operators” and insert the “<” block into the slot after the word “if”.
  5. Now we need to read the distance sensor. Click “Sensing” and place a distance sensor block (in cm) to the left of the < sign.
  6. The distance sensor indicates how many centimeters away an object is. Click the white box to the right of the < and type 12.
  7. Add an LED block (in the “Looks” category) to the “if” section of the if block. Set its intensity to 100 and port to 2. This will turn on the red LED if someone is closer than 12 centimeters.
  8. Now we want to make the yellow “warning light” turn on if someone is within 30 centimeters. Drag another if…else… block into the else section of the initial if statement.
  9. Right-click the green “<” block and click “duplicate”. Connect the copied blocks to the new if statement.
  10. Change the number 12 to 30 in this new set of blocks.
  11. Duplicate the LED block and place it in the if section. Then set the LED to port 1 (the yellow LED).
  12. To turn off both lights when nothing is within 30 centimeters from the distance sensor, add two LED blocks to the else section that set the intensities of the LEDs on ports 1 and 2 to 0.
  13. Run the code by pressing your space bar. The yellow light should turn on when you get closer to the sensor and both lights should turn on when you get even closer. Click the stop button when you are done.
  14. Add two vibration motor blocks from the “Motion” category to make the Hummingbird vibrate when someone is too close. They go in the first if section and the else section. Use intensity 100 for on and 0 for off.

Graphics

  1. This takes care of the Hummingbird’s outputs, but Snap! also lets us display images on the screen. To take advantage of this, click the “Costumes” tab.
  2. Costumes change the appearance of the “sprites” on your screen. The Turtle is the default costume. Click the paint brush icon to create a new costume. You can also upload pictures from your computer.
  3. Make the first screen for when the sensor does not detect anything.
  4. Name the costume by right-clicking its icon and selecting “rename”. We chose “nothingDetected” as the name.
  5. Click the paint brush and design another costume for when the warning light is on.
  6. Finally, make a costume for when the red LED is lit.
  7. Now go back to the “Scripts” tab and select “Looks”. Then use three “switch costume to” blocks to set the correct screen.
  8. Finally, we can use two speak blocks from the “Sound” category to alert us when an intruder is detected. Be sure to add a wait 1 second block from the “Control” category after each speak block to give the computer time to finish speaking.
  9. Turn up your computer’s volume, press the spacebar, and try it out!
News

Snap! Traffic Light

Hummingbird setup

  1. Connect the green, yellow, and red LEDs to ports 1, 2, and 3, respectively.
  2. Connect the Hummingbird to your computer to begin programming.

Programming steps

  1. Open the BirdBrain Robot Server.
  2. Wait for the screen to say “Hummingbird Duo Connected”.
  3. Click “Open Snap!”.
  4. Delete any blocks in the middle of the screen by right-clicking them and selecting “delete”.
  5. First we need a “trigger” to indicate when our program should run. Select the “Control” category.
  6. Drag a “When space key pressed” block into the middle of the screen to make the program begin when the space key is pressed.
  7. Now we are ready to add the blocks that control the Hummingbird’s LEDs. Select the “Looks” category and notice the two Hummingbird blocks at the bottom of the list.

    We want our traffic light to go from green to yellow to red and then back to green. First we need to turn on the green light (port 1).
  8. Attach a “Hummingbird LED” block to the “When space key pressed” block.
  9. We want the LED to be at its maximum power, which is 100. Click the 50 and type 100.Your screen should look like this:
  10. You can test a block by clicking on it. Click on the LED block and the green light should turn on.That turns the green light on. Now we need to turn it back off.
  11. Right-click (or ctrl-click) the LED block and click “duplicate”.
  12. Attach the new LED block to the end of the original one and change the intensity to 0.But we don’t want the light to turn off immediately. We want to leave the green light on for some time.
  13. Select the “Control” category and drag a “wait 1 secs” in-between the two LED blocks.
  14. Set the delay to 3 seconds.
  15. Duplicate the stack of three blocks by right-clicking the first LED block and selecting “duplicate”.
  16. Attach the new blocks to the end of the stack.Your code should now look like this:
  17. Now let’s change the second two LED blocks to affect the yellow LED. Click the 1 after “LED” and change the port number to port 2 on the bottom two LED blocks.
  18. The yellow light should be on for a shorter time than the green light. Change the delay to 1 second.
  19. Now duplicate the blocks again and set them to the red LED (port 3).
  20. Set the delay to 2 seconds.The program is now almost complete:
  21. We want the program to repeat forever. Attach a forever loop (from the “Control” category) below the key pressed block. Then drag the remaining blocks into the loop.
  22. Now run the program by pressing your space bar. The light should go from green to yellow to red. To stop the program, click the red stop sign.
  23. To save your program, click the file icon and select “Save As”. Then save the file to your browser.
News

Scratch Combination Lock

Hummingbird setup

  1. Print out the dial template or design your own.
  2. Cut out the dial and glue or staple it to a piece of cardboard.
  3. Cut a small slit below the 5 that lines up with the bump on the rotation sensor.
  4. Attach the dial to the rotation sensor.
  5. Twist the rotation sensor counter-clockwise as far as it will go.
  6. Cut a small arrow shape out of cardboard and wedge it into the slit in the rotation sensor so that it points toward the word “Reset”.
  7. Connect the rotation sensor to sensor port 1 of the Hummingbird.
  8. Add a tri-color LED to port 1.
  9. Connect a servo to servo port 1.
  10. Plug the Hummingbird into a wall socket and into your computer.

Measurements

  1. Select a three-digit combination to use with the lock. We chose 4-8-1.We want to detect when the arrow is pointed toward the correct number, but we also want to leave a little room for error. We need some way of measuring the rotation sensor’s output when the arrow is in the correct position.
  2. Drag a Hummingbird rotary block on to the screen and set it to port 1.
  3. Twist the dial to the first digit of your combination (1 in our case).
  4. Click the block and the current value for the rotation sensor will appear.
  5. Write down an upper and lower bound for each position to give some margin for error. If the value of the rotation sensor is within the two numbers, it will be considered correct.
  6. After you have recorded the upper and lower bounds for each of the 3 digits, delete the block (right-click, delete).

Programming steps

We need a way to see if the dial is currently twisted to a certain position. We will be performing this check three times, so it would be nice if there was a way to make the code once but use it multiple times. To do this, we will define a new block.

  1. Click “More Blocks” and click “Make a Block”.
    A custom block groups many blocks into one block. It can take inputs and do something with them, just like any other block. This block will check to see if the dial position is within a set of upper and lower bounds.
  2. Name this block “checkDialPosition”. Then click “Options”.
  3. We want the checkDialPosition block to have two inputs. Click the add number input button twice and name the inputs “lowerBound” and “upperBound”.
  4. We want the program to remember how many numbers were entered correctly. To remember this, we need to add a variable. Click “Data” and “Make a Variable”.
  5. Name the variable “numberCorrect” and make it accessible to all sprites
  6. Now we want the checkDialPosition block to increase the numberCorrect only if the rotation sensor value is greater than or equal to the lower bound and less than or equal to the upperBound. Arrange an if statement, an “And” block (in the Operators section), a “<” block, a “>” block, and two rotary sensor blocks in the following configuration:
  7. Drag the lowerBound and upperBound inputs into the correct places.
  8. Now we want to add one to the numberCorrect. Go to the “Data” category and use a “change numberCorrect by 1” block to accomplish this.

    The program will first wait for the dial to be moved to the reset position. It will then read the dial three times with a two second delay between each reading (to give time for the user to turn the dial). Finally, the program will check the numberCorrect and turn the servo if it equals three. We need a block that waits for the user to turn the dial to reset and then resets the servo position and turns off the LED.
  9. Make the new block and name it “reset”.
  10. We want the program to wait until the dial is at reset (100). We can use a wait until block in the Control category to do this. Add the wait until block, an equals block, and a rotary sensor block to the reset block’s definition like this:
  11. When it does reach 100, we want to reset the servo (set it to 0) and turn off the LED. It should also set numberCorrect to 0.
  12. Now that the dial is reset, we want to wait until the dial is moved a significant amount before taking the first reading. Make a block “waitForMovement” that waits until the rotation sensor value is less than 100.
  13. We should give the user some feedback when the dial is read so that he or she knows to move to the next dial position. We will do this by flashing the LED blue for 0.2 seconds. Make a procedure named flashLED that does this.

    We are now ready to make the main piece of code! We want this to run when the space bar is pressed and loop forever.
  14. First, let’s reset the lock and then wait for it to be turned. The reset and waitForMovement procedures should do this. Drag them in from the more blocks category.
  15. Now, let’s wait 2 seconds to give the user time to move the dial to the first position. Then we will read to see if it is correct and flash the blue LED. Use delay, checkDialPosition, and flashLED to do this.
  16. Add the upper and lower bounds for the first digit of your combination.
  17. Do the same for the second and third digits. Don’t flash the LED for the last digit.
  18. Finally, if the numberCorrect = 3, turn the servo to 90 and make the LED green. Else, make the LED red.
  19. Now run the code! Twist the dial to reset. Then enter the combination, waiting for the light to blink after each number. If the combination is correct, the servo should turn. You can hide the numberCorrect variable by unchecking the checkbox in the “Data” category.

Graphics

The Hummingbird part of your program is complete! Now let’s add some graphics to indicate how many numbers the user should enter and show when the measurements are being taken.

  1. Select the first (and only) sprite and click the blue “i” that appears.
  2. Name the sprite “Text”. This sprite will be responsible for the text that will appear on the screen.
  3. Design three costumes for the Text sprite: one showing that the servo is in the locked position, one stating that the combination was incorrect, and one showing that it is unlocked. Name the costumes accordingly.
    Locked:
    Incorrect:
    Unlocked:
  4. Add “switch costume” blocks into the reset block’s definition and the if/else sections of the forever loop.
  5. Run the project by pressing the space bar. The text on the screen should now indicate the current state of the lock.
  6. Now let’s make some sprites that show how many digits have been entered. Make a new sprite with the paint brush button.
  7. Give the sprite two costumes: one for when the digit it represents has not been entered and one for when it has.
    Not entered:
    Entered:
  8. Name the sprite “Digit1” using the blue “i”.
  9. Drag this sprite to an appropriate location on the screen in the corner (called the stage). Be sure to leave room for other sprites.
  10. This sprite represents the first digit. We will make two more copies of it for the other digits. But before we do that, let’s add some scripts to this sprite. Select the “Scripts” tab.The page will now be blank. This is because each sprite has its own set of scripts. These scripts control the properties of that specific sprite. For example, placing a switch costume block in the scripts of the sprite named “Text” (like we just did) will cause that sprite to change its costume. Putting the same block in the scripts of the Digit1 sprite would cause Digit1 to change its costume. The scripts tab only shows the scripts of the sprite that is currently selected. Scripts from different sprites can communicate with each other using “broadcasts”.
  11. Select the Text Sprite and add a broadcast block from the Events category to the reset block’s definition. Click the dropdown on the broadcast block and click “new message”. Call it “reset”. This will tell other sprites to reset their positions.
  12. Add another broadcast block named “beginCombination” at the end of the waitForMovement block’s definition.
  13. Add a broadcast block after each checkDialPosition block in the forever loop. Name them “firstNumber”, “secondNumber”, and “thirdNumber”, respectively.
  14. Now switch back to the Digit1 sprite and add two broadcast received blocks from the “Events” category and connect a switch costume block to each.
  15. Duplicate the Digit1 sprite twice and change the broadcasts to receive secondNumber and thirdNumber.Digit2 sprite:Digit3 sprite:

  16. Position the sprites in a line.
  17. Run the project and the digits will enter themselves as the dial is turned.We could stop here, but the user still does not know how much time he/she has before a reading will be taken. We can fix this by adding an indicator that shows when a reading will be taken.
  18. Create a new sprite and add a costume using the “Choose costume from library” button. Choose the up arrow. Then delete the original blank costume.

  19. Drag the arrow sprite so that it is below the first digit.
  20. Under the scripts tab, add a broadcast receive block for the beginCombination message.
  21. Attach a “go to” block from the “Motion” section to the event block. The x and y coordinates will already have the sprite’s current position.
  22. Move the sprite so that it is touching the first digit.
  23. Add a “glide to” block below the “go to” block.
  24. Move the sprite so that it is touching the last digit.
  25. Add another “glide to” block.
  26. Place a “show” block (from the “Looks” section) above the first block and a “hide” block below the last one.
  27. The arrow should touch each digit as that digit is read. To time this correctly, the first glide block should be set to 2 seconds, and the second one should be 4.6 seconds.

    Your code is complete! The arrow should point to each digit as it is recorded. Great job!

 

News

Scratch Sprite Sizer

Hummingbird setup

  1. Connect the distance sensor to sensor port 1.
  2. Plug the Hummingbird into your computer.

Programming steps

  1. We are going to make the value of the distance sensor determine the size of the cat sprite. To change the sprite’s size, go to the “Looks” category and move a “set size to” block to your screen.
  2. Next, attach a distance sensor block to the size block.
  3. This resizing should occur repeatedly, so place a forever loop around the size block.
  4. Run the code by clicking the loop.
    The code works, but the cat gets smaller when your hand is closer to the sensor. Real objects get larger when they are closer, so it would make more sense if we made the cat get bigger when the distance detected that the object was closer.
  5. Add a subtraction block that calculates 105 minus the current sensor reading. 5 is the minimum value the distance sensor can produce, so 105 minus the sensor’s value will produce a size of at most 100%.
  6. Run the code again. This time the cat should get larger when the object is closer.
  7. Now let’s make the size change more noticeably by making the cat three times larger. Add a multiplication block to do this.
  8. Run the program and the cat should fill the screen when your hand is close.
News

Scratch Rainbow LED

Hummingbird setup

  1. Connect the tri-color LED to port 1.
  2. (optional) Cover the LED with Styrofoam (or another translucent substance) to diffuse the light.

Programming steps

  1. Let’s begin with a “when space key pressed” block to launch our program. (It is in the “Events” category.) Drag the block to the middle of the screen.
  2. To make our program repeat, use a forever loop from the “Control” category.
  3. We need to keep track of how far along we are in the fading process as we fade the LED from one color to another. A variable will allow our program to do this. Click the “Data” category and click “Make a Variable”.
  4. Name the variable “Count”.
  5. The count variable will start at 0 and increase as the LED fades. Add a “set Count to 0” block in the loop to do this.
  6. Add a repeat loop (from the “Control” category) under the set block and make it repeat 100 times.
  7. To know how many times the loop has repeated, add a “change Count by 1” block (from the “Data” category) inside the repeat loop.
  8. Now we are ready to add a tri-color LED block from the “More Blocks” category. Place it above the “change Count by 1” block.

    We will start by fading from red to green. This means fading green on while fading red off.
  9. To fade the green LED on, the green part of the tri-color LED should start at 0 and increase to 100. This can be achieved by dragging a “Count” block (from the “Data” category) into the tri-color LED block’s green section.
  10. Now to make the red LED fade off, set the red component to 100 minus the current value of the Count variable. Use a “-” block from the “Operators” category to do this.
  11. To go from green to blue, duplicate the set and repeat blocks.
  12. Rearrange the blocks to make the green fade out while the blue fades in. Set red to 0.
  13. Now do the same to fade from blue back to red.
  14. Press the space bar and the LED will fade through the spectrum!
News

Scratch Traffic Light

Hummingbird setup

  1. Connect the green, yellow, and red LEDs to ports 1, 2, and 3, respectively.
  2. Connect the Hummingbird to your computer to begin programming.

Programming steps

  1. Open the BirdBrain Robot Server.
  2. Wait for the screen to say “Hummingbird Duo Connected”.
  3. Click “Open Scratch”.
  4. First we need a “trigger” to indicate when our program should run. This is called an event. Select the “Events” category.
  5. Drag a “When space key pressed” block into the middle of the screen to make the program begin when the space key is pressed.
  6. Now we are ready to add the blocks that control the Hummingbird. Select the “More Blocks” category to reveal the Hummingbird-specific blocks.

    We want our traffic light to go from green to yellow to red and then back to green. First we need to turn on the green light (port 1).
  7. Attach an “HB LED” block to the “When space key pressed” block.
  8. We want the LED to be at its maximum power, which is 100. Click the 50 and type 100.Your screen should look like this:
  9. You can test a block by clicking on it. Click on the LED block and the green light should turn on.That turns the green light on. Now we need to turn it back off.
  10. Right-click (or ctrl-click) the LED block and click “duplicate”.
  11. Attach the new LED block to the end of the original one and change the intensity to 0.But we don’t want the light to turn off immediately. We want to leave the green light on for some time.
  12. Select the “Control” section and drag a “wait 1 secs” block and snap it in-between the two LED blocks.
  13. Set the delay to 3 seconds.
  14. Duplicate the stack of three blocks by right-clicking the first LED block and selecting “duplicate”.
  15. Attach the new blocks to the end of the stack.Your code should now look like this:
  16. Now let’s change the second two LED blocks to affect the yellow LED. Click the 1 after “LED” and change the port number to port 2 on the bottom two LED blocks.
  17. The yellow light should be on for a shorter time than the green light. Change the delay to 1 second.
  18. Now duplicate the blocks again and set them to the red LED (port 3).
  19. Set the delay to 2 seconds.The program is now almost complete:
  20. We want the program to repeat forever. To do this, drag a forever loop (from the “Control” section) over the current blocks so that it expands to wrap around all of them.
  21. Now run the program by pressing your space bar. The light should go from green to yellow to red. To stop the program, click the red stop sign.
  22. To save your program, click “File” and then “Save As” and select a location to save the file. Subsequent modifications can be saved using “File” and “Save”.