Hello folks,

I'm doing a similar project done 
by 
http://www.robotfreak.de/blog/en/tutorial/ioio-robotics-tutorial-1-ultrasonic-sensor/732
But, I want to use three ultrasonic sensors and i'm trying to modify the 
code example, see below:
How to do for three sensors? The code below, demonstrate with two sensors, 
but only one is working.
I'd three sensors, one is SRF05 and the rest is URM37. 

package ioio.examples.ultrasonic;

public class IOIOUltrasonicSensorActivity extends AbstractIOIOActivity {
/* ultrasonic sensor */
private ProgressBar progressBar1_;
private TextView textView2_;
private ProgressBar progressBar2_;
private TextView textView3_;
private int echoSeconds;
private int echoDistanceCm;
    private int echoSeconds_;
    private int echoDistanceCm_;
    
/**
 * Called upon creation for initialization
 */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* ultrasonic sensor */
progressBar1_ = (ProgressBar) findViewById(R.id.progressBar1);
textView2_ = (TextView) findViewById(R.id.textView2);
progressBar2_ = (ProgressBar) findViewById(R.id.progressBar2);
textView3_ = (TextView) findViewById(R.id.textView3);
}

 class IOIOThread extends AbstractIOIOActivity.IOIOThread {
 /* ultrasonic sensor */
private DigitalOutput triggerPin_;
private PulseInput echoPin_;
        //private DigitalOutput triggerPin2_;
        //private PulseInput echoPin2_;
/**
 * Called every time a connection with IOIO has been established. (opens
 * pins)
 * 
 * @throws ConnectionLostException
 *             (if IOIO connection is lost)
 */

public void setup() throws ConnectionLostException {
try {
/* ultrasonic sensor */
echoPin_ = ioio_.openPulseInput(6, PulseMode.POSITIVE);
triggerPin_ = ioio_.openDigitalOutput(7);
//echoPin2_ = ioio_.openPulseInput(10, PulseMode.POSITIVE); //TRIG
//triggerPin2_ = ioio_.openDigitalOutput(11); //
} catch (ConnectionLostException e) {
throw e;
}
}

/**
 * Loop section
 */

public void loop() throws ConnectionLostException {
try {
// read HC-SR04 ultrasonic sensor
triggerPin_.write(false);
sleep(5);
triggerPin_.write(true);
sleep(1);
triggerPin_.write(false);
echoSeconds = (int) (echoPin_.getDuration() * 1000 * 1000);
echoDistanceCm = echoSeconds / 29 / 2;
      //read URM37 ultrasonic sensor
//triggerPin2_.write(false);
//sleep(5);
//triggerPin2_.write(true);
//sleep(1);
//triggerPin2_.write(false);
//echoSeconds_ = (int) (echoPin2_.getDuration() * 1000 * 1000);
//echoDistanceCm_ = echoSeconds_ /29 /2;
 /* update UI */
updateViews();

sleep(20);
} catch (InterruptedException e) {
ioio_.disconnect();

} catch (ConnectionLostException e) {
throw e;

}
}
}

/**
 * A method to create our IOIO thread.
 */

@Override
protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
return new IOIOThread();
}

private void updateViews() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView2_.setText(String.valueOf(echoDistanceCm));
progressBar1_.setProgress(echoDistanceCm);
//textView3_.setText(String.valueOf(echoDistanceCm_));
//progressBar2_.setProgress(echoDistanceCm_);
}
});
}
}

-- 
You received this message because you are subscribed to the Google Groups 
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to