sonar fix + formatting
@@ -345,6 +345,7 @@ void setup()
pinMode(PIN_PWM_R1, OUTPUT);
pinMode(PIN_PWM_R2, OUTPUT);
#endif
+
//Initialize with the I2C addr 0x3C
#if HAS_OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
@@ -379,8 +380,8 @@ display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
digitalWrite(PIN_LED_RI, LOW);
#endif
#if (HAS_SONAR)
- pinMode(PIN_TRIGGER, OUTPUT);
pinMode(PIN_ECHO, INPUT);
+ pinMode(PIN_TRIGGER, OUTPUT);
#endif
#if (HAS_VOLTAGE_DIVIDER)
pinMode(PIN_VIN, INPUT);
@@ -1165,12 +1166,14 @@ void send_ping()
{
echo_time = 0;
ping_success = false;
+ if (PIN_TRIGGER == PIN_ECHO) pinMode(PIN_TRIGGER, OUTPUT);
digitalWrite(PIN_TRIGGER, LOW);
delayMicroseconds(5);
digitalWrite(PIN_TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIGGER, LOW);
- attachInterrupt(digitalPinToInterrupt(PIN_ECHO), start_timer, RISING);
+ if (PIN_TRIGGER == PIN_ECHO) pinMode(PIN_ECHO, INPUT);
+ attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_ECHO), start_timer, RISING);
}
void update_distance_estimate()
@@ -1195,13 +1198,13 @@ void update_distance_estimate()
void start_timer()
{
start_time = micros();
- attachInterrupt(digitalPinToInterrupt(PIN_ECHO), stop_timer, FALLING);
+ attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_ECHO), stop_timer, FALLING);
}
// ISR: Stop timer and record the time
void stop_timer()
{
echo_time = micros() - start_time;
- detachInterrupt(digitalPinToInterrupt(PIN_ECHO));
+ detachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_ECHO));
ping_success = true;
}
#endif
