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