URGENT: Problem making an arduino elevator

Hello, I am making an arduino elevator and I have trouble controlling what floor the elevator goes to depending on the button I press and the floor the elevator is currently on. I have connected a sharpIR sensor and it senses how close and far the elevator is. The buttons are connected and whenever i push a button it does whatever i coded it to. The problem occurs when I want the elevator to go to the first floor when it is on the second floor. Typically when the elevator is on the second floor the sensor reads 150 analog and when it is on the first floor it reads around 600 analog. How can I code it so that when I press button 1 it will sense where the elevator currently is and then keep turning the motor until the analog value becomes what it should be at floor 1 which is 600?

Here is my code:

#include <Keypad.h>
#include <Servo.h>
#include <SharpIR.h>
SharpIR sharp(A0, 25, 93, 1080);
int dis=sharp.distance();
const int LOL=A0;
Servo myservo;


const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns


// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {8, 7, 6, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {4, 3, 2};

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13
#define ir A0
#define model 1080

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
  myservo.attach(10);
}

void loop()
{
  myservo.write(90);
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  Serial.print("Button Pressed: ");
  Serial.println(key);
  
  
  {int sensorValue= analogRead(LOL);
  int dist=map(sensorValue, 105.5, 602.5, 46.1, 0);
  Serial.print("Analog Reading: ");
  Serial.print(sensorValue);
  Serial.print(" Centemeters: ");
  Serial.print(dist);
  Serial.println("cm");
  delay(100);}

  {
    int sensorValue= analogRead(LOL);
    
    switch (key)
       case '1':
        while(sensorValue<600)
        {
            myservo.write(180);
            delay(sensorValue>600);
       break;
        
    
    }
   }
  }

Does that code compile?

Yes it compiles

  int dist=map(sensorValue, 105.5, 602.5, 46.1, 0);

Decimals in map?

     delay(sensorValue>600);

Delay one or zero milliseconds. What's the point of that?
What changes sensorValue in the loop?

 switch (key)
       case '1':

A switch with one case?
Why?

When posting code containing non standard libraries you have to post a link to them. Please read How to use this forum

delay(sensorValue>600);

What ever you think this does it does not do it. Delay takes in a number and delays for than many microseconds. There should be no comparison operations in it.

Okay I get the Delay part so I took the comparison out of the code, however I have already put in the Sharp.IR library and the values the sensor is picking up at each floor is showing up in the serial monitor. I just want to know to make a comparison between the values I get. So say the elevator is at floor 2 which is 150 analog and I want to make go to floor 1 which 600 analog, when I press button 1 it should spin the motor until the sensor sense 600 and then stop the motor which would make the elevator go from floor 2 to floor 1. However I have no clue how to do that.

it should spin the motor until the sensor sense 600 and then stop the motor

Pick that phrase apart, and imagine it as a few C expressions.
Maybe a while loop?

AWOL:
Pick that phrase apart, and imagine it as a few C expressions.
Maybe a while loop?

What is a C expression, I am a total newbie to arduino and programming in general

motor (ON);
while (getRange () < 600);
motor (OFF);

It won’t compile, but it should give you a place to start to think about a solution

AWOL:

motor (ON);

while (getRange () < 600);
motor (OFF);




It won't compile, but it should give you a place to start to think about a solution

So I tried to code that so that when I press button 1 it writes myservo.write(90) so that the servo doesnt move at first and then if the (range)<600 then it does myservo.write(180) and move the elevator down.
But the code wont compile and it keeps telling me that case ‘2’ is not in the switch statement when it is so I have no idea what im doing wrong.

#include <Keypad.h>
#include <Servo.h>
#include <SharpIR.h>
SharpIR sharp(A0, 25, 93, 1080);
int dis=sharp.distance();
const int LOL=A0;
Servo myservo;


const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns


// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {8, 7, 6, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {4, 3, 2};

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13
#define ir A0
#define model 1080

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
  myservo.attach(10);
}

void loop()
{
  myservo.write(90);
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  Serial.print("Button Pressed: ");
  Serial.println(key);
  
  
  {int sensorValue= analogRead(LOL);
  int dist=map(sensorValue, 105.5, 602.5, 46.1, 0);
  Serial.print("Analog Reading: ");
  Serial.print(sensorValue);
  Serial.print(" Centemeters: ");
  Serial.print(dist);
  Serial.println("cm");
  delay(100);}

  {
    int sensorValue= analogRead(LOL);
    
    switch (key)
        case '1':
        myservo.write(90);
        while(sensorValue < 600){
        myservo.write(180);
        break;
        
      
      case '2':
        myservo.write(0);
        delay(5000);
        break;

      case '3':
         myservo.write(180);
         delay(5000);
         break;

      case '4':
         myservo.write(0);
         delay(5000);
         break;

      case '5':
          myservo.write(180);
          delay(30000);
          break;

      case '6':
           myservo.write(0);
           delay(28000);
           break;
        
      case '*':
        //digitalWrite(ledpin, LOW);
        break;
      case '#':
        //digitalWrite(ledpin, HIGH);
        break; 
      //default:
        
    }
   }
  }

Your switch lacks braces, which is OK for a single case, but needs to be fixed for multiple cases.

what is a brace? and how do I fix it for my code?

Is is a brace {
Here is another one }
A brace of braces.

This project is way above your skill level so far. Do some tutorial examples first.

I have no time to attempt the examples sir, how would i know where to add the braces?

Look at any example of a switch/case, and you'll see where they need to go.

Okay I figured it out and the code complies however whenever I press 1 nothing happens it doesnt move.

#include <Keypad.h>
#include <Servo.h>
#include <SharpIR.h>
SharpIR sharp(A0, 25, 93, 1080);
int dis=sharp.distance();
const int LOL=A0;
Servo myservo;


const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns


// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {8, 7, 6, 5};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {4, 3, 2};

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13
#define ir A0
#define model 1080

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
  myservo.attach(10);
}

void loop()
{
  myservo.write(90);
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  Serial.print("Button Pressed: ");
  Serial.println(key);
  
  
  {int sensorValue= analogRead(LOL);
  int dist=map(sensorValue, 105.5, 602.5, 46.1, 0);
  Serial.print("Analog Reading: ");
  Serial.print(sensorValue);
  Serial.print(" Centemeters: ");
  Serial.print(dist);
  Serial.println("cm");
  delay(100);}

  {
    int sensorValue= analogRead(LOL);
    
    switch (key)
      { 
       case '1':
       myservo.write(90);
       while(sensorValue<600)
       {myservo.write(180);
            
           //delay(sensorValue>600);
       break;
        
      
      case '2':
        myservo.write(0);
        delay(5000);
        break;

      case '3':
         myservo.write(180);
         delay(5000);
         break;

      case '4':
         myservo.write(0);
         delay(5000);
         break;

      case '5':
          myservo.write(180);
          delay(30000);
          break;

      case '6':
           myservo.write(0);
           delay(28000);
           break;
        
      case '*':
        //digitalWrite(ledpin, LOW);
        break;
      case '#':
        //digitalWrite(ledpin, HIGH);
        break;
      //default:
       
    }
   }
  }
}
{
       case '1':
       myservo.write(90);
       while(sensorValue<600)
       {myservo.write(180);
            
           //delay(sensorValue>600);
       break;

I think your use of braces falls apart here - it’s difficult to see where the brace that matches the one before the “myservo.write (180)” is.

Also see my earlier comments about the value of sensorValue not changing inside the while loop

Shouldn’t the sensorValue still change while inside the loop? Because when i look at the serial monitor i can see the sensorValue changing with every read.

Okay so I cahnged the code and now the brace that matches with the “myswrite” statement occurs right after and the elevator responds to the button 1 however it stops at around 300 and not 600.

 {int sensorValue= analogRead(LOL);
  int dist=map(sensorValue, 105.5, 602.5, 46.1, 0);
  Serial.print("Analog Reading: ");
  Serial.print(sensorValue);
  Serial.print(" Centemeters: ");
  Serial.print(dist);
  Serial.println("cm");
  delay(100);}

  {
    int sensorValue= analogRead(LOL);
    
    switch (key)
      { 
       case '1':
       myservo.write(90);
       while(sensorValue<600)
       {myservo.write(180);}
            
           //delay(sensorValue>600);
       break;
        
      
      case '2':
        myservo.write(0);
        delay(5000);
        break;

      case '3':
         myservo.write(180);
         delay(5000);
         break;

      case '4':
         myservo.write(0);
         delay(5000);
         break;

      case '5':
          myservo.write(180);
          delay(30000);
          break;

      case '6':
           myservo.write(0);
           delay(28000);
           break;
        
      case '*':
        //digitalWrite(ledpin, LOW);
        break;
      case '#':
        //digitalWrite(ledpin, HIGH);
        break;
      //default:
       
    
   }
  }
}

Shouldn't the sensorValue still change while inside the loop?

It should, but it doesn't.
You can change that.

UsmanJ:
I have no time to attempt the examples sir, how would i know where to add the braces?

So this is a school assignment then?
I would suggest that you do not have time not to do any examples.