... | ... | @@ -54,16 +54,16 @@ We see that the build solves our problem extremely well, as the robot accurately |
|
|
Next, we modified our program to receive raw values directly from the sensor port instead of using *readValue()* on the *SoundSensor* class. We did this because we would like an accurately known interval of measured values in order to map to any range that we desire. The sensor port measures a raw value between 0 and 1023, where a high raw value means a low sound level. We simply convert the measured value to a percentage of the maximum value 1023, then invert this percentage by subtracting it from 100, and then pass it along as the value of the motor power parameter (se code snippet 1). As can be seen in Video 3, this modification still works perfectly fine, and as it allows for more precise and easier mapping to any ranges we want, we decided to continue using raw values.
|
|
|
|
|
|
```java
|
|
|
while(! Button.ESCAPE.isDown()) {
|
|
|
soundLevel = SensorPort.S4.readRawValue();
|
|
|
LCD.drawString(Integer.toString(soundLevel), 0, 1);
|
|
|
while(! Button.ESCAPE.isDown()) {
|
|
|
soundLevel = SensorPort.S4.readRawValue();
|
|
|
LCD.drawString(Integer.toString(soundLevel), 0, 1);
|
|
|
|
|
|
motorPower = (soundLevel/1023.0)*100.0;
|
|
|
LCD.drawString("mopo: " + motorPower, 0, 2);
|
|
|
mp = 100 - (int) motorPower;
|
|
|
LCD.drawString("MP: " + mp, 0, 3);
|
|
|
Car.forward(mp, mp);
|
|
|
}
|
|
|
motorPower = (soundLevel/1023.0)*100.0;
|
|
|
LCD.drawString("mopo: " + motorPower, 0, 2);
|
|
|
mp = 100 - (int) motorPower;
|
|
|
LCD.drawString("MP: " + mp, 0, 3);
|
|
|
Car.forward(mp, mp);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
*Code snippet 1: Code that reads raw values from the sound sensors sensor port and maps it by the described mapping.*
|
... | ... | |