Go Down

Topic: communicating two arduino's and getting readings of one distance-sensor. (Read 74 times) previous topic - next topic

dragonvlame

Hi!

For a project I am working on I have two arduino's which work with the same sensor, an distance sensor, mostly called a PING-sensor (I do not have the original PING sensor, but the HC-SR04)

I want one arduino to get servos running when the distance is less than one meter. The other arduino maps the distance to the LED-brightness of a bunch of LEDS.

The trouble with a HC-SR04 is that it sends a signal with one pin, and reads it with another, so just splitting the signal between two arduino's won't work. How can I have the arduino's to communicate the data from the sensor?

I have some code, but it's really long, so I will cut it a bit :)

For this code I want the servos to move when the distance is less than one meter.


Code: [Select]
#include <Servo.h>

Servo head1; //hoofd omhoog en omlaag
Servo head2; //hoofd opzij
Servo elbow1;// rechter elleboog omhoog/omlaag
Servo elbow2;//linker elleboog omhoog/omlaag
Servo hand1;//rechterhand omhoog/omlaag
Servo hand2;//linkerhand omhoog/omlaag

int pos = 90;    // variable to store the servo position

//afstandsmeter
const int influence = 150;
const int echoPin = 7;
const int trigPin = 8;
int maximumRange = 500;
int minimumRange = 0;
long duration, distance;

int obstacle = afstand();

void setup(){
head1.attach(11);
head2.attach(10);
elbow1.attach(9);
elbow2.attach(6);
hand1.attach(5);
hand2.attach(3);

//set everything to neutral position
  head1.write(90);
  head2.write(90);
  elbow1.write(0);
  elbow2.write(0);
  hand1.write(180);
  hand2.write(0);

//afstandssensor
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}

void loop(){
  delay(100);
  afstand(); 
  Serial.println(distance);
  delay(5000);
 
  head1.write(180);
}



//afstandssensor
long afstand(){
  /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 distance = constrain(distance, 0, 500);
 
 return duration / 58.2;
 
}



For the next all I want to do is map the distance to the LED-brightness:

Code: [Select]
//afstandsmeter

const int echoPin = 7;
const int trigPin = 8;
int maximumRange = 150;
int minimumRange = 0;
long duration, distance;
int afstand2 = afstand();


//lichten
int blauw12Pin = 11;
int blauw34Pin = 10;
int blauw56Pin = 9;
int blauw78Pin = 6;
int wit12Pin = 5;
int wit34Pin = 3;




void setup() {
  Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 
 
 //lichten
 pinMode(blauw12Pin, OUTPUT);
 pinMode(blauw34Pin, OUTPUT);
 pinMode(blauw56Pin, OUTPUT);
 pinMode(blauw78Pin, OUTPUT);
 pinMode(wit12Pin, OUTPUT);
 pinMode(wit34Pin, OUTPUT);
 
 digitalWrite(wit12Pin, HIGH);
 digitalWrite(wit34Pin, HIGH);

}

void loop() {
  delay(500);
  afstand();
  Serial.println(distance);
 
  int ledLevel = map(distance, 0, 150, 255, 0);
 
  //Lichtlevel
  analogWrite(blauw12Pin, ledLevel);
  analogWrite(blauw34Pin, ledLevel);
  analogWrite(blauw56Pin, ledLevel);
  analogWrite(blauw78Pin, ledLevel);
 
 
 
  delay(500);
 
}

long afstand(){
  /* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 

 distance = constrain(distance, 0, 150);
 
 
 return distance;
 
 
}


These are seperate codes, but I want to combine them. I have to use two arduinos because of the number of PWM pins I need....

Please help :)

PaulS

Quote
How can I have the arduino's to communicate the data from the sensor?
Using what mechanism? I2C? SPI? Serial?

Code: [Select]
int obstacle = afstand();
The afstand() function returns a distance, not an obstacle.

chucktodd

Hi!

For a project I am working on I have two arduino's which work with the same sensor, an distance sensor, mostly called a PING-sensor (I do not have the original PING sensor, but the HC-SR04)

Just control the PING sensor from one of the Arduino's, Connect the Arduinos using the I2C buss.
Configure the PING Ardunio as an I2C SLAVE, have the PING Arduino constantly updating the distance variable.
when the other Arduino (I2C Master) requests the data the slave would sendit.

See my Master/Slave I2C example

In this example you would replace the code in the loop() of the slave to control the PING sensor, just update the foo with the new distance value.

in the Master example just don't send a new value to the Slave, Just read the current value.

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560's.  It adds 1MB of RAM for those projects where 8KB is not enough.

Go Up