I've looked all over google and all over the forum but i cant find anything that relates to this problem.
I'm trying to create 2 separate ultrasonic sensors, but when i try this code it doesn't work
class UltrasonicSensor {
const int ultrasonicTrig;
const int ultrasonicEcho;
public:
UltrasonicSensor(int pin1, int pin2) : ultrasonicTrig(pin1), ultrasonicEcho(pin2) {
}
void setup() {
pinMode(ultrasonicTrig, OUTPUT);
pinMode(ultrasonicEcho, INPUT);
}
void loop() {
}
long getDistance() {
long duration;
long cm;
//Trigger short LOW pulse, clean HIGH pulse on first ultrasonic sensor
digitalWrite(ultrasonicTrig1, LOW);
delayMicroseconds(5);
digitalWrite(ultrasonicTrig1, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrig1, LOW);
//Gets the time taken for the pulse to come back to the echo
duration = pulseIn(ultrasonicEcho1, HIGH);
//Convert time into distance
cm = (duration/2) / 29.1;
delay(100);
return cm;
}
};
UltrasonicSensor sensorLeft = new UltrasonicSensor(5, 4);
UltrasonicSensor sensorRight = new UltrasonicSensor(3, 2);
void setup() {
// put your setup code here, to run once:
sensorLeft.setup();
sensorRight.setup();
Serial.begin(9600); //Initiate Serial Monitor for debugging
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(String(sensorRight.getDistance()) + " cm " + String(sensorLeft.getDistance()) + " cm");
}
I get the error when i create the right hand sensor and i don't understand why. Literally any explaination would be helpful because i'm completely at a loss