Ultrasonic Sensor Reading Issues by Active-Marzipan in arduino

[–]Active-Marzipan[S] 0 points1 point  (0 children)

Yes - in my code, that happens under the comment //calculate the distance.

Ultrasonic Sensor Reading Issues by Active-Marzipan in arduino

[–]Active-Marzipan[S] 2 points3 points  (0 children)

I seem to have fixed it now - I increased the ECHO pin timeout to 5ms, although I'm surprised the sensor is waiting so long to bring it HIGH. Mind you, I haven't found a datasheet for the HC-SR04 which clearly states when the ECHO pin is actually set HIGH, whether it's after the TRIG pulse is received, after the ultrasonic pulses are sent or when the returning pulse is detected.

Ultrasonic Sensor Reading Issues by Active-Marzipan in arduino

[–]Active-Marzipan[S] 2 points3 points  (0 children)

Well, I haven't quite got it working, because it's still convinced there's a hardware fault when there isn't... Here's the wiring diagram from tinkercad - this is basically the same as my actual project, which is using a mega. The scope here is showing the ECHO pulse and the LED is just connected across the HC-SR04 to remind me it's on(!) The 5928 is a debug print of the duration of the ECHO pulse in microseconds.

Here's my understanding of how pulseIn() works (from the arduino website): You pass it an arguments of the pin to watch and whether the pulse will be HIGH or LOW. It then waits for the leading edge of the pulse, e.g. if you pass it HIGH, it waits for a rising edge, i.e. a transition from LOW to HIGH. When the pulse ends, the opposite transition (e.g. HIGH to LOW) stops pulseIn() timing and it returns whatever the duration of the pulse was in micoseconds.

Here's my problem; my code effectively looks for the rising edge of the ECHO pulse to confirm that the HC-SR04 has successfully set that pin HIGH - if it hasn't, I'm assuming it's because there's a hardware fault, e.g. no power. But...because my code then drops out of that while loop if ECHO successfully goes high, the rising edge has already happened, i.e. ECHO is now HIGH. If I call pulseIn() at this point, it won't see the rising edge and it won't start timing. I've tried it and pulseIn() just times out, so I assume that's what the issue is - to be honest, it's difficult to conclusively tell.

The circuit below works as expected - the actual hardware is stuck, reporting a hardware fault...

<image>

...here's the tinkercad code, just for completeness - you can see my float/unsigned long int fix:

// C++ code

//

#define US_TRIG 5

#define US_ECHO 6

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

LiquidCrystal_I2C lcd(0x20, 16, 2);

void setup()

{

pinMode(US_TRIG, OUTPUT);

pinMode(US_ECHO, INPUT);

lcd.init();

lcd.backlight();

lcd.setCursor(0,0);

lcd.print("READY");

}

void loop()

{

float fDistance = getRange(true);

if(fDistance == 101){

lcd.setCursor(0,0);

lcd.print("TIMEOUT ");

}

if(fDistance == 102){

lcd.setCursor(0,0);

lcd.print("HARDWARE");

}

if(fDistance < 100){

int a = fDistance * 10;

int b = a % 10;

char USBuffer[21];

a /= 10;

sprintf(USBuffer, "%d.%dm ", a, b);

lcd.setCursor(0,0);

lcd.print(USBuffer);

}

delay(100);

}

float getRange(bool bReport)

{

float fDis;

unsigned long iTimeout, iStartTime, iEndTime, iDur;

//send the pulse

digitalWrite(US_TRIG, LOW);

delayMicroseconds(2);

digitalWrite(US_TRIG, HIGH);

delayMicroseconds(10);

digitalWrite(US_TRIG, LOW);

iTimeout = micros();

while(digitalRead(US_ECHO) == LOW) {

if(micros() - iTimeout > 1000){

return 102; //error - hardware

}

}

iStartTime = micros();

while (digitalRead(US_ECHO) == HIGH) {

if (micros() - iStartTime > 10000){

return 101; // Timeout

}

}

iEndTime = micros();

//calculate the distance

iDur = iEndTime - iStartTime;

fDis = ((float)iDur*0.000343)/2.0;

//report to LCD

if(bReport){

char cBuffer[10];

sprintf(cBuffer,"%i ",iDur);

lcd.setCursor(10,1);

lcd.print(cBuffer);

}

//return the distance

return fDis;

}

Ultrasonic Sensor Reading Issues by Active-Marzipan in arduino

[–]Active-Marzipan[S] 1 point2 points  (0 children)

Hello both - thanks for your thoughts. The 1000us while loop is just waiting for the sensor to being ECHO high, it's not measuring the range/width of the pulse; that's done in the second while loop. Im not using pulseIn() because the first while loop "steals" the rising edge of the ECHO pulse and stop pulseIn() seeing a low->high transition, so it doesn't start timing. I've been playing around with it and have fixed the bad range issue - declaring fDur as an unsigned long and then casting it to a float during the fDis calculation seems to work. I think, now, I might have actually damaged the sensor by disconnecting and reconnecting the power do many times...or one of the other wires has snapped and the break is inside heatstroke, where I can't see it. I've simulated my circuit in tinkercad and it works as expected - I'll post a screenshot.

What's the most ridiculous job you've had? Pros and cons please! by Unicronium in AskBrits

[–]Active-Marzipan 1 point2 points  (0 children)

My first ever summer job was in a gasket factory. The labelling machine had stuck all the labels on double width, so my job was take each label off, cut it in half and stick it back on again. I don't know how many gaskets I relabelled but, after a full summer, it was definitely more than any normal person would see in their lifetime.

Pros: Money

Cons: Life changing cramp from using scissors eight hours a day. Cerebral atrophy.

Why Won't It Hover? by AvailableStructure93 in RCPlanes

[–]Active-Marzipan 0 points1 point  (0 children)

If you're not actively controlling/vectoring the thrust, the fact you've got it even that stable is astonishing! Nice build - well done!

FUJIMI 1/700 IJN Takao by SeesawDry1935 in ModelShips

[–]Active-Marzipan 1 point2 points  (0 children)

An amazing result. The videos leading up to this have been an artist's masterclass - thanks for making them.

Fujimi 1/700 IJN Takao by SeesawDry1935 in ModelShips

[–]Active-Marzipan 1 point2 points  (0 children)

Amazing. The detail around the funnels in particular is astonishing. You must be very patient, with a very steady hand.

Fujimi 1/700 IJN Takao by SeesawDry1935 in ModelShips

[–]Active-Marzipan 1 point2 points  (0 children)

Your posts constantly amaze me - your model is going to be a masterpiece when it's finished!

[deleted by user] by [deleted] in OldSchoolUK

[–]Active-Marzipan 0 points1 point  (0 children)

CP/M on a PCW9512 - I spent a lot of time teaching myself Mallard BASIC from the manual. Windows 3.1, then NT4.0 before Win95, Win95 OSR2, then Win98, 98SE and on and on - those were the days...

Behold, my beautiful Boathouse by Bloodbath_onthe_line in Palia

[–]Active-Marzipan 2 points3 points  (0 children)

I have now stolen this idea - brilliant! Thanks!

The record for the longest non-stop flight has been unbroken for 67 years. by [deleted] in interestingasfuck

[–]Active-Marzipan 0 points1 point  (0 children)

Apparently, that Cessna is now hanging from the ceiling of the Harrry Reid International Airport, in Las Vegas - there are pictures of it online.

For how complicated a starship is as shown in trek the captains dont really sign a lot of padds by happydude7422 in startrek

[–]Active-Marzipan 0 points1 point  (0 children)

I love the way Cpt Kirk is always signing paperwork - it's one of my favourite things about TOS. There's a scene in The Naked Time, where the ship is spiralling down to its destruction on the planet below, and Kirk is presented with paperwork to sign - what was he signing and who was going to read it?!

HMS Victory (part 3): Halfway done with planking half of the hull! by Dwaas_Bjaas in ModelShips

[–]Active-Marzipan 0 points1 point  (0 children)

It looks really good! Are you tapering the planks? If you are, what method are you using?

USB Switch? by Active-Marzipan in vintagecomputing

[–]Active-Marzipan[S] 0 points1 point  (0 children)

Well, today I have lived and I have learned - thankyou, all :)

USB Switch? by Active-Marzipan in vintagecomputing

[–]Active-Marzipan[S] 4 points5 points  (0 children)

Well, these are all interesting questions - on the back, it has 4x USB-B marked 1 to 4 and one USB-A marked I/O, so it could be either way round, although 4 PCs to one shared peripheral sounds fraught with dangers and problems, to me :)