HELP! Sonar Sensing DC Motors!

Hi all,

I have been trying to so hard for nearly a week to get this simple bit of code working!

I am in dire need of help, being a noob to the programming side of things ecspecially.

All I want is to send 5V of power from my leftPin and rightPin digital pins when the echoPin reads a value higher than a set amount.

My Sensor is a US-020 and has a vcc, grn, trig and echo pins!

If any one could help me with this code then it would be much appreciated.

At this stage it DOES verify.

Thanks in advance,

Dan

const int echoPin = 10; //const because it is Read only

int val = 0;
int leftPin = 3;
int rightPin = 4;
int trigPin = 12; //because it changes
long duration; //to calculate distance
long HR_dist = 0; //calculated distance

void setup()
{
  Serial.begin (9600); //establish comms
  pinMode(echoPin, INPUT);
  pinMode(leftPin, OUTPUT);
  pinMode(rightPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
}

void loop()
{
  {
    long duration;
  }
  {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);
  }
  
  {
    duration = pulseIn(echoPin, HIGH);
    val = digitalRead(echoPin);
    if (val >=30)
    digitalWrite (leftPin, HIGH);
    else
    digitalWrite (leftPin, LOW);
  }
}
   val = digitalRead(echoPin);
    if (val >=30)

val will only EVER have the value of 0 or 1 (high or low).

Mark

  {
    long duration;
  }

Cute. A local variable that immediately goes out of scope because of useless curly braces.

  {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);
  }

More useless curly braces.

  {
    duration = pulseIn(echoPin, HIGH);
    val = digitalRead(echoPin);
    if (val >=30)
    digitalWrite (leftPin, HIGH);
    else
    digitalWrite (leftPin, LOW);
  }

And more.

Why are you reading the echo pin? The value that you want to use in the if statement is duration.

So besides starting and finishing the whole void loop section, is there any other place within the loop that I would need to use the curly braces?

I thought it grouped the code together so that it didn’t jump too far forward without checking all the parameters within those brackets?

Thank you very much for replying and helping me out.

So once I tell it to measure something I can get it to read it? I thought digitalRead was only for reading a pin value? So if I say that instead of reading 0-1023 or whatever a digital pin can read, I can use an equation so that 0-1023 is represented in CMs and then used as a duration or whatever so that a value of 50 does equal 50 out of 1023 but actually represents 50CMs?

So I should be saying:

if duration<30
digitalWrite(leftPin, HIGH);
else
digitalWrite(leftPin, LOW);

Sorry if I’m complicating something simple, but I am just wondering if I am thinking along the right lines.

is there any other place within the loop that I would need to use the curly braces?

Yes. Whenever there is a block of code that should be executed, such as after an of statement, a for statement, a while statement.

while(curlyBracesUsesful)
{
   // Do some stuff
}

I thought it grouped the code together so that it didn't jump too far forward without checking all the parameters within those brackets?

You lost me there with the technical jargon.

So if I say that instead of reading 0-1023 or whatever a digital pin can read

HIGH or LOW (1 or 0)

I can use an equation so that 0-1023 is represented in CMs and then used as a duration or whatever so that a value of 50 does equal 50 out of 1023 but actually represents 50CMs?

I don't know why you are fixated on 0 to 1023. The sensor you are using measures time of flight (how long the sound wave takes to travel to, and back from, an object). There is no 0 to 1023 range involved. It can't, obviously, take less than 0 time, but it could take two weeks, if the nearest object is far enough away.

So, no, 50 out of 1023 is meaningless.

You say whenever there is a block of code that should be executed...

Won't it execute anyway as long as it is below where you want it to execute? Or does it basically stop what it is doing when it encounters the bracket, execute what is in the bracket, then move on?

Thank you very much for you help!

Are you able to write the code so that it works for me?

So do digital pins simply either turn on or off and it is the rate of which that we utilise as values, etc?

Analogue pins are the ones that have a sort of range are they?

You say whenever there is a block of code that should be executed…

Won’t it execute anyway as long as it is below where you want it to execute? Or does it basically stop what it is doing when it encounters the bracket, execute what is in the bracket, then move on?

No.

if(i < 30)
   digitalWrite(pin1, LOW);
   digitalWrite(pin2, HIGH);

The pin named in pin2 will always be set HIGH.

if(i < 30)
{
   digitalWrite(pin1, LOW);
   digitalWrite(pin2, HIGH);
}

Here, the pin named in pin2 will only be set HIGH if i is less than 30.

The curly braces surround code that is to be executed as a block. Putting curly braces around code that is not a block isn’t harmful. It just usually indicates that the coder didn’t understand when they were, or were not, necessary.

Are you able to write the code so that it works for me?

Yes. I’ve already told you how, though. If deleting the digitalRead() that values var, and the use of duration instead of val in the if statement doesn’t work, then there is a hardware problem.

So do digital pins simply either turn on or off

INPUT pins are either on or off, based on external stimuli. OUTPUT pins are either on or off based on what you told them to do.

and it is the rate of which that we utilise as values, etc?

It is usually the interval or the exact state that is of interest, rather than the rate of change. If you want to know the rate of change, you have to determine that based on the interval, anyway.

WOW!

It's 2AM here in Australia at the moment. Will get onto this tomorrow!

Thank you so much for taking the time to write so much and explain everything I asked!

:smiley:

Woohoo!

I got it working.

God it's so rewarding when you actually get something working the way that you want it!

Thank you very much for your help!

Now the problem I am having is running my Arduino Uno off battery power

It works fine when using the usb port to upload code and run the unit. But if I disconnect the usb cable and then plug a battery pack in the power socket of the Arduino, one RED LED stays steady and another blinks, I think, three times with a pause inbetween and the Arduino does not do anything.

I'm guessing there is a simple explanation as to why my board is doing this?

I have an R3 UNO!

Thanks! :smiley:

Hi, what size battery and where are you connecting it.

Tom… :slight_smile:

Hey Tom,

Yeah I did some research and realised that the cheap alkaline AAs or 9V batteries do not provide anywhere near enough current to power motors, etc.

So I charged up some AAs I have lying around which are rated at 2450mAh. More than enough to run and now it does work.

I'm going to take the next step, now that I have it stopping and starting as a very basic test. I will put four sonar sensors for each side of the robot car and by using two dc motors make it drive around the house without running into anything.

I also need it to be able to reverse out of a corridor if need be, to ensure that it CAN NOT hit anything.

After that then I will put another two wheels on the car to sort of represent a quads four rotors.

Then I will start the final goal which is to get the car to track a wifi signal or the likes which will ultimately be me walking around the house, etc.

I guess to mount the sensors properly I am going to need to get some thin flat alloy sheet from work and just make some L brackets. Is there anything like a PCB spacer I can buy to space the sensors away from the alloy so that they do not short on the brackets? Otherwise I will just put some rubber or elec tape on them.

Thanks! :smiley:

Hi, Dan
No problem, good to see its running and you are implementing things a step at a time.
I’ll be keen to see how you go with 4 sensors, I have one here I’m about to put on chassis.
Good-luck Dan.

Tom… :slight_smile: (Victoria Australia)