I have not studied your code closely but you should be able to do all the fromFloorXXX() stuff in one function and replace all the upXXXFloor() functions with one function
I suggest you write down a description of a button call and a subsequent move in English rather than code. It may make it easier to see all the common parts.
The way I envisage, it a floor call button should set a value in an array with an element for each floor. That way any, all or no floors can be selected at the same time.
Then the move function will look at that array and at other variables that keep track of whether it was going up or down and what floor it is at. For example if it is at floor 2 and got there by going up it will look through the array to see if there is a call for floor 3 or 4 and if there is it will move up more. When it arrives at a floor it will cancel the call for that floor. If there is no request for a higher floor it will check for lower floors and maybe change the direction variable and move down.
In most computer programs the data structure is more important than the code. In this case, with the correct data structure the code can be very short.
I am still in need of help and I think it is still going back to my original lack of knowledge of writing a function to call a subroutine. To try and troubleshoot my way through what the program was doing I incorporated an LCD screen that I could print to after each step was performed and to keep an update on the current floor number. In various versions of this code I have been more or less successful with having the LCD communicate at what state the floor number is after various movement changes. However currently I can not even get the LCD to print at the first step where int currentFloor = 1 has been defined in the void loop setup.
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <LiquidCrystal.h>
#define DISTANCE1 1000
#define DISTANCE2 4000
int StepCounter = 0;
int Stepping = false;
int currentFloor = 1;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9); //creates LCD
void setup() {
lcd.begin(16, 2); //Initialize the 16x2 LCD
lcd.setCursor(0, 0); //Set the (invisible) cursor to column 0,
pinMode(11, OUTPUT); //Forward and Reverse (HIGH,LOW)
pinMode(10, OUTPUT);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
pinMode(1, INPUT); //Floorswitch 1
pinMode(2, INPUT); //Floorswitch 2
pinMode(3, INPUT); //Floorswitch 3
pinMode(4, INPUT); //Photocell Floor 1
pinMode(5, INPUT); //Photocell Floor 2
pinMode(6, INPUT); //Photocell Floor 3
}
void loop() {
if (currentFloor == 1) {
lcd.clear();
lcd.print("Floor 1");
fromFloorOne();
}
else if (currentFloor == 2 ) {
lcd.clear();
lcd.print("Floor 2");
fromFloorTwo();
}
else if (currentFloor == 3 ) {
lcd.clear();
lcd.print("Floor 3");
fromFloorThree();
}
}
void fromFloorOne() { //IF BUTTON IS PUSHED WHILE ON FLOOR ONE
if (currentFloor == 1 && digitalRead(1) /*Switch Floor 1*/ == LOW && Stepping == false) { //Floor One Button One
noMovement(); //Remain in position
}
else if (currentFloor == 1 && digitalRead(2) /*Switch Floor 2*/ == LOW && Stepping == false) { //Floor One Button Two
upOneFloor(); //Move up one floor
currentFloor = 2;
}
else if (currentFloor == 1 && digitalRead(3) /*Switch Floor 3*/ == LOW && Stepping == false) { //Floor One Button Three
upTwoFloor(); //Move up two floor
currentFloor = 3;
}
}
void fromFloorTwo() { //IF BUTTON IS PUSHED WHILE ON FLOOR TWO
if (currentFloor == 2 && digitalRead(1) /*Switch Floor 1*/ == LOW && Stepping == false); {//Floor Two Button One
downOneFloor(); //Move down one floor
currentFloor = 1;
}
if (currentFloor == 2 && digitalRead(2) /*Switch Floor 2*/ == LOW && Stepping == false); {//Floor Two Button Two
noMovement(); //Remain in position
}
if (currentFloor == 2 && digitalRead(3) /*Switch Floor 3*/ == LOW && Stepping == false); {//Floor Two Button Three
upOneFloor(); //Move up one floor
currentFloor = 3;
}
}
void fromFloorThree() { //IF BUTTON IS PUSHED WHILE ON FLOOR THREE
if (currentFloor == 3 && digitalRead(1) == LOW && Stepping == false); {//Floor Three Button One
downTwoFloor; //Move down two floor
currentFloor = 1;
}
if (currentFloor == 3 && digitalRead(2) == LOW && Stepping == false); {//Floor Three Button Two
downOneFloor(); //Move down one floor
currentFloor = 2;
}
if (currentFloor == 3 && digitalRead(3) == LOW && Stepping == false); {//Floor Three Button Three
noMovement(); //Remain in position
}
}
void noMovement() { //TRAVEL NO DISTANCE, does not turn on the stepper motor
Stepping = false;
}
void upOneFloor() { //TRAVEL ONE FLOOR DISTANCE UP
digitalWrite(11, HIGH);
Stepping = true;
delay(1);
digitalWrite(10, HIGH);
delay(1);
digitalWrite(10, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE1)
{
StepCounter = 0;
Stepping = false;
}
}
void upTwoFloor() { //TRAVEL TWO FLOOR DISTANCE UP
digitalWrite(11, HIGH);
Stepping = true;
delay(1);
digitalWrite(10, HIGH);
delay(1);
digitalWrite(10, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE2)
{
StepCounter = 0;
Stepping = false;
}
}
void downOneFloor() { //TRAVEL ONE FLOOR DISTANCE DOWN
digitalWrite(11, LOW);
Stepping = true;
delay(1);
digitalWrite(10, HIGH);
delay(1);
digitalWrite(10, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE1)
{
StepCounter = 0;
Stepping = false;
}
}
void downTwoFloor() { //TRAVEL TWO FLOOR DISTANCE DOWN
digitalWrite(11, LOW);
Stepping = true;
delay(1);
digitalWrite(10, HIGH);
delay(1);
digitalWrite(10, LOW);
delayMicroseconds(100);
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE2)
{
StepCounter = 0;
Stepping = false;
}
}/code]
In my conceptual design I see the program flowing seamlessly through the "if, else" statements on loop and given a definition of int currentFloor = 1 I assume it should be printing on the LCD and then accessing the function "fromFloorOne" and at that point it the program would sit there idling until there is a pinMode input to change an "if, else" statement to true and implement a further sub function. If one of you readers could please be kind enough to scan for errors or explain how I am not successfully calling these functions from my void loop I would greatly appreciate help.
Robin in response to your last post, the way you envisage my project is much grander than it will be in actual implementation. I can see the functionality you would achieve by structuring it the manner you suggested, however I am trying to build simply and write the code in a manner where one button call receives one movement result and nothing happens until that movement has been completed. Ignoring the functionalities of an actual elevator capable of handing many requests at once.
Agreed wholeheartedly.
Output messages and debugging information to the Serial monitor as a first step. Add output to LCD when you know that the basics work.
Remove the LCD code and the physical connections to the LCD while you debug the code.