niedziela, 20 listopada 2011

Arduino Sonar Grid

Because of the small project that I did recently I had to build sonar grid on Arduino. The circuit was gathering data from five ultrasonic sensors LV MaxSonar EZ0 and was sending readings to computer via Firmata. I turns out that each sonar will interfere with every other so the signal you will get can be very noisy (depending it on sonar mutual locations and room layout). Something you can do (with this particular sonar) is to turn-off all others sonar in the time of reading data. So if you read data for sonar connected to pin 0 others should not send the ultrasonic wave.

Here goes modified minimal Firmata implementation to achieve this.


#include <Firmata.h>

int analogPin = 0; // counter for reading analog pins
int digitalPin = 0; //conuter for turning on/off digital pins (RX for sensors)
unsigned long currentMillis; // store the current value from millis()
unsigned long previousMillis; // for comparison with currentMillis

void setup()
{
Firmata.setFirmwareVersion(0, 2);

for (digitalPin=2;digitalPin<=7;digitalPin++){
pinMode(digitalPin, OUTPUT);
}
Firmata.begin(57600);
}

void loop()
{
while(Firmata.available())
Firmata.processInput();

currentMillis = millis();
if(currentMillis - previousMillis > 20) {
previousMillis += 20; // run this every 20ms

for(analogPin=0;analogPin<TOTAL_ANALOG_PINS;analogPin++) {
for (digitalPin=2;digitalPin<=7;digitalPin++){
if (digitalPin - 2 == analogPin) {
digitalWrite(digitalPin, HIGH);
}
else {
digitalWrite(digitalPin, LOW);
}
}
delay(40); //wait wait man!
Firmata.sendAnalog(analogPin, analogRead(analogPin));
}
}
}


Analog output (AN pin) from all sonar are connected to A0-A5 pins on Arduino (since we are reading from analog). RX pin (which controls measuring state of sonar) are connected to digital pins 2-5. As you probably see this will get way much lower signal resolution that we can get from one sonar. MaxSonar works in 49ms cycles and sends 42Hz waves at the begining of each cyle only if RX pin is HIGH or unconnected. After changing digital pins state you need to wait a while to seetle state on it and also you need to wait for the sonar to its cycle begining (40ms delay gives very smooth signal).

Of course this is easiest possible solution. Depeding on your need you can read from sonars in pairs on weave reading with differents operations.

Brak komentarzy: