void loop() {
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] =
{
{'#', '#', '#', '*' },
{'3', '#', '#', '7' },
{'2', '#', '5', '6' },
{'1', '#', '4', '#' }
};
byte rowPins[ROWS] = {A12, A13, A14, A15}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A11, A10, A9, A8}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int correct = 0;
char customKey = customKeypad.getKey(); //get the key value
digitalWrite(triggerPin, LOW);// put your main code here, to run repeatedly:
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
unsigned long duration = pulseIn(echoPin, HIGH);
int distance = duration / 29 / 2; // Distance measurement using Ultrasonic sensor
if (distance >= 0.1 && distance <= 2 ) {
lcd.setCursor(4, 0);
lcd.println("1 f l o o r");
}
else if (distance >= 7 && distance <= 9 ) {
lcd.setCursor(4, 0);
lcd.println("2 f l o o r");
}
else if (distance >= 14 && distance <= 16) {
lcd.setCursor(4, 0);
lcd.println("3 f l o o r");
}
// Determining the number of floors according to distance
unsigned int i4, i5, i6, i7;
if (customKey == '5') {
i5 = 0;
}
if (customKey == '4') {
i4 = 0;
}
if (customKey == '6') {
i6 = 0;
}
if (customKey == '7') {
i7 = 0;
}
if (distance >= 0.1 && distance <= 7) {
switch (customKey) {
case '2' :
myStepper.step(-stepsPerRev * 1.1);
break ;
case '3' :
myStepper.step(-stepsPerRev * 1.1);
if (strcmp (i5, 0) == 0) {
delay (5000);
myStepper.step(-stepsPerRev * 1.1);
}
else {
myStepper.step(-stepsPerRev * 1.1);
}
break ;
}
}
if (distance >= 7.5 && distance <= 8.5) {
switch (customKey) {
case'1' :
myStepper.step(stepsPerRev * 1.1);
break;
case '3' :
myStepper.step(-stepsPerRev * 1.1);
break;
}
}
if (distance >= 9 && distance <= 20) {
switch (customKey) {
case '2' :
myStepper.step(stepsPerRev * 1.1);
break;
case '1' :
myStepper.step(stepsPerRev * 1.1);
if (strcmp (i6, 0) == 0) {
delay (5000);
myStepper.step(stepsPerRev * 1.1);
}
else {
myStepper.step(stepsPerRev * 1.1);
}
break ;
}
}
if (customKey)
{
lcd.setCursor(4, 1);
lcd.print("keyValue: ");
lcd.println(customKey);
}
delay(100);
}
I’m trying to operate the stepper motor when i press the button. and at the first if-switch syntax,
My intention is when i press the customKey number ‘3’ only, stepper motor will operate one time and once
again and in case of pressing customKey number ‘5’ after pressing number ‘3’ , stepper motor will operate
one time and after than perform delay(5000) and lastly drive once again
but some how the motor doesn’t perform commands separately under the ‘if,else’ condition in the ‘case’
syntax.