How to make it loop only once in "if" statement, and ignore other 'if' statement

Hello excellent and very smart people.

This is the most extensive code I've written by myself, so please bear with me.

Basically, I've got two switches, I flick one, it turns on a few relays to control a solenoid, and reads an ultrasonic sensor, which displays to an LCD screen.

I flick the other switch, and it does the same, but with another ultrasonic sensor, and other set of relays.

This is my code (sorry if you're spotting lots of errors, but it complies and mostly works).

#include <LiquidCrystal.h>
 
const int rs = 2, en = 1, d4 = 3, d5 = 4, d6 = 5, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 
int righttriggerPin = 9;      //Define IO pins
int rightechoPin = 10;
int lefttriggerPin = 11;      //Define IO pins
int leftechoPin = 12;
 
long rightduration;
double rightdistance;

long leftduration;
double leftdistance;


const int rightbut = 24;
const int leftbut = 26;

int shutbutstate = 0;
int rightbutstate = 0;
int leftbutstate = 0;

const int relay1 = 31;
const int relay2 = 33;
const int relay3 = 35;
const int relay4 = 37;
const int relay5 = 39;
const int relay6 = 41;
const int relay7 = 43;
const int relay8 = 45;
 
void setup()
{
  pinMode(lefttriggerPin, OUTPUT);   //Define pin
  pinMode(leftechoPin, INPUT);
    pinMode(righttriggerPin, OUTPUT);   //Define pin
  pinMode(rightechoPin, INPUT);
  lcd.begin(16,2);              //Start the LCD screen, define the number of characters and rows
  lcd.clear();                  //Clear the screen
  lcd.setCursor(0,0);           //Set the cursor to first character, first row
  lcd.print("Wood Distance:");        //Display this text

  pinMode (rightbut, INPUT);
  pinMode (leftbut, INPUT);
  pinMode (relay1, OUTPUT);
  pinMode (relay2, OUTPUT);
  pinMode (relay3, OUTPUT);
  pinMode (relay4, OUTPUT);
  pinMode (relay5, OUTPUT);
  pinMode (relay6, OUTPUT);
  pinMode (relay7, OUTPUT);
  pinMode (relay8, OUTPUT);

}
 
void loop()
{
 


    rightbutstate = digitalRead(rightbut);
  if (rightbutstate == HIGH) {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
        digitalWrite(relay7, HIGH);
    digitalWrite(relay8, HIGH);
    delay (1000);
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
      digitalWrite(relay7, LOW);
    digitalWrite(relay8, LOW);
   delay (1000);

//how do I make it only run the above bit once
    
  digitalWrite(righttriggerPin, LOW);   //Reset the trigger pin
  delay(1000);
  digitalWrite(righttriggerPin, HIGH);     //Create a 10 micro second pulse
  delayMicroseconds(10);
  digitalWrite(righttriggerPin, LOW);
  rightduration = pulseIn(rightechoPin, HIGH); //Read the pulse travel time in microseconds.
  rightdistance= rightduration*0.034/2;        //Calculate the distance - speed of sound is 0.034 cm per microsecond    

    lcd.setCursor(0,1);               //Set cursor to column 0, row 1
  lcd.print(rightdistance*10);                     //Display the distance
  lcd.print(" mm");  
  }
  else {
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    delay (1000);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    delay (1000);

    
   lcd.setCursor(0,1);               //Set cursor to column 0, row 1
   lcd.print("Push Button");                     //Display the distance

  }
 
    
    leftbutstate = digitalRead(leftbut);
  if (leftbutstate == HIGH) {
    digitalWrite(relay6, HIGH);
    digitalWrite(relay5, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    delay (1000);
    digitalWrite(relay6, LOW);
    digitalWrite(relay5, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    delay (1000);
    //how do I stop it after here?
  digitalWrite(lefttriggerPin, LOW);   //Reset the trigger pin
  delay(1000);
  digitalWrite(lefttriggerPin, HIGH);     //Create a 10 micro second pulse
  delayMicroseconds(10);
  digitalWrite(lefttriggerPin, LOW);
  leftduration = pulseIn(leftechoPin, HIGH); //Read the pulse travel time in microseconds.
  leftdistance= leftduration*0.034/2;        //Calculate the distance - speed of sound is 0.034 cm per microsecond    

    lcd.setCursor(0,1);               //Set cursor to column 0, row 1
  lcd.print(leftdistance*10);                     //Display the distance
  lcd.print(" mm");  
  }
  else {
     digitalWrite(relay5, HIGH);
     digitalWrite(relay6, HIGH);
     delay (1000);
     digitalWrite(relay7, LOW);
     digitalWrite(relay8, LOW);
    
    lcd.setCursor(0,1);               //Set cursor to column 0, row 1
    lcd.print("Push Button");                     //Display the distance

  }
}

The problem I'm running into, is that I only want the solenoids to cycle once when the switch is flicked (they continue to run atm), then for the ultrasonic sensor to continue running until it's turned off, then stop, and stop displaying to the lcd screen so if I flick the other switch, it'll display the other ultrasonic sensor on it.

ATM, the LCD screen/relays is getting conflicted with the other 'if' statement, and I'm a little stuck.

Basically what I'm trying to make is an ultrasonic sensor to use on a miter saw station, flick a switch, it pops out, and displays the distance to the wood from the sensor (which will tell me how long the wood is with some maths, and I can make accurate cuts).

And my tangle of mess, it does make sense, and I have distance required to go to the spots it needs to go to.

Car solenoids are cheap and easy.

Even taught myself how to attach pins to make things neater and easier.

Thanks in advance, I'm sure it's annoying constantly pointing out what's obvious with newbies sketches.

I tried to put the if statement in the setup, but it popped up with errors, so I don't think that works there.

Columnmn:
The problem I'm running into, is that I only want the solenoids to cycle once when the switch is flicked (they continue to run atm), then for the ultrasonic sensor to continue running until it's turned off, then stop, and stop displaying to the lcd screen so if I flick the other switch, it'll display the other ultrasonic sensor on it.

A general outline is: Sense that the button of interest *has changed . * See IDE - File/examples/digital/state change. This is a different thing than knowing the button input is at a high or low level. An important concept to grasp is that this *changed state * only applies for one scan of the program. It's basically just a very short logic pulse. Use this pulse to set the state of a flag which enables your desired code, call it 'lengthEnable'.

So, you set up an if() statement which tests the state of lengthEnable and executes your sensor code if it's true.

// some code here turns on 'stateChange' - it goes true
if(stateChange == true){
  lengthEnable = true;
}
// the next time this code is executed 'stateChange'
// will be reset to false but lengthEnable remains true
//
// now use lengthEnable to execute the sensing code
if (lengthEnable == true){
  // sensor stuff or whatever goes here
  // when sensing is done
  lengthEnable == false  // this makes it stop sensing 'til next button press
}

Thanks for the help, I think that's what I'm looking for. I'll fiddle with code further!

Thanks again!

Sorry my images didn't work, I didn't know photobucket had gotten annoying like that:


Jumble of mess, it does make sense:

I had a play with the code, added the state change stuff.

I've gotten the LCD/ultrasonic sensors working perfectly. But the relays are still playing up. they won't turn on until the switch is flicked, but then it won't stop, even after the switch is turned off.

Could somebody check what I messed up with the code?

#include <LiquidCrystal.h>
 
const int rs = 2, en = 1, d4 = 3, d5 = 4, d6 = 5, d7 = 6;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
 
int righttriggerPin = 9;      //Define IO pins
int rightechoPin = 10;
int lefttriggerPin = 11;      //Define IO pins
int leftechoPin = 12;
 
long rightduration;
double rightdistance;

long leftduration;
double leftdistance;


const int rightbut = 24;
const int leftbut = 26;


int rightbutstate;
int leftbutstate;
int rightlengthEnable;
int leftlengthEnable;


const int relay1 = 31;
const int relay2 = 33;
const int relay3 = 35;
const int relay4 = 37;
const int relay5 = 39;
const int relay6 = 41;
const int relay7 = 43;
const int relay8 = 45;
 
void setup()
{
  pinMode(lefttriggerPin, OUTPUT);   //Define pin
  pinMode(leftechoPin, INPUT);
    pinMode(righttriggerPin, OUTPUT);   //Define pin
  pinMode(rightechoPin, INPUT);
  lcd.begin(16,2);              //Start the LCD screen, define the number of characters and rows
  lcd.clear();                  //Clear the screen
  lcd.setCursor(0,0);           //Set the cursor to first character, first row
  lcd.print("Wood Distance:");        //Display this text

  pinMode (rightbut, INPUT);
  pinMode (leftbut, INPUT);
  pinMode (relay1, OUTPUT);
  pinMode (relay2, OUTPUT);
  pinMode (relay3, OUTPUT);
  pinMode (relay4, OUTPUT);
  pinMode (relay5, OUTPUT);
  pinMode (relay6, OUTPUT);
  pinMode (relay7, OUTPUT);
  pinMode (relay8, OUTPUT);
  //Serial.begin(9600);

}
 
void loop()
{
 


    rightbutstate = digitalRead(rightbut);
  if (rightbutstate == true)  {
   rightlengthEnable = true;
  }
    
   

  if (rightlengthEnable == true) {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
        digitalWrite(relay7, HIGH);
    digitalWrite(relay8, HIGH);
    delay (1000);
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
      digitalWrite(relay7, LOW);
    digitalWrite(relay8, LOW);
    delay (1000);

  rightlengthEnable == false;
  }
  else{}

    if (rightbutstate == HIGH){
  digitalWrite(righttriggerPin, LOW);   //Reset the trigger pin
  delay(1000);
  digitalWrite(righttriggerPin, HIGH);     //Create a 10 micro second pulse
  delayMicroseconds(10);
  digitalWrite(righttriggerPin, LOW);
  rightduration = pulseIn(rightechoPin, HIGH); //Read the pulse travel time in microseconds.
  rightdistance= rightduration*0.034/2;        //Calculate the distance - speed of sound is 0.034 cm per microsecond    

    lcd.setCursor(0,1);               //Set cursor to column 0, row 1
  lcd.print(rightdistance*10);                     //Display the distance
  lcd.print(" mm Right    ");  
  }
 else {
    
  //lcd.setCursor(0,1);               //Set cursor to column 0, row 1
   //lcd.print("Push Button");                     //Display the distance

   }
 
    
   leftbutstate = digitalRead(leftbut);
  
  if (leftbutstate == true)  {
   leftlengthEnable = true;
  }
    if (leftlengthEnable == true) {  
    digitalWrite(relay6, HIGH);
    digitalWrite(relay5, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    delay (1000);
    digitalWrite(relay6, LOW);
    digitalWrite(relay5, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    delay (1000);
  
    leftlengthEnable == false;
    }
    else {}

    if (leftbutstate == HIGH) {
  digitalWrite(lefttriggerPin, LOW);   //Reset the trigger pin
  delay(1000);
  digitalWrite(lefttriggerPin, HIGH);     //Create a 10 micro second pulse
  delayMicroseconds(10);
  digitalWrite(lefttriggerPin, LOW);
  leftduration = pulseIn(leftechoPin, HIGH); //Read the pulse travel time in microseconds.
  leftdistance= leftduration*0.034/2;        //Calculate the distance - speed of sound is 0.034 cm per microsecond    

    lcd.setCursor(0,1);               //Set cursor to column 0, row 1
  lcd.print(leftdistance*10);                     //Display the distance
  lcd.print(" mm Left    ");  
  }
  else {
    //lcd.setCursor(0,1);               //Set cursor to column 0, row 1
    //lcd.print("Push Button");                     //Display the distance

  
}}

Thanks in advance.

The speed of sound varies with temperature.

How much does the temperature in your wood shop vary?

odometer:
The speed of sound varies with temperature.
Speed of sound - Wikipedia

How much does the temperature in your wood shop vary?

It goes between 10C-40C, which according to the chart is from 337-350m/s. Could add a thermometer, and according to that temp, it'll make my current 0.034 measurement a variable. Will see how much it's out in certain temps though. If it's 1 or 2 mm, I'm not too worried.

Thanks for bringing it to my attention though, will keep an eye on it now.

Could somebody check what I messed up with the code?

That would be hard to say, since I do not understand your thread title. An if statement is true or it is not. There is NOTHING about an if statement that involves looping. The body of the if statement, or of the else block, could involve looping, but the if statement doesn't loop.

Keeping that in mind, perhaps you should revise your thread title.

How IS your leftbut switch connected? How IS your rightbut switch connected? You are not using the internal pullup resistors, so you MUST have external resistors. Are they pullup or pulldown resistors?

    rightbutstate = digitalRead(rightbut);
  if (rightbutstate == true)  {

digitalRead() does NOT return true or false. It returns HIGH or LOW.

boolean variables are assigned true or false, NOT ints.

  rightlengthEnable == false;

Why are you comparing rightlengthEnable to false, outside of an if statement?

What is wrong with your camel?

That was as far as I got, reading that code that looks like the rat's nest of wiring that you have. While cleaning up the wiring might be challenging, any monkey can select Tools + Auto Format, and make the code look orders of magnitude better, in milliseconds. It's long past time that you did that.

Someone may have mentioned this, but your code will be a lot easier to maintain if your next step is to read about arrays[] and implementyjrm.

These things (state change, and using arrays) are very common learning steps - and very useful in the long term!
Good luck.

PaulS:
There is NOTHING about an if statement that involves looping.

You mean because, if it involves looping, then it is not an if statement but rather a while statement?

odometer:
You mean because, if it involves looping, then it is not an if statement but rather a while statement?

No. An if statement is used to decide if some code should be executed, or not. It is evaluated ONE time.

A while statement is used to execute some code over and over, or not at all, while some condition is true.

Obviously, a while statement could be evaluated over and over. An if statement is evaluated once. Doing something over and over is the definition of looping. Doing something once does not involve looping.

Now, what is done, in the body of the if statement could involve looping, but the if statement DOES NOT LOOP.