Function not running in the code

Yep, it was stupid on me not seeing that missmatch, Paul, thanks. I just deleted the function and added the code to the main loop, so now it is working fine. I don't know why it would let me compile...

I downloaded the software from this website, its version 1.6.5 and I am using an arduino Due

Thanks for the tip, Vaclav. I wanted to organize stuff by making functions but I might as well not do that if the function is going to be called just once.

Problem solved.
Thanks again.

I wanted to organize stuff by making functions but I might as well not do that if the function is going to be called just once.

I would say that it is good practice to use functions even if they are only called once. It allows code to be tested more easily by simply commenting out the call to a function, the function can be easily copied to a test program and a function has a name which, if chosen carefully, can aid understanding what the code it contains does and what type of value, if any, it returns.

I would say that it is good practice to use functions even if they are only called once. It allows code to be tested more easily by simply commenting out the call to a function, the function can be easily copied to a test program and a function has a name which, if chosen carefully, can aid understanding what the code it contains does and what type of value, if any, it returns.

I'll second that.

Can you figure out what this does, at a quick glance:

	digitalWrite(trigPin1, LOW);
	delayMicroseconds(2);
	digitalWrite(trigPin1, HIGH);
	delayMicroseconds(10);
	digitalWrite(trigPin1, LOW);
	duration1 = pulseIn(echoPin1, HIGH);
        distance1 = duration1 / 148;

How about this:

void loop()
{
   int dist1 = distFromPinger(trigPin1, echoPin1);
   int dist2 = distFromPinger(trigPin2, echoPin2);
}

int distFromPinger(int trigPin, int echoPin)
{
	digitalWrite(trigPin, LOW);
	delayMicroseconds(2);
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(trigPin, LOW);
	long duration = pulseIn(echoPin, HIGH);
        return duration / 148;
}
int distFromPinger(int trigPin, int echoPin)
{
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(trigPin, LOW);
	long duration = pulseIn(echoPin, HIGH);
        return duration / 148;
}

If the pin's already LOW, why bother taking LOW again?

If the pin's already LOW, why bother taking LOW again?

Which pin is already low? The trigger pin is turned on and then off a short time later.

The echo pin is presumably low to start with, and the code times how long it take for it to go high.

Yeah, I can understand that. I guess what he meant is that for this function you gave you enter the function calling the pin to be LOW and end calling the pin to be LOW, so when it enters the function the second time it is already LOW.

My code is working all right now, Im pretty happy about it. I attatched leds to indicate elevator movement and door movement and it seems that it works.

I can see it is good practice to have functions, that is why I was doing it on the first place. I will watch out for consistency more carefully from now on. Thanks again =)

so when it enters the function the second time it is already LOW.

The second time the function is called, it is called with a different pin.

Not that it matters. The code sets a pin HIGH, regardless of its previous state, waits a bit, and sets it LOW. There is no way to determine, from looking at the code that AWOL posted, the state of any pin when the function is called. The code does not set any pin to the same state more than once, unless I'm blind or missing something.