I am new to programming so bear with me. I am writing a simple code to control a motor such that the motor will turn at full speed as long as the sensor does not detect an object within 5" of the sensor. If it does see something at 5 inches or less, then I want to motor to stop. My question is this: When using an IF statement, do you have to have the ELSE, or can you simply do two IF statements with no ELSE?
Here is my example:
int motorpin1 = 10 , motorpin2 = 11;
int trig = 12;
int echo = 9;
float distance;
float time;
void setup()
{
pinMode (motorpin1, OUTPUT);
//yellow wire
pinMode (motorpin2, OUTPUT);
//blue wire
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
Serial.begin (9600);
}
void loop()
{
// setup ultrasonic sensor to control
//motor
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
time = pulseIn(echo, HIGH);
distance = time/148.1;
Serial.println(distance);
if distance <5,digitalWrite (motorpin1, LOW);
digitalWrite (motorpin2, LOW);
if distance >=5, digitalWrite (motorpin1, HIGH);
digitalWrite (motorpin2, LOW);
delay (1000); //wait for 1 second
Is there a better way to do this with an ELSE statement? I want to capture the parameter where the code controls the specific parameters of >5 and then <or=5. Also, my next thing is to pause, and reverse the motor if after 1 second, the sensor still sees an object <=5.
Definitely a learning curve for this old brain of mine!
1 Answer 1
You can do something along the line of
if (distance < 5) {
digitalWrite (motorpin1, LOW);
}
else {
digitalWrite (motorpin1, HIGH);
}
It seems like you only set motorpin2
to LOW
and never to HIGH
. If you don't want to set it HIGH
, move that line to the setup()
section. There is no need to keep setting it LOW
.
-
you could use a C++ ternary operator ... replace your six lines with one line ...
digitalWrite (motorpin1, (distance < 5) ? LOW : HIGH );
jsotola– jsotola09/07/2022 18:22:41Commented Sep 7, 2022 at 18:22 -
@jsotola Well, if you're to bother going that route, the ternary operator is itself unnecessary:
digitalWrite (motorpin1, !(distance < 5));
or justdigitalWrite (motorpin1, distance >= 5);
timemage– timemage09/07/2022 20:55:53Commented Sep 7, 2022 at 20:55 -
it depends on if
HIGH
is equal totrue
andLOW
is equal tofalse
jsotola– jsotola09/07/2022 21:13:19Commented Sep 7, 2022 at 21:13 -
1You could use a ternary operator and I do this myself, but since the question is asked by a beginner, I valued readability over reducing number of lines.jkp– jkp09/08/2022 07:17:30Commented Sep 8, 2022 at 7:17
-
@jsotola, something considered and discarded. No one in their right mind will design a core with LOW as truthy an vice versa. Anyway, particularly for new people it's okay the way it is.timemage– timemage09/08/2022 17:18:59Commented Sep 8, 2022 at 17:18
if
statements in your everyday life ... they happen all the time ... for exampleif (red light) {stop car};
... then think if there would be anelse
block .... don't forget that you can put any command in theelse
block ...else if
is useful