1

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!

2
  • did you try to compile your code?
    – jsotola
    Sep 7, 2022 at 14:58
  • start thinking about if statements in your everyday life ... they happen all the time ... for example if (red light) {stop car}; ... then think if there would be an else block .... don't forget that you can put any command in the else block ... else if is useful
    – jsotola
    Sep 7, 2022 at 15:02

1 Answer 1

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.

5
  • you could use a C++ ternary operator ... replace your six lines with one line ... digitalWrite (motorpin1, (distance < 5) ? LOW : HIGH );
    – jsotola
    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 just digitalWrite (motorpin1, distance >= 5);
    – timemage
    Sep 7, 2022 at 20:55
  • it depends on if HIGH is equal to true and LOW is equal to false
    – jsotola
    Sep 7, 2022 at 21:13
  • 1
    You 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
    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
    Sep 8, 2022 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.