decisions looping and input sensors
Download
Skip this Video
Download Presentation
Decisions, Looping, and input sensors

Loading in 2 Seconds...

play fullscreen
1 / 23

Decisions, Looping, and input sensors - PowerPoint PPT Presentation


  • 123 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