Troubleshooting with two Distancesensors

Co-Posted by Theresa

Today we tried to work with two NeoPixel stripes and two Distancesensors.

 

First we only wanted to find out how heavy the electrical components were if we put them on the body and how they would influence moves of a dancer. We put two NeoPixel stripes, a batterycase with 4 AA-batteries, a Distancesensor and a bunch of crocodile-wires in a little cotton sac and wore it.
You can see the results on how the sensor reacts when worn sideways on the back in the video below.

During the midterm presentations discussion it was suggested that we put two Distancesensors on the back of the body which are orientated to either side of the room. In an actual stage situation people that aproach from behind never actually approach directly from behind but rather from either side of the stage, because otherwise they wouldn’t be seen by the audience. Our situation will be a stage situation as well, which is why we decided to include a second sensor in our concept.

Unfortunately we didn’t get the results we hoped we would get. Even though we connected the sensors parallel to each other like they did on this site, we just received an output from one of the sensors while the other only gave us zeros. We swaped both sensors to make sure that both of them are working fine. It turned out that the problem must be something else, so we tried to change all the wires in question. This did not solve the problem either, which we doubted the functionality of the pins. After changing the input pins to some that were working we still did not get a signal out of one of the sensors while the other was still working fine. Proofreading our code several times didn’t give us any hint that it might be wrong.
We were using the following code:

#include <Adafruit_NeoPixel.h>
#define PIN_1 12
#define PIN_2 13
#define N_LEDS 30
#define Echo 5
#define Trigger 6
#define Echo_2 7
#define Trigger_2 8

Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(N_LEDS, PIN_1, NEO_GRBW + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(N_LEDS, PIN_2, NEO_GRBW + NEO_KHZ800);

long duration, distance, duration_2, distance_2;

void setup() {
Serial.begin(9600);
pinMode(Trigger, OUTPUT);
pinMode(Trigger_2, OUTPUT);
pinMode(Echo, INPUT);
pinMode(Echo_2, INPUT);

strip1.begin();
strip2.begin();
strip1.show();
strip2.show();
}

void loop() {
digitalWrite(Trigger, LOW);
delayMicroseconds(2);
digitalWrite(Trigger_2, LOW);
delayMicroseconds(2);

digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger_2, HIGH);
delayMicroseconds(10);

digitalWrite(Trigger, LOW);
digitalWrite(Trigger_2, LOW);
duration = pulseIn(Echo, HIGH);
duration_2 = pulseIn(Echo_2, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
distance_2 = duration_2/58.2;

Serial.println(distance);
Serial.print(“Distance2: “);
Serial.println(distance_2);

if (distance <= 90){
chase(strip1.Color(0, 0, 0, 255)); // White
}
if (distance_2 <= 90) {
chase(strip1.Color(0, 0, 255)); // Blue
}
}

static void chase(uint32_t c) {
for(uint16_t i=0; i<strip1.numPixels()+5; i++) {
strip1.setPixelColor(i , c); // Draw new pixel
strip1.setPixelColor(i-5, 0); // Erase pixel a few steps back
strip2.setPixelColor(i , c);
strip2.setPixelColor(i-5, 0);
strip1.show();
strip2.show();
delay(20);
}
Update:
After trying to get two Distancesensors working with the Arduino Uno, we somehow managed to get a response from
both sensors. What exactly the problem was isn’t really clear up till now, but it could realy be related to the pins we were
using.
Our first attempt on the Arduino Uno didn’t work out either, but after switching pins on the Uno both sensors worked.
Afterwards we tried both sensors again with a different Lilypad and suddenly everything worked out perfectly.

Here is the code which we were using in the end:


#define Echo_1 6
#define Echo_2 9
#define Trigger_1 5
#define Trigger_2 8

long duration, distance, Left, Right; // Duration used to calculate distance

void setup() {
Serial.begin (9600);
pinMode(Trigger_1, OUTPUT);
pinMode(Echo_1, INPUT);
pinMode(Trigger_2, OUTPUT);
pinMode(Echo_2, INPUT);
}

void loop() {
SonarSensor(Trigger_1,Echo_1);
Left = distance;
SonarSensor(Trigger_2,Echo_2);
Right = distance;

Serial.println(Left);
Serial.println(Right);
Serial.println();
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}