OpenGarage Forums OpenGarage Firmware Second Sensor for One Door 2 Car Reply To: Second Sensor for One Door 2 Car

#868

penright
Participant

Sorry took so long, setting up the home network. Never had access to a public IP before, I was behind 2 or 3 different NAT with the hotspot.
Anyway, tried the 3 wire again. Not sure what I am missing, maybe a second set of eyes.

So I cut the read_distance and read_distance_once straight from the OG. Then I call the read_distance from the loop. One sensor.
If I wired it for 4 wires I pass the correct PIN_TRIG/PIN_ECHO. If I wired it for 3 wires then I pass the same pin (PIN_TRIG/PIN_TRIG).
You can see where I can comment/uncomment to switch back and forth. Three wire gives me all zeros. It’s almost like the pin mode is not switching. I am linking to some pictures, but I don’t know if they are good enough to help.
Do you have something you could try and see if you get same results? Not sure if it matters, I am using a NodeMCU V3.


/** GPIO pins */
#define PIN_RELAY  15 //D8
#define PIN_BUTTON  0
#define PIN_TRIG   12 //D6
#define PIN_ECHO   14 //D5
#define PIN_LED     2
#define PIN_RESET  16
#define PIN_BUZZER 13 //D7
#define PIN_JP2_4  5 //Jumper 2 ping 4 which is GPIO 5 d1
#define PIN_JP2_3  4 //Jumper 2 ping 3 which is GPIO 4 d2 (Reserved for switch primary door)
#define PIN_JP2_6  0 //A0
#define pingPin    PIN_TRIG

void setup() {
  // put your setup code here, to run once:
   Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("Distance 1 ");
  //uint dist = read_distance(PIN_TRIG,PIN_ECHO); //Works
  uint dist = read_distance(PIN_TRIG,PIN_TRIG); //Returns 0
  
  Serial.print(dist);
  Serial.println("");
  delay(1000);
}

long read_distance_once(int TRIG, int ECHO) {
  //TODO handle max value as handled error in the UI - check long distance for car detection
  
  pinMode(TRIG,OUTPUT);
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  pinMode(ECHO,INPUT);
  // wait till echo pin's rising edge
  unsigned long quit_time=micros()+26233L;
  while((digitalRead(ECHO)==LOW)&& (micros()<quit_time));
  {//Do nothing
  };
  unsigned long start_time = micros();
  quit_time=start_time+26233L;
  //wait till echo pin's falling edge
  while((digitalRead(ECHO)==HIGH) && (micros()<quit_time));
  ulong lapse = micros() - start_time;
  if (lapse>26233L) lapse = 26233L;
  //DEBUG_PRINTLN(F("Distance issue, setting to low value"));
  //DEBUG_PRINT(lapse);
  Serial.print(" lapse ");
  Serial.print(lapse);
  Serial.print(" ");
  return lapse;
}

uint read_distance(int TRIG, int ECHO) {
  byte i;
  unsigned long _time = 0;
  // do three readings in a roll to reduce noise
  byte K = 3;
  for(i=0;i<K;i++) {
    _time += read_distance_once(TRIG, ECHO);
    delay(50);
  }
  _time /= K;
  //echo_time = _time;
  return (uint)(_time*0.01716f);  // 34320 cm / 2 / 10^6 s
}

4 Wire
3 Wire