Decisions looping and input sensors
Download
1 / 23

Decisions, Looping, and input sensors - PowerPoint PPT Presentation


  • 119 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Decisions, Looping, and input sensors' - silas


An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Decisions looping and input sensors

Decisions, Looping, and input sensors

Mindstorms, Eclipse and Java


Decisions and looping
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


If statement
IF Statement

The Java:

if(condition){

// java code

}


But what can the condition be
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

    true always true

    false always false


Example
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
While

The Java:

while(condition){

// java code

}


Example1
Example

int y= 1;

while (y <= 3){

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

y = y + 1;

}

What is displayed on the LCD?


Using input sensors
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
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
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
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



Try this code
Try this code:

What is displayed on the LCD?


Sound sensor
Sound Sensor

  • SoundSensor

    • Readvalue()

      • 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
Try out this code

What is displayed on the LCD?



The ultra sonic distance sensor
The Ultra Sonic Distance Sensor

  • UltrasonicSensor

    • getDistance()

      • Returns the distance to an object in CM.

      • A value of 255 means no distance was detected

      • Maximum is about 170

  • Example Code:


Light sensor
Light Sensor

  • LightSensor

    • Sensor how much light in around the Mindstorm

    • readValue()

      • Returns the raw value

    • readNormalizedValue()

      • Returns a value in between 0 to 1023

    • setFloodLight(boolean)

      • Turn flood light on or off.



Wrapping up
Wrapping up…

  • Sound Object

    • Make the Mindstorm beep

    • beep()

    • twoBeeps()

    • beepSequence()

    • beepSequenceUp()

    • playTone(intaFrequency, intaDuration)

    • playSample(File aWAVfile)

    • playSample(File aWAVfile, int volume)

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


Wrapping up1
Wrapping up…

  • Battery

    • getVoltageMilliVolt()

    • getVoltage()

  • LCD

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

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




ad