Unit 03 Section 02 Lesson 02



Integrated System Two Remote Control

Alternate Activity using Gadget Master II

Assigned activities

The purpose of the following activity is to develop capability with building an integrated system that combines input and output under software control. The specific activity is to prototype a trap door that can be activated either by a remote control and infrared light sensor or by some other device such as a magnetic sensor.

Activity 1 Part 1


Figure Trap Door

Required Resources

  • Phidget Motor Controller
  • Solenoid
  • Phototransistor
  • Infrared LED
  • Push button switch
  • 9v battery and battery connector
  • Foam core board, or
  • 3D Printed trap door
    • frame
    • trapdoor
    • catch
  •  Hardware, tape, and glue

Fabrication

The trap door assembly can be made from foam core board, thin plywood, sheet plastic, or aluminium.  Templates for the individual components are available in the next link.

 A 3D printed trap door can be used as well. The 3D SketchUp Pro files can be downloaded here.

Trap Door Templates - Foam Core Board

Once you have the components cut to size, use the following link as an assembly guide.

Assembly Guide - Foam Core Board

The Assembly guide for the 3D printed trap door is next.

Assembly Guide - 3D Printed Trap Door

Circuit Configuration - Foam Core Board


Figure Connections
  • Connect the red wire from the phototransistor to Input line 0 and the black wire to ground (G)
     
  • Connect the two wires from the solenoid to Output 0. Since the solenoid is not a polarized device, it doesn't matter which wire is connected to the respective output lines.

Circuit Configuration - 3D Printed Trapdoor

  • If you are using the CDLI 3D printed trapdoor, the hookup is the same. See the next image

Create the Graphical User Interface

Note: Save your project at frequent intervals!

  • Run Visual Basic 6.  Select a Standard Exe.
     
  • Draw a Frame on the form.
     
  • Draw the following controls on the frame. A suggested layout follows
     
    • Two command buttons
    • One text box
    • One timer
    • One image box

Figure Form 1 with Controls

Change the properties for the following:

  • Frame1
    • Caption - Trap Door
    • Font - Arial, 14pt, bold
    • Backcolor - something appropriate!
       
  • Command1 button:
    • Caption - Enable
    • Font - Arial, Bold, 12
    • Style - Graphical
    • Backcolor - some color to contrast with the Frame color
       
  • Command2 button:
    • Caption - Disable
    • Font -  Arial, Bold, 12
    • Style - Graphical
    • Backcolor - some color to contrast with the Frame color
       
  • Text1 box:
    • Text - Delete the default text (Text1)
    • Backcolor -  green
    • Font - Arial, Bold, 14
       
  • Timer1
    • Enabled - False
    • Interval - 100

    The image box will load 2 different images, one as default and one when the trap door is "sprung". You can get the images by clicking the two links below. Save the images in the C:/vbimages folder on your hard drive. (If they are placed in another folder, you will have to adjust the code and properties.)  Right Click on each link and select Save Target As.  Place in C:\vbimages
     

  • Image1
    • Picture - Click the button (3 dots), browse to c:\vbimages and select trapup.jpg
    • Border Style - 1 - Fixed Single

Code the Interface

  • You will be using Phidgets therefore the Phidget library must be referenced. Open the Project menu and select References.

    Scroll down and check Phidget Library 2.1. Click OK.
     
  • Open the code window. Start by selecting General...Declarations in the small windows. Enter the following code:

    Dim WithEvents Motor As PhidgetMotorControl

    Code the Form_Load procedure:

    Set Motor = New PhidgetMotorControl
    Call Motor.Open

     
  • Open the code window and code the Command1 (Enable) button with the following:

Timer1.Enabled = True
Set Image1.Picture = LoadPicture("c:\vbimages\trapup.jpg")

  • Code the Command2 (Disable) button with the following:

    Timer1.Enabled = False
    Text1 = ""
    Text1.BackColor = vbGreen
    Text1.Refresh
    Set Image1.Picture = LoadPicture("c:\vbimages\trapup.jpg")

Next you need to add a sub procedure.

  • Open the Tools menu and select Add Procedure. Name the first procedure ScanSensor.
     
  • Code the Scan Sensor Procedure:

If motor.InputState(0) = True Then
   Text1 = "Hit"
   Text1.BackColor = vbRed
   Text1.Refresh
   Set Image1.Picture =    LoadPicture("c:\vbimages\trapdown.jpg")
   Timer1.Enabled = False
   motor.Velocity(0) = 100
   For T = 1 To 100000000: Next T
   motor.Velocity(0) = 0
Else
   Text1 = "Waiting..."
   Text1.BackColor = vbGreen
   Text1.Refresh
End If

  • Code Timer1 with the following:

ScanSensor

Test the System

Before you test the prototype, check all wiring. Make sure the power supply is connected to the Phidget Motor Controller. Adjust the solenoid armature until it just keeps the trap door from falling. Place something on the trapdoor.

  • Run the program. Click the Enable button.

    The sensor is scanned every 100 milliseconds.
     
  • Point the remote control at the target and press the button.  You may need to try it several times to "hit the target".

When you do hit the target, the trapdoor should open and the screen should change. See how far back you can go and still activate the trapdoor.

To reset the system, click Disable then Enable again in the program.

Part 2 Modify the Code

Now let's make the project a little harder. Instead of "hitting the target" once, we will add code so that it has to be hit 5 times before the trapdoor is sprung.

  • Add some code to the General...Declarations window. The added code is bolded.

Dim WithEvents motor As PhidgetMotorControl, Flag As Boolean, Counter

  • Add code to the Form_Load procedure. The additional code is also shown in bold.

Set Motor = New PhidgetMotorControl
Call Motor.Open
Flag = False
Counter = 0

  • Code the ScanSensor procedure with the following code (the original code is included). Because there are a number of changes, be careful.

'Counter variable keeps track of the number of hits

If (motor.InputState(0) = True) And (Flag = False) Then
   Counter = Counter + 1
   Flag = True
   Text1 = "Hit #" + Str$(Counter)
   Text1.BackColor = vbRed
   Text1.Refresh
   If Counter = 5 Then
      Set Image1.Picture =  LoadPicture("c:\vbimages\trapdown.jpg")
      Text1 = "See Yaaaaaaaaaaaa...."
      Text1.Refresh
      Timer1.Enabled = False
      motor.Velocity(0) = 100
      For T = 1 To 100000000: Next T
      motor.Velocity(0) = 0
   End If
Else
   Text1 = "Waiting..."
   Text1.BackColor = vbGreen
   Text1.Refresh
   Flag = False
End If

Note: The Flag variable is needed to make sure that you can't just hold the remote button down and rack up 5 hits. You must let up on the button between "hits". If you want to change the number of hits, change the number in the line:

If Counter = 5 Then

Run the program. You should need 5 hits to spring the trap door.

Part 3 Your Turn - Additional Challenges

  1. You can turn your trapdoor project into a "better mousetrap". Try taping the magnetic sensor (the Hall effect sensor) to the bottom of the trap door (in the centre). Place a magnet (as a simulated lump of cheese) on top of the platform and use the code from Part1.

There will be one difference in code in the ScanSensor procedure. If the magnet is detected by the sensor it is the same as a closed switch. You want the door to drop when the magnet is removed, that is, when the switch is open. Compare the following code segment to the original in part 1.

If motor.InputState(0) = False
   Text1 = "Hit.."
   Text1.BackColor = vbRed
   Text1.Refresh
   Set Image1.Picture =    LoadPicture("c:\vbimages\trapdown.jpg")
   Timer1.Enabled = False
   motor.Velocity(0) = 100
   For T = 1 To 100000000: Next T
   motor.Velocity(0) = 0
Else
   Text1 = "Waiting..."
   Text1.BackColor = vbGreen
   Text1.Refresh
End If

You might want to change the messages in the textbox and even the images :) have  fun!!

  1. Optional Challenge: Review the concept of random number generation from the previous unit. Could you have a user pick a number, say between 1 and 6. Have the computer generate a random number between 1 and 6 and if it matches the one chosen (use a text box) then the trapdoor is sprung.

 

 
2016 CDLI