This presentation is the property of its rightful owner.
1 / 23

# Decisions, Looping, and input sensors PowerPoint PPT Presentation

Decisions, Looping, and input sensors. Mindstorms , Eclipse and Java. Decisions and Looping. So far, we can only execute a sequence of lines of code. Decision control structures Vary which lines of code to execute if something is true or false. The IF statement

Decisions, Looping, and input sensors

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## Decisions, Looping, and input sensors

Mindstorms, Eclipse and Java

### Decisions and Looping

• So far, we can only execute a sequence of lines of code.

• Decision control structures

• Vary which lines of code to execute if something is true or false.

• The IF statement

• Looping control structures

• Repeat code over and over again some number of times.

• The WHILE loop

The Java:

if(condition){

// java code

}

### But what can the condition be?

• Relational operators:

==equal to

!=not equal to

>greater than

<less than

>=greater than or equal to

<=less than or equal to

truealways true

falsealways false

### Example:

int x = 5;

if (x > 10){

LCD.drawString(“Hi”, 0, 0);

}

if(x <= 10){

LCD.drawString(“Bye”, 0, 0);

}

What is displayed on the LCD?

### While

The Java:

while(condition){

// java code

}

### Example

int y= 1;

while (y <= 3){

LCD.drawString(“Hi”, 0, y);

y = y + 1;

}

What is displayed on the LCD?

### Using Input Sensors

• The Mindstorms input sensors can return data that you can use in a condition to make a decision.

Buttons

• The Button class has four instances, accessed by static fields:

• Button.ENTER

• Button.ESCAPE

• Button.LEFT

• Button.RIGHT

### Methods

• waitForPress()

• Returns an integer corresponding a button

• ENTER: 1

• LEFT:2

• RIGHT:4

• ESCAPE:8

• waitForPressAndRelease()

• Can be applied to any button

• Example: Button.ENTER.waitForPressAndRelease();

• isPressed()

• Can be applied to any button

• Returns true or false

### Example Button Code

while (true) {

LCD.clear();

if (Button.ENTER.isPressed())

LCD.drawString("ENTER", 0, 0);

if (Button.ESCAPE.isPressed())

LCD.drawString("ESCAPE", 0, 0);

if (Button.LEFT.isPressed())

LCD.drawString("LEFT", 0, 0);

if (Button.RIGHT.isPressed())

LCD.drawString("RIGHT", 0, 0);

}

### Sensing Touch

• TouchSensor

• isPressed()

• Returns true if the button is pressed.

• Needs to be plugged into one of the Sensor ports.

• In your code, you will need to declare a new touch sensor, indicating which port it is plugged into:

TouchSensor t = new TouchSensor(SensorPort.S1);

Give it a name

Indicate the port number

Sensor port 1

### Try this code:

What is displayed on the LCD?

### Sound Sensor

• SoundSensor

• Returns a value in the range [0-100] corresponding to how much noise it hears.

• Like the touch sensor it needs to be plugged into a sensor port and initialized

### Try out this code

What is displayed on the LCD?

### The Ultra Sonic Distance Sensor

• UltrasonicSensor

• getDistance()

• Returns the distance to an object in CM.

• A value of 255 means no distance was detected

• Example Code:

### Light Sensor

• LightSensor

• Sensor how much light in around the Mindstorm

• Returns the raw value

• Returns a value in between 0 to 1023

• setFloodLight(boolean)

• Turn flood light on or off.

### Wrapping up…

• Sound Object

• Make the Mindstorm beep

• beep()

• twoBeeps()

• beepSequence()

• beepSequenceUp()

• playSample(File aWAVfile)

• playSample(File aWAVfile, int volume)

• playNote(int[] inst,int freq, intlen)

### Wrapping up…

• Battery

• getVoltageMilliVolt()

• getVoltage()

• LCD

• setPixel(intpixelValue, int x, int y);

• Can be used to draw a single pixel (dot) on the LCD