Arduino elevator

Hello!
Please help me with this problem. :frowning: :~ I have to finish a project by tuesday and I got stuck.
I have to make an elevator model using an arduino, i am using a dc motor, which is connected to an RC car breadboard so when i push on the remote buttons, the motor moves in the specific direction. I am using two relays to virtually press the remote buttons, which work perfectly. I connected them to the arduino, each through one wire, and I made the motor go in each direction with a delay of 1000 using the blink example code a bit modified, all of this works fine. Then I went a step further and added buttons connected to the arduino so when i would press them the motor will go in the two direction… with or without delays… Now here’s the part where I got stuck. My elevator has 3 floors, each wit a limit switch, so when the elevator is at floor 2 , the 2nd switch is pressed, and so on at all the floors.

What I want to do is make a code that would say:

if the elevator is on floor 1 (limit switch 1 is pressed), an I press the button for the third floor, the motor will spin in direction 1, until the limit switch at floor 3 is pressed(the elevator reached the third floor.

I tried variations of the “Button” example code, but didn’t work. I’m guessing I need a type of code similar to “move up until you hit the wall, then stop”.

Other codes I used:

void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT); //limit for 3rd floor
pinMode(3, INPUT); //limit for 2nd floor
pinMode(4, INPUT); //limit for 1st floor
pinMode(5, INPUT); //button for 1
pinMode(6, INPUT); //button for 2
pinMode(7, INPUT); //button for 3
pinMode(8, OUTPUT); //motor dir UP (relay 1)
pinMode(9, OUTPUT); //motor dir DOWN (relay 2)

}

void loop() {

//Go from level 1 to level 2
while(4==HIGH)
{
if(6==HIGH)
{
digitalWrite(8, HIGH);
}
if(3==HIGH)
{
digitalWrite(8, LOW);
}
}

//Go from level 1 to level 3
while(4==HIGH)
{
if(7==HIGH)
{
digitalWrite(8, HIGH);
}
if(2==HIGH)
{
digitalWrite(8, LOW);
}
}

//Go from level 2 to level 1
while(3==HIGH)
{
if(5==HIGH)
{
digitalWrite(9, HIGH);
}
if(4==HIGH)
{
digitalWrite(9, LOW);
}
}

//Go from level 2 to level 3
while(3==HIGH)
{
if(7==HIGH)
{
digitalWrite(8, HIGH);
}
if(2==HIGH)
{
digitalWrite(8, LOW);
}
}

//Go from level 3 to level 1
while(2==HIGH)
{
if(5==HIGH)
{
digitalWrite(9, HIGH);
}
if(4==HIGH)
{
digitalWrite(9, LOW);
}
}

//Go from level 3 to level 2
while(2==HIGH)
{
if(6==HIGH)
{
digitalWrite(9, HIGH);
}
if(3==HIGH)
{
digitalWrite(9, LOW);
}
}
}

int timer = 100; // The higher the number, the slower the timing.

void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}

// loop from the highest pin to the lowest:
for (int thisPin = 7; thisPin >= 2; thisPin–) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}

Please help me as soon as possible, I have to finish this project by tuesday!

Thank you!

You have two main ways to tackle this. Firstly you can have code in loop() that determines what to do next (based on the buttons and switches), and then calls a routine to do that task (say go from floor 1 upto floor 3.)

The routine doesn't return control until the task is done, so it will start the motor, busy-wait for the elevator to get there, then return.

This is simple to code, and intuitive, but means that you can't program anything else to happen at the same time as loop() doesn't get called again till the task is over. Separating the tasks into separate routines will make the code easier to deal with - each can easily be tested separately for instance. The code in loop is just a long if ... then ... else if ... then ... else if ... then ... else ...

The second, more powerful, approach is to maintain 'state' - that is you record in variables what the current task and progress is - a set of boolean variables is typical for this. Then the loop() code looks to see what the current task is and continues it (say by testing for completion), and then returns immediately. It also tests the buttons for what task to do next and updates the state. This means you can do other stuff in the loop() routine not related to the elevator if you want since it is being called frequently.

For your project the first method ought to be enough I think (its much easier) - and I reckon you are closer than you think - make loop() read the buttons and decide what to do next, then make it call the relevant routine. Then each task is coded in its own routine (which I think just needs to read the floor-switches and switch the motor on/off).

Make sense?

I see that it makes perfect sense, but i don't know how to write the code. :% Can you show me a part of the code please? For example, just the void setup() and the void loop() only for going from floor one to floor 2 when I press button 2. The rest, I can figure out on my own. It would be great.

Thank you for helping me here. I appreciate it. :%