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.
#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:
//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 ![]()