230 likes | 376 Views
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
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
IF Statement 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 true always true false always 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
Example: Plug the touch senor in the port 1 Sensor port 1
Try this code: What is displayed on the LCD?
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 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 • Maximum is about 170 • Example Code:
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… • 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 up… • Battery • getVoltageMilliVolt() • getVoltage() • LCD • setPixel(intpixelValue, int x, int y); • Can be used to draw a single pixel (dot) on the LCD