Sonar and Sun SPOT
Jun 17
Sensor, SunSPOT analog, java, sonar, spaughts 1 Comment
I got an ultrasonic rang finder or sonar from SparkFun couple of moths ago, did not have time to play with it. Anyway, let the game begin…

It supports serial port, pwm and analog interface. I am using the simplest analog to interface it with Sun SPOT. If you wan to get more accurate data, please use serial port connection wich I will describe in next post. To connect the analog port to Sun SPOT is simple, no resistor is required and the module actually works with both 3V and 5V power supply.
Sonar GND –> GND Sun SPOT
Sonar +5 –> +5 or +3 Sun SPOT
Sonar AN –> A0 or A1 or A3 Sun SPOT

The 8 LEDs on Sun SPOT now represented as a gauge of distance to a object, watch in in action
Here is the source code:
package org.sunspotworld;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.RadioFactory;
import com.sun.spot.sensorboard.io.IScalarInput;
import com.sun.spot.util.*;
import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class StartApplication extends MIDlet {
private ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs();
int range;
protected void startApp() throws MIDletStateChangeException {
System.out.println("Hello, world");
new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host
long ourAddr = RadioFactory.getRadioPolicyManager().getIEEEAddress();
System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));
IScalarInput rawAnalog = EDemoBoard.getInstance().getScalarInputs()[EDemoBoard.A0];
while (true) {
Utils.sleep(300);
try {
range = rawAnalog.getValue();
System.out.println(range);
} catch (IOException ex) {
ex.printStackTrace();
}
showGauge(range);
}
}
protected void pauseApp() {
// This is not currently called by the Squawk VM
}
/**
* Called if the MIDlet is terminated by the system.
* I.e. if startApp throws any exception other than MIDletStateChangeException,
* if the isolate running the MIDlet is killed with Isolate.exit(), or
* if VM.stopVM() is called.
*
* It is not called if MIDlet.notifyDestroyed() was called.
*
* @param unconditional If true when this method is called, the MIDlet must
* cleanup and release all resources. If false the MIDlet may throw
* MIDletStateChangeException to indicate it does not want to be destroyed
* at this time.
*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
for (int i = 0; i < 8; i++) {
leds[i].setOff();
}
}
private void showGauge(int range) {
if (range < 20) {
for (int i = 0; i < 8; i++) {
leds[i].setOff();
}
} else if ((range >= 20) && (range < 80)) {
for (int i = 1; i < 8; i++) {
leds[i].setOff();
}
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
} else if ((range >= 80) && (range < 140)) {
for (int i = 2; i < 8; i++) {
leds[i].setOff();
}
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
} else if ((range >= 140) && (range < 200)) {
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
leds[2].setRGB(75, 25, 0);
leds[2].setOn();
for (int i = 3; i < 8; i++) {
leds[i].setOff();
}
} else if ((range >= 200) && (range < 260)) {
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
leds[2].setRGB(75, 25, 0);
leds[2].setOn();
leds[3].setRGB(63, 38, 0);
leds[3].setOn();
for (int i = 4; i < 8; i++) {
leds[i].setOff();
}
} else if ((range >= 320) && (range < 380)) {
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
leds[2].setRGB(75, 25, 0);
leds[2].setOn();
leds[3].setRGB(63, 38, 0);
leds[3].setOn();
leds[4].setRGB(50, 50, 0);
leds[4].setOn();
for (int i = 5; i < 8; i++) {
leds[i].setOff();
}
} else if ((range >= 380) && (range < 440)) {
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
leds[2].setRGB(75, 25, 0);
leds[2].setOn();
leds[3].setRGB(63, 38, 0);
leds[3].setOn();
leds[4].setRGB(50, 50, 0);
leds[4].setOn();
leds[5].setRGB(38, 63, 0);
leds[5].setOn();
for (int i = 6; i < 8; i++) {
leds[i].setOff();
}
} else if ((range >= 440) && (range < 480)) {
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
leds[2].setRGB(75, 25, 0);
leds[2].setOn();
leds[3].setRGB(63, 38, 0);
leds[3].setOn();
leds[4].setRGB(50, 50, 0);
leds[4].setOn();
leds[5].setRGB(38, 63, 0);
leds[5].setOn();
leds[6].setRGB(25, 75, 0);
leds[6].setOn();
leds[7].setOff();
} else if (range >= 480) {
leds[0].setRGB(100, 0, 0);
leds[0].setOn();
leds[1].setRGB(88, 13, 0);
leds[1].setOn();
leds[2].setRGB(75, 25, 0);
leds[2].setOn();
leds[3].setRGB(63, 38, 0);
leds[3].setOn();
leds[4].setRGB(50, 50, 0);
leds[4].setOn();
leds[5].setRGB(38, 63, 0);
leds[5].setOn();
leds[6].setRGB(25, 75, 0);
leds[6].setOn();
leds[7].setRGB(0, 100, 0);
leds[7].setOn();
}
}
}
RSS

Apr 09, 2012 @ 10:32:10
Hi,
Thanks for this post, but can you tell me how to use the pwm pin i am a little confused. What i got is a built in system so i only know that i need to use the pwm pin.
Thanks