Windows messed me up and now I'm here - DC motor , Sensors

Hello everyone

I come to you in defeat and still a little green to coding.

My Windows machine went down and lost some files :confused:

There's just one I'm trying to remake and I can't figure out how it was put together and still too annoyed or had a mini stroke (feels like it) from fixing the computer.
I'm not sure how I worked it out last time but would really like some help getting back on track please.

Trying to change a DC motor speed with input from thouse little ultrasound sensors to stop little robot build slamming into walls at speed .

Code below - This is just as far as I got to tonight and is only to test if it works before adding to full script and messing with speed\distance .

#define trigPin 10
#define echoPin 11 // Ultra sound
void FarEcho();
void NearEcho();
void Wait();
int Motor =A1;
float duration, distance;
void setup() {
pinMode (Motor, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0343;

if (distance <=20 and distance >=15)
{
FarEcho();
}
if (distance <=15 and distance >=5)
{
NearEcho();
}
else
{
Wait();
}
}

void FarEcho()
{
analogWrite (Motor, 170);
delay(100);
}
void NearEcho()
{
analogWrite (Motor, 135);
delay(100);
}
void Wait()
{
delay(100); //Robot stop and scan
}

Again sorry guys I know it's silly, was just so angry.
All the wireings done and I know it was working before it's just the code I lost .

Thank you for reading and sorry if I've missed something - or gone about this in a silly way .

"if (distance <=15 and distance >=5)"

Silly indeed.

Well it's just so I can wave my hand and check it works ATM

melongrenade:
Well it's just so I can wave my hand and check it works ATM

The code mentioned in Reply #1 can never be true - think about it.

EDIT ... Another of my Senior Moments see Replies #5 and #6. Apologies for being stupid.

When posting code please use the code button </>

...R

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

"if (distance <=15 and distance >=5)"

Maybe I have had too much sun but I don't see a problem with that. If, for example, distance were 8 that would be true.

PerryBebbington:

"if (distance <=15 and distance >=5)"

Maybe I have had too much sun but I don't see a problem with that. If, for example, distance were 8 that would be true.

I think it was I who have had too much sun.

I guess if I was writing it I would have put it the other way round

if (distance >= 5 and distance <= 15)

...R

"if (distance <=15 and distance >=5)"

Would work better as OR:

"if (distance <=15 or distance >=5)"

Otherwise, can never be true.

Robin2:
I think it was I who have had too much sun.
...R

:slight_smile:

I looked at it over and over again and could not see anything wrong with it, and at the same time I believe you generally know what you are talking about :slight_smile:

Sorry just got back in

Bit guilty or copying ultra sound code :wink: Unsure but think it helps with travel and bounce times ?

"distance = (duration / 2) * 0.0343;"

So just to clear things up there's nothing wrong with the code exept for some bad arguments and sloppy structure ? I'll have another look later tonight and see what's going on .

Thank you and sorry Tom , I don't use fourms just got desprate

CrossRoads:
"if (distance <=15 and distance >=5)"

Would work better as OR:

"if (distance <=15 or distance >=5)"

Otherwise, can never be true.

It sure can. Read the other replies. Substitute 8 for distance, for example:

8 <= 15: true
8 >= 5: true
true and true: true

I would personally write it in a way that resembles the mathematical comparison "5 <= distance <= 15":

if (5 <= distance and distance <= 15)

Ok had a look last night and it still didn't run. In the end got out another arduino and it seems like the ultra sonic sensor is not giving any readings . So waiting on parts to see if that's all that's wrong with it.

Have changed the AND to an OR as it seemed to drive people here up the wall :grinning:

Will let you know if it's working and\or not

Change it back to the and. If you use or, it's equivalent to if(true).

Hi,
Write some code JUST to read the sensor, forget about the motor for the moment.

Use serial.print and the IDE monitor to show your sensor readings.

Have you tried the NewPing library and its examples to read data from the sensor?

Thanks.. Tom... :slight_smile:

Hey

Update

Right changed the sound sensor and got no results back (used monitor mode Tom :wink: )
Then thought it was my LiOp BPB, Changed that.

Turns out it was the Arduino after my multi meter had a weak reading. boost converter sent too much power - oops :sweat_smile:

Code and everything else is working now so a happy bunny . Sorry for any drama, was just a little loopy\defeated with lockdown and Windows throwing a wobbler when I needed it.

Thank you

Thank you for the update and glad you got it working.
This:

Boost converter sent too much power

Is nonsense. Boost converters (or any other kind of power supply) do not 'send' power. I think you mean you had the voltage set too high.