1 / 23

Decisions, Looping, and input sensors

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

silas
Download Presentation

Decisions, Looping, and input sensors

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decisions, Looping, and input sensors Mindstorms, Eclipse and Java

  2. 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

  3. IF Statement The Java: if(condition){ // java code }

  4. 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

  5. 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?

  6. While The Java: while(condition){ // java code }

  7. Example int y= 1; while (y <= 3){ LCD.drawString(“Hi”, 0, y); y = y + 1; } What is displayed on the LCD?

  8. 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

  9. 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

  10. 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); }

  11. 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

  12. Example: Plug the touch senor in the port 1 Sensor port 1

  13. Try this code: What is displayed on the LCD?

  14. 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

  15. Try out this code What is displayed on the LCD?

  16. This code uses sound to trigger a motor to rotate

  17. 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:

  18. 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.

  19. Light Sensor Example Code

  20. 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)

  21. Wrapping up… • Battery • getVoltageMilliVolt() • getVoltage() • LCD • setPixel(intpixelValue, int x, int y); • Can be used to draw a single pixel (dot) on the LCD

  22. Drawing a Parabola on LCD

  23. Plot sound waves over a 2 second period. VERY COOL!!!!

More Related