Kann mir jemand sagen, wie man mehrere HC-SR04 auslesen kann? Ich bekomme es zwar mit einem hin, aber mit mehreren klappt es einfach nicht
Außerdem kommt immer diese Fehlermeldung :
Ping_4.cpp.o: In function __static_initialization_and_destruction_0': /Applications/Ping_4.ino:3: undefined reference to Ultrasonic::Ultrasonic(int, int)'
/Applications/Ping_4.ino:4: undefined reference to Ultrasonic::Ultrasonic(int, int)' /Applications/Ping_4.ino:5: undefined reference to Ultrasonic::Ultrasonic(int, int)'
/Applications/Ping_4.ino:6: undefined reference to Ultrasonic::Ultrasonic(int, int)' Ping_4.cpp.o: In function loop':
/Applications/Ping_4.ino:17: undefined reference to Ultrasonic::Ranging(int)' /Applications/Ping_4.ino:22: undefined reference to Ultrasonic::Ranging(int)'
/Applications/Ping_4.ino:27: undefined reference to Ultrasonic::Ranging(int)' /Applications/Ping_4.ino:32: undefined reference to Ultrasonic::Ranging(int)'
Hier mein Code:
#include "Ultrasonic.h"
Ultrasonic sonicF(12,11);
Ultrasonic sonicB(3,4);
Ultrasonic sonicL(5,7);
Ultrasonic sonicR(6,8);
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
int distf, distb, distl, distr;
Serial.print("Pingfront: ");
distf=sonicF.Ranging(CM);
Serial.print(distf);
Serial.println("cm");
delay(5);
Serial.print("Pingback: ");
distb=sonicF.Ranging(CM);
Serial.print(distb);
Serial.println("cm");
delay(5);
Serial.print("Pingleft: ");
distl=sonicF.Ranging(CM);
Serial.print(distl);
Serial.println("cm");
delay(5);
Serial.print("Pingright: ");
distr=sonicF.Ranging(CM);
Serial.print(distr);
Serial.println("cm");
delay(5);
delay(1000);
}
und hier der Code der Ultrasonic.h
// Ultrasonic - Library for HR-SC04 Ultrasonic Ranging Module.
// Rev.4 (06/2012)
// J.Rodrigo ( http://www.jra.so )
// more info at http://www.ardublog.com
// GitHub: https://github.com/elrodri/Ultrasonic-HC-SR04
#ifndef Ultrasonic_h
#define Ultrasonic_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define CM 1
#define INC 0
class Ultrasonic
{
public:
Ultrasonic(int TP, int EP);
Ultrasonic(int TP, int EP, long TO);
long Timing();
long Ranging(int sys);
private:
int Trig_pin;
int Echo_pin;
long Time_out;
long duration,distacne_cm,distance_inc;
};
#endif
erni-berni:
Lad den File Ultrasonic.cpp mal in einen weiteren TAB. Dann gings bei mir.
Alternativ Lib im Folder Library installieren.
Wenn ich die Ultrasonic.cpp mit rein packe dann krieg ich diese Fehlermeldungen:
Ultrasonic.cpp:41: error: 'Ultrasonic' has not been declared
Ultrasonic.cpp:41: error: ISO C++ forbids declaration of 'Ultrasonic' with no type
Ultrasonic.cpp: In function 'int Ultrasonic(int, int)':
Ultrasonic.cpp:43: error: 'OUTPUT' was not declared in this scope
Ultrasonic.cpp:43: error: 'pinMode' was not declared in this scope
Ultrasonic.cpp:44: error: 'INPUT' was not declared in this scope
Ultrasonic.cpp:45: error: '_trigPin' was not declared in this scope
Ultrasonic.cpp:46: error: '_echoPin' was not declared in this scope
Ultrasonic.cpp:47: error: '_cmDivisor' was not declared in this scope
Ultrasonic.cpp:48: error: '_inDivisor' was not declared in this scope
Ultrasonic.cpp: At global scope:
Ultrasonic.cpp:51: error: 'Ultrasonic' is not a class or namespace
Ultrasonic.cpp: In function 'long int timing()':
Ultrasonic.cpp:53: error: '_trigPin' was not declared in this scope
Ultrasonic.cpp:53: error: 'LOW' was not declared in this scope
Ultrasonic.cpp:53: error: 'digitalWrite' was not declared in this scope
Ultrasonic.cpp:54: error: 'delayMicroseconds' was not declared in this scope
Ultrasonic.cpp:55: error: 'HIGH' was not declared in this scope
Ultrasonic.cpp:58: error: '_echoPin' was not declared in this scope
Ultrasonic.cpp:58: error: 'pulseIn' was not declared in this scope
Ultrasonic.cpp: At global scope:
Ultrasonic.cpp:61: error: 'Ultrasonic' is not a class or namespace
Ultrasonic.cpp: In function 'float convert(long int, int)':
Ultrasonic.cpp:64: error: '_cmDivisor' was not declared in this scope
Ultrasonic.cpp:66: error: '_inDivisor' was not declared in this scope
Ultrasonic.cpp: At global scope:
Ultrasonic.cpp:69: error: 'Ultrasonic' is not a class or namespace
Ultrasonic.cpp: In function 'void setDivisor(float, int)':
Ultrasonic.cpp:71: error: '_cmDivisor' was not declared in this scope
Ultrasonic.cpp:72: error: '_inDivisor' was not declared in this scope
Jetzt bekomm ich das als Antwort :
Abstand 1 ~ ¹ mm
Abstand 2 ~ þ mm
Abstand 3 ~ « mm
Abstand 4 ~ ø mm
Abstand 1 ~ ? mm
Abstand 2 ~ 7 mm
Abstand 3 ~ 1 mm
Abstand 4 ~ A mm
Abstand 1 ~ , mm
Abstand 2 ~ ] mm
Abstand 3 ~ ¸ mm
Abstand 4 ~ ± mm
Hatte da was falsch im Code. Anstatt
Serial.print(distance)
hatte ich
Serial.print(distance,0)
Jetzt bekomm ich diese Werte:
Abstand 1 ~ -1317 mm
Abstand 2 ~ 30 mm
Abstand 3 ~ 33 mm
Abstand 4 ~ 0 mm
Abstand 1 ~ 89 mm
Abstand 2 ~ 30 mm
Abstand 3 ~ 33 mm
Abstand 4 ~ 0 mm
Abstand 1 ~ -1445 mm
Abstand 2 ~ -1642 mm
Abstand 3 ~ 4332 mm
Abstand 4 ~ 0 mm
#ifndef NewPing_h
#define NewPing_h
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
// Sollte man nicht andern außer du musst es.
#define MAX_SENSOR_DISTANCE 500 // Maximum sensor distance can be as high as 500cm, no reason to wait for ping longer than sound takes to travel this distance and back.
#define US_ROUNDTRIP_IN 146 // Microseconds (uS) it takes sound to travel round-trip 1 inch (2 inches total), uses integer to save compiled code space.
#define US_ROUNDTRIP_CM 57 // Microseconds (uS) it takes sound to travel round-trip 1cm (2cm total), uses integer to save compiled code space.
#define DISABLE_ONE_PIN false // Set to "true" to save up to 26 bytes of compiled code space if you're not using one pin sensor connections.
// Sollte man nicht andern außer du weißt was du tust.
#define NO_ECHO 0 // Value returned if there's no ping echo within the specified MAX_SENSOR_DISTANCE or max_cm_distance.
#define MAX_SENSOR_DELAY 18000 // Maximum uS it takes for sensor to start the ping (SRF06 is the highest measured, just under 18ms).
#define ECHO_TIMER_FREQ 24 // Frequency to check for a ping echo (every 24uS is about 0.4cm accuracy).
#define PING_MEDIAN_DELAY 29 // Millisecond delay between pings in the ping_median method.
// Konvertieren von uS in Entfernung (round result to nearest cm or inch).
#define NewPingConvert(echoTime, conversionFactor) (max((echoTime + conversionFactor / 2) / conversionFactor, (echoTime ? 1 : 0)))
class NewPing {
public:
NewPing(uint8_t trigger_pin, uint8_t echo_pin, int max_cm_distance = MAX_SENSOR_DISTANCE);
unsigned int ping();
unsigned int ping_in();
unsigned int ping_cm();
unsigned int ping_median(uint8_t it = 5);
unsigned int convert_in(unsigned int echoTime);
unsigned int convert_cm(unsigned int echoTime);
void ping_timer(void (*userFunc)(void));
boolean check_timer();
unsigned long ping_result;
static void timer_us(unsigned int frequency, void (*userFunc)(void));
static void timer_ms(unsigned long frequency, void (*userFunc)(void));
static void timer_stop();
private:
boolean ping_trigger();
boolean ping_wait_timer();
uint8_t _triggerBit;
uint8_t _echoBit;
volatile uint8_t *_triggerOutput;
volatile uint8_t *_triggerMode;
volatile uint8_t *_echoInput;
unsigned int _maxEchoTime;
unsigned long _max_time;
static void timer_setup();
static void timer_ms_cntdwn();
};
#endif
Die funktioniert ganz Gut, aber ich weiß nicht wie ich diese mit mehreren Ultraschall Sensoren nutzen kann. Vielleicht weiß da jemand was.
Hier noch der passende Code dazu:
#include <NewPing.h>
#include <Settings.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
// the loop routine runs over and over again forever:
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
So hab mich mal hingesetzt und ein bisschen nachgedacht. Letztenendes bin ich zu diesem code gekommen, der auch funktioniert:
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN2 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN3 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN3 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN4 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN4 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing pivo(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing pihi(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing pili(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing pire(TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
// the loop routine runs over and over again forever:
void loop() {
delay(35); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uSvo = pivo.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping vorn: ");
Serial.print(uSvo / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
delay(5);
unsigned int uShi = pihi.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping hinten: ");
Serial.print(uShi / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
delay(5);
unsigned int uSli = pili.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping links: ");
Serial.print(uSli / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
delay(5);
unsigned int uSre = pire.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping rechts: ");
Serial.print(uSre / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}