4 floor elevator

hello friends. i'm writing a 4 floor elevator program. since here, i've wrote basics of this, but i have a problem:

cabin doesn't go down to floors 2 and 3. when keys work, it just goes down to floor 1, goes up to floor 2, 3 and 4. but doesn't go down to 2 and 3 from 4.
please give me a hint!

elevator.ino (2.91 KB)

Please read the "How to use this Forum" post at the top of the Forum as written by Nick Gammon, especially on using code tags to post your code. Readers are very reluctant to open unknown files, so edit your post to use the code tags so we can see your code.

const int TRIG_PIN = 6;
const int ECHO_PIN = 10;
int key1=0;
const int k1=52;
int key2=0;
const int k2=50;
int key3=0;
const int k3=48;
int key4=0;
const int k4=46;
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
 
  pinMode(TRIG_PIN,OUTPUT);
  pinMode(ECHO_PIN,INPUT);
    pinMode(22,OUTPUT);

  pinMode(24,OUTPUT);
    pinMode(26,OUTPUT);
    pinMode(38,OUTPUT);
    pinMode(40,OUTPUT);
    //
 pinMode(52,INPUT);
  pinMode(50,INPUT);
 pinMode(48,INPUT);
 pinMode(46,INPUT);
   //
}
 
void loop()
{
   long duration, distanceCm, distanceIn;
 
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN,HIGH);
 
  // convert the time into a distance
  distanceCm = duration / 29.1 / 2 ;
  distanceIn = duration / 74 / 2;
    //
  key1=digitalRead(k1);
    key2=digitalRead(k2);
  key3=digitalRead(k3);
  key4=digitalRead(k4);
    //
  if (distanceCm <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distanceIn);
    Serial.print("in, ");
    Serial.print(distanceCm);
    Serial.print("cm");
    Serial.println();
  }
  delay(100);
  if (1==1) {
  
   // floor 1
 
    if (key1==HIGH && distanceCm>4) {
    digitalWrite (24,HIGH);
    digitalWrite(26,LOW);
    digitalWrite(22,HIGH);
    
    
  }
    
  else {
    digitalWrite(24,LOW);
    digitalWrite(26,LOW);
    digitalWrite(22,LOW);
    
    
     //floor 2 up
 

if (key2==HIGH && distanceCm<24) {
  digitalWrite (24,LOW);
    digitalWrite(26,HIGH);
        digitalWrite(22,HIGH);
}
else {
   digitalWrite(24,LOW);
    digitalWrite(26,LOW);
        digitalWrite(22,LOW);

 // floor 2 down
 
  if (key2==HIGH && distanceCm>24) {
   digitalWrite (24,HIGH);
    digitalWrite(26,LOW);
        digitalWrite(22,HIGH);

    
  }
    
  else {
    digitalWrite(24,LOW);
    digitalWrite(26,LOW);
    digitalWrite(22,LOW);
  }
    
    //floor 3 up


if (key3==HIGH && distanceCm<39) {
  digitalWrite (24,LOW);
    digitalWrite(26,HIGH);
        digitalWrite(22,HIGH);
}
else {
   digitalWrite(24,LOW);
    digitalWrite(26,LOW);
        digitalWrite(22,LOW);


 // floor 3 down
 
  if (key3==HIGH && distanceCm>39) {
   digitalWrite (24,HIGH);
    digitalWrite(26,LOW);
    digitalWrite(22,HIGH);
    
  }
    
  else {
    digitalWrite(24,LOW);
    digitalWrite(26,LOW);
    digitalWrite(22,LOW);
  }
//floor 4 DOWN


if (key4==HIGH && distanceCm<57) {
  digitalWrite (24,LOW);
    digitalWrite(26,HIGH);
        digitalWrite(22,HIGH);
}
else {
   digitalWrite(24,LOW);
    digitalWrite(26,LOW);
        digitalWrite(22,LOW);


 // floor 4 UP
 
  if (key3==HIGH && distanceCm>57) {
   digitalWrite (24,HIGH);
    digitalWrite(26,LOW);
    digitalWrite(22,HIGH);
    
  }
    
  else {
    digitalWrite(24,LOW);
    digitalWrite(26,LOW);
    digitalWrite(22,LOW);
  }
  }
  }
  }

  }
  
}
}
 if (1==1)

That's a pretty sure bet.
Why bother testing?

When you are tempted to suffix the names of your variables with numbers (e.g. key1, key2, key3, key4, k1, k2, k3, k4) it is time to learn about using arrays.

Pins 22, 24 and 26 get used a lot and probably should have names. In fact, ALL pins that are used should be given names.

This

   // convert the time into a distance
  distanceCm = duration / 29.1 / 2 ;
  distanceIn = duration / 74 / 2;

probably does not give expected results because distanceCM and distanceIn are declared as long.

I can't really figure out your program because your bracketing style is very confusing and you are not using meaningful names for things like the pin numbers. If you use the Auto Format tool it will sort out the bracketing to make everything more readable.

However I think the approach I would use is very different from yours.

I would have an array of 4 elements representing the 4 floors and another variable representing the direction the elevator is moving.

When a button is pressed to select a floor the relevant element of the array would be set.

At any time the elevator is moving up (say) it will stop at any floor where the element is set and at the same time clear the element for that floor. If there is a higher element set it will continue up. If there is no higher element (and if there is a lower element set) it will start moving down.

I think with this system the whole program would only require half, or less of the code that you have.

...R

sorry guys as you know, I'm new here so it looks I made some mistakes.

i would say(about the code) that i have a SRF05 ultrasonic sensor in the elevator cabin which determines the place of cabin, and when the key of any floor pressed, cabin goes there. on the road it should play a music ( set pin 22 HIGH).
can you explain how to put key and motor pin numbers to an array and use them for this project?

econjack:
Please read the "How to use this Forum" post at the top of the Forum as written by Nick Gammon, especially on using code tags to post your code. Readers are very reluctant to open unknown files, so edit your post to use the code tags so we can see your code.

done

HAmid-Iran:
how to put key and motor pin numbers to an array and use them for this project?

First you need to learn about arrays

...R

vaj4088:
When you are tempted to suffix the names of your variables with numbers (e.g. key1, key2, key3, key4, k1, k2, k3, k4) it is time to learn about using arrays.

And when you have a set of arrays whose elements correspond: key[], floor[], led[], then it's time to learn about using objects.

HAmid-Iran:
can you explain how to put key and motor pin numbers to an array and use them for this project?

Create 2 arrays, one for the pin numbers and one to hold the key that was pressed.

const byte pins[] = {11, 17, 13, 20};
bool floorselection[sizeof(pins) / sizeof(pins[0])];

The first line defines an array with pins; note the use of the 'const' keyword; the compiler will throw an error if you try to change it in the code.
For the second line, note the use of the 'sizeof' operator; the 'floorselection' array will automatically have the same number of elements as the 'pins' array.

Next you can use a loop to read the pins defined in 'pins' and store the value in the array 'floorselection'.

thanks guys. i used some arrays in the code.

in my code, some actions(up to 10 lines) must be performed up to 4 times, so what's your idea to use Functions?
for example this lines done in all flors:

digitalWrite(motor1R,LOW);
    digitalWrite(motor1L,LOW);
    digitalWrite(M,LOW);
 delay(2000);
 digitalWrite (motor2R,LOW);
    digitalWrite(motor2L,HIGH);
     delay(2000);
 digitalWrite (motor2R,LOW);
    digitalWrite(motor2L,LOW);

Good idea; that's one of the things that functions are for. Just implement it.

i changed the program to a better one, but it does't work at all. i mean the elevator does't do any thing :confused:

why?!!

const int TRIG_PIN = 6; // SRF05
const int ECHO_PIN = 10; // SRF05
int key1=0;
const int k1=52; // key 1
int key2=0;
const int k2=50; // key 2
int key3=0;
const int k3=48; // key 3
int key4=0;
const int k4=46; // key 4 

const int M=22;  // melody kit
const int motor1R=24; // motor 1
const int motor1L=26; // motor 1
const int motor2R=28; // motor 2
const int motor2L=30; // motor 2

int pinArray1[]={6,22,34,36,38,40};
int pinArray2[]={10,46,48,50,51,52};
int count=0;
int timer=2000;

//

void setup () {
  
   Serial.begin(9600);
 
  for (count=0; count<6; count++) {
    pinMode (pinArray1[count], OUTPUT);
    pinMode (pinArray2[count], INPUT);
    
  }
}

void loop () {
  
long duration, distanceCm, distanceIn;
 
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN,HIGH);
 
  // convert the time into a distance
  distanceCm = duration / 29.1 / 2 ;
    //
  key1=digitalRead(k1);
    key2=digitalRead(k2);
  key3=digitalRead(k3);
  key4=digitalRead(k4);

    //
  if (distanceCm <= 0){
    Serial.println("Out of range");
  }
  else {
    
    Serial.print(distanceCm);
    Serial.print("cm");
    Serial.println();
  }
  delay(100);
  // main commands
  
      if (key1==HIGH && distanceCm>4) {
        GODOWN();   }
    
        else {
          STOP();   }
        
      
       if (key2==HIGH && distanceCm<22) {
         GOUP();   }
         
         else {
           STOP();   }
           
           if (key2==HIGH && distanceCm>22) {
             GODOWN();    }
             
             else {
               STOP();    }
               
               if (key3==HIGH && distanceCm<39){
                 GOUP();    }
                 
                 else {
                   STOP();   }
                   
                   if (key3==HIGH && distanceCm>39) {
                     GODOWN();        }
                     
                     else {
                       STOP();        }
                       
                   
            if (key4==HIGH && distanceCm<55){
              GOUP();         }
              
              else {
                STOP();        }
 
}

// functions


void GOUP() {
     digitalWrite (motor1R,HIGH);
    digitalWrite(motor1L,LOW);
    digitalWrite(M,HIGH); // melody
}

void GODOWN() {
  
  digitalWrite (motor1R,LOW);
    digitalWrite(motor1L,HIGH);
    digitalWrite(M,HIGH); // melo
      }
      
    
void STOP() {
    digitalWrite(motor1R,LOW);
    digitalWrite(motor1L,LOW);
    digitalWrite(M,LOW);
 delay(2000);
 digitalWrite (motor2R,LOW);
    digitalWrite(motor2L,HIGH);
     delay(2000);
 digitalWrite (motor2R,LOW);
    digitalWrite(motor2L,LOW);
  }

i changed the program to a better one, but it does't work at all

That must be some new usage of the word "better" I was previously unaware of.

AWOL:
That must be some new usage of the word "better" I was previously unaware of.

but I've used arrays and functions and reduced the code lines. what's my fault؟

  key1=digitalRead(k1);
  key2=digitalRead(k2);
  key3=digitalRead(k3);
  key4=digitalRead(k4);

Arrays?
Right.

Please, do yourself (and us) a favour, and use the IDE's auto-format tool before posting code.

You've got a bunch of debug prints in your sketch.
What do they tell you about how your sketch is performing?

AWOL:
You've got a bunch of debug prints in your sketch.
What do they tell you about how your sketch is performing?

don't you want to give me a hint and say where is the faults?

No, I've given enough hints.
You're the one with the hardware in front of you, and the debug prints on the screen - I gave up on playing guessing games around the time I started school.