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;
}
}
}