Go Down

Topic: Newbie Help (Read 176 times) previous topic - next topic

Seaviator

I'm just starting to understand programming in general and I attempted to make a simple set up where an ultrasonic sensor turned on and off a buzzer depending on weather or not something was in front of the sensor or not. I attached a picture of my wiring and here is the code I created.




#include <SR04.h>


#define TRIG_PIN 3
#define ECHO_PIN 2
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
   Serial.begin(9600);
   delay(1000);
   pinMode(5,OUTPUT);
}

void loop() {
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(1000);
loop();{
   if(a<30){
    digitalWrite(5,HIGH);
   }
   else(a>30);{
    digitalWrite(5,LOW);
   }
}
}


Thanks for the help in advance!

slipstick

Does it compile? If not what errors do you get?

Does it work? If not what does it do?

And what do you expect the second loop() to do?

Steve

Delta_G

Code: [Select]
loop();

That line calls loop from loop.  This is called recursion.  It's an awesome tool in the hands of those who know what they're doing.  It is a disaster in the hands of those who don't.  You should never ever ever be calling the loop function from inside the loop function.  The code after that point where the comparisons happen is never getting called.  Each time loop only gets to the point where it calls itself and then starts over a new one.  None of them ever finish.  Eventually the stack overflows and the whole program crashes. 
|| | ||| | || | ||  ~Woodstock

Seaviator

Thanks for the great tips! I added the second loop in an attempt to fix the problem, obviously it didn't work.

The program does compile and the serial moniter shows that the ultrasonic sensor is working. I think I have a pretty good grasp of how to use those elements but what I can't get it to do is make the buzzer go off. I tested the buzzer and it does actually work. I think my problem may be that I don't fully understand digital pins and what they can do.

right now I've got the TRIG and ECHO pins hooked up to digital pins 2 and 3 and I'm trying to output a HIGH signal from pin 5 to the buzzer so that it goes off if something is 30cm or closer to the ultrasonic sensor.

Does that help at all?

JMeller

Your digital output control code is fine; I believe Delta_G was hinting to the alternative below to correct your issue:

void loop() {
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(1000);
getDistance(a);
}

void getDistance(long distance){
   if(a<30){
    digitalWrite(5,HIGH);
   }
   else(a>30);{
    digitalWrite(5,LOW);
   }
}

Delta_G

That's not quite what I had in mind.  If I wrote a function called getDistance then you could be sure that the one thing it would definitely do is get a distance and return it so it couldn't be declared void. 

Thanks for the great tips! I added the second loop in an attempt to fix the problem, obviously it didn't work.
What was the problem you were trying to fix there? 



I think you need to do a little bit of time with some C++ tutorials.  Just the basics.  You can stop when they start getting to classes and structs and oop and stuff.  But you need to learn what an if else is supposed to look like and what the semicolons mean and where they have to go and where they should never go. 

For instance, there's a lot wrong with this line. 

Code: [Select]
else(a>30);{

That says, else (as in if the if before it wasn't true) compare to see if a is less than 30, then throw that result away and do nothing.  Then there is an unconditional code block that turns pin 5 LOW regardless of the value of a.  So if the pin 5 was ever on it would only be for a tiny fraction of a millisecond before this line turned it back off. 

an else statement

1.  Shouldn't have a condition.  It is the one that gets run when all the other conditions are false.  If you want a condition there make it "else if"

2.  Shouldn't have a semicolon for sure. 


 
|| | ||| | || | ||  ~Woodstock

JMeller

Quote
That's not quite what I had in mind.  If I wrote a function called getDistance then you could be sure that the one thing it would definitely do is get a distance and return it so it couldn't be declared void. 
lol - simplicity so the n00b can follow the small change; I was there (and, in many respects, I'm still there).   I didn't catch the semicolon - it takes a qualified trained eye.  After solving the OP's issue, I'd like to learn what you meant by
Quote
... get a distance and return it so it couldn't be declared void.
I'll have to look it up; I assume it is similar to:

Private Function (ByVal a As Integer)

....
Return X

End Function

Seaviator

oh I think I understand my mistake now. As a note the ; after the else statement was put there in an attempt to get it to compile and so was the extra loop. I have taken a couple of those free online courses for C++ and I think I've got most of the syntax in concept but I'm having trouble using the code to interact with the arduino and more then one other module chip, in other words getting the different inputs and outputs to interact with eachother. If anyone has a good resourse for learning this stuff that would be awesome!

That being said the error that led to me add all that random junk I put in there was that I didn't add an amount of time that the buzzer should be active for. Thanks for pointing that out Delta_G! you all have been a big help!

Delta_G

#8
Feb 10, 2018, 03:22 am Last Edit: Feb 10, 2018, 03:22 am by Delta_G
@JMeller

I might write a function that returns a long in this code called getDistance.  But it would look like this:

Code: [Select]


long getDistance(){

     return sr04.Distance();
}


I might write a function that returns nothing in this code that does something about the distance.

Code: [Select]


void doSomethingAbout(long aDistance){

    if(aDistance < 30){
         digitalWrite(5, HIGH);
    }
    else {
         digitalWrite(5, LOW);
    }
}
         




Then your loop could just be:

Code: [Select]

void loop(){
   long dist = getDistance();
   doSomethingAbout(dist);
   delay(1000);
}


Or since I don't need dist anywhere else here I could make it shorter and just do:

Code: [Select]


void loop(){
   doSomethingAbout(getDistance());
   delay(1000);
}

|| | ||| | || | ||  ~Woodstock

JMeller

Thank you Delta_G for the detailed explanation: +1

Go Up