Motor control issues in senior capstone project (help!)

There are two issues I am running into. I am not completely new to programming, but I am self taught, thus there are massive gaps in my understanding of almost every component of coding. The issues I am running into are two fold:

  1. Currently, this code will execute two loops whether "f" - or my temperature measurement data - is below or above a certain threshold. We are concerned only with the second case, "if (f > tempmin and READ_SWITCH_POSITION_A == HIGH)". In this case, it will rotate the stepper motor approximately 86 degrees counterclockwise. The issue is that after a delay, the code will loop infinitely, and rotate as such. I would like to make the stepper rotate it 86 degrees once and ONLY once, when the conditions included in the code are met. For clarity the READ_SWITCH_POS refers to one of two positions of a 3-position toggle switch (middle position is neutral).

  2. At some indeterminate time later, I would like the stepper motor to rotate back to its original position if the toggle is switched to the other active position, or (READ_SWITCH_POSITION_B == HIGH), but I want it to execute this clockwise rotation back to its starting position ONLY if the counterclockwise cycle has executed.

I am not sure how to implement either of these functions, and have been banging my head against the wall for days trying to figure it out. Any help or insight would be greatly appreciated. Thank you all in advance for your time!

#include <AccelStepper.h>
#include <MultiStepper.h>
#include <LiquidCrystal.h>
#include <DHT.h>

// set the DHT Pin:
#define DHTPIN 2

// initialize the library with the numbers of the interface pins:
LiquidCrystal lcd(13, 12, 11, 10, 9, 3);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Define stepper motor connections and steps per revolution:
#define dirPin1 4
#define stepPin1 5
#define dirPin2 7
#define stepPin2 8
#define stepsPerRevolution 200

// Define Ignition toggle and switch positions:
#define   SWITCH_POSITION_A_PIN   14
#define   SWITCH_POSITION_B_PIN   15
#define   READ_SWITCH_POSITION_A  digitalRead(SWITCH_POSITION_A_PIN)
#define   READ_SWITCH_POSITION_B  digitalRead(SWITCH_POSITION_B_PIN)

// variables
byte switchState = 0;             // to store switch reading 0 = off 1 = posA 2 = posB
byte lastSwitchState = 0;         // to check for change


void setup() {
  Serial.begin(9600);
  // Declare switch pins as inputs:
  pinMode(SWITCH_POSITION_A_PIN, INPUT);      
  pinMode(SWITCH_POSITION_B_PIN, INPUT);
  
  // Declare pins as output:
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  
  // Declare temp/hum sensor as input:
  pinMode(DHTPIN, INPUT);
  
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  dht.begin();
  
  // Print a message to the LCD.
  lcd.print("Temp:  Humidity:");
}



void loop() {

  delay(500);
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // read humidity
  float h = dht.readHumidity();
  //read temperature in Fahrenheit
  float f = dht.readTemperature(true);
  //Define temperature min and max
  int tempmin = 70;
  int tempmax = 83;
 
  if (isnan(h) || isnan(f)) {
    lcd.print("ERROR");
    return;
  }

  lcd.print(f);
  lcd.setCursor(7,1);
  lcd.print(h);  


  // Read the switch and put the result is switchState
  if (READ_SWITCH_POSITION_A == HIGH) switchState = 1;
  if (READ_SWITCH_POSITION_B == HIGH) switchState = 2;
  else switchState = 0;

  // Only do anything if the state has changed
  if (switchState != lastSwitchState){
    lastSwitchState = switchState;
    switch(switchState){
    case 1:

    case 2:

    case 0:      

      break;
    }
  }


  if(f < tempmin and READ_SWITCH_POSITION_A == HIGH){ 
    delay(1000);
  // Set the spinning direction counterclockwise AND clockwise:
  digitalWrite(dirPin1, LOW);
  digitalWrite(dirPin2, HIGH);
  // Spin the stepper motor 1 revolution quickly:
  for (int i = 0; i < 5 * stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(500);
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(500);
  }
  }  
  

  if (f > tempmin and READ_SWITCH_POSITION_B == HIGH){
  delay(1000);
  // Set the spinning direction counterclockwise:
  digitalWrite(dirPin1, LOW);
  digitalWrite(dirPin2, LOW);
  // Spin the stepper motor 1 revolution quickly:
  for (int i = 0; i < 1.03*stepsPerRevolution; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(15000);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(15000);
 
  }
  
  return;
  
}

 

}

That sounds like a classic case for a "state machine" approach (look it up). "State machine" is just a fancy name for a simple concept in which one or more variables is used to keep track of the state of the system.

In your cases the states might be NORMAL_TEMP, LOW_TEMP, HIGH_TEMP which could be recorded in a char variable as 'N', 'L' and 'H'. Or you could use an ENUM

The changes in temperature and the switch position (and maybe time) will cause the current state to change.

The position of the stepper motor will be determined by whichever state is current.

...R

@DATAm0d, do not cross-post. Other thread removed.