Hello all. I am writing a program to control a model elevator and am basing it off a simple "if else" method for determining movement and a stepper motor and variable distances to reach the floor. The logic behind it is that with each floor reads two sensors, a photocell which will read if the elevator is on that floor, and a button on each floor. So there are only 9 variable actions in total. Example: if elevator is on floor one then photocell 4 is reading and then if button on floor 2 is pushed the subsequent code written for this "if else" statement becoming true will control the actions. My trouble right now is the way I am coding is not creating mutually exclusive results for my "if" statements. In my mind I want to have this "if" statement be read as true and then have a big chunk of commands and subsequent "if" statements written off to the side in parentheses that only effect anything if accessed because of a true "if else" statement.
I imagine this is done with a function or a subroutine but all I can find through research and examples are one liner functions. Not anything that contains further "if" statements or variable changes. If I can accomplish this then I think I can have 9 different sets of result codes for the 9 potentialities from the 3 elevator floors. And then run the "if else" to determine which to operate.
Thank you for your advice and help!
#include <AccelStepper.h>
#include <MultiStepper.h>
#define DISTANCE1 1000
#define DISTANCE2 4000
int StepCounter = 0;
int Stepping = false;
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
}
void loop() {
//FLOOR ONE BUTTON 1
{
if (digitalRead(4) == LOW && digitalRead(1) == LOW && Stepping == false)
{
digitalWrite(8, HIGH);
Stepping = false;
}
if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE1)
{
StepCounter = 0;
Stepping = false;
}
}
}
//FLOOR ONE BUTTON 2
{
if (digitalRead(4) == LOW && digitalRead(2) == LOW && Stepping == false)
{
digitalWrite(8, HIGH);
Stepping = true;
}
if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE1)
{
StepCounter = 0;
Stepping = false;
}
}
}
//FLOOR ONE BUTTON 3
{
if (digitalRead(4) == LOW && digitalRead(3) == LOW && Stepping == false)
{
digitalWrite(8, HIGH);
Stepping = true;
}
if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE2)
{
StepCounter = 0;
Stepping = false;
}
}
}
}