Touch Sensors


Topics
Touch Sensors
Wait For Commands
Sounds
De-bugging


Classes Learned
Wait For (This is a class I constructed to make touch sensors easier to understand, download it here)
Sensor
Sound

Preparation
Pre-build the touch sensors (optional)

Handouts
Touch Sensor Challenges - Solutions
Cheat Sheet

Lesson
Begin by handing out the touch sensor "heads" or having students construct their own. Explain how with the motors the robot could only move around in precise motions. With a touch sensor the robot is able to determine if it has hit a wall. Put up a simple program on the board that uses a wait-for command.
Here is an example:
 
import josx.platform.rcx.*;

public class Example {

    public static void main (String[] args) throws Exception {
        Sensor.S1.setTypeAndMode (1, 0x20);
        Sensor.S1.activate();

        Motor.A.forward();
        Motor.C.forward();

        WaitFor.push(Sensor.S1);

        Motor.A.stop();
        Motor.C.stop();
    }
}


This program turns on both motors in the forward direction causing the robot to go forward. It then waits until the touch sensor is pressed and then stops. Essentially the robot goes forward till it hits a wall and then stops.

The new stuff:
    Sensor.S1.setTypeAndMode (1, 0x20);
    Sensor.S1.activate();

Whenever a sensor is used the program needs to know what type of sensor it is and what kind of information you want from it. The above code specifies a touch sensor and activates it.
setTypeAndMode(Type, Mode)

    WaitFor.push(Sensor.S1);

The WaitFor class is a custom class designed to make learning touch sensors less complex and more simmiler to the RoboLab software. It can wait for a push or a release. It can also wait for a specified number of pushes and releases.

The last topic is sounds. Though this is not needed for the general operation of the Robot, it is a great debugging tool. Sounds also make students more excited about programing. Sounds also work well for in-class examples. It may be easier for the students to hear something than to see it.
The two most important commands with sounds are playTone() which lets the students play a note. (See cheet sheet for specifics) And systemSound() which can play one of 6 different pre-programed sounds.
 
Note: After each sound is played you have to insert a Thread.sleep(time) or some type of action that requires time. leJOS dosn't pause for the sound to be played.

Divide the students into their groups and give them a copy of the challenges handout and the Cheat Sheet.