I have a 4x4 keypad with corresponding command for every key. In my program, I have a buzzer sound put on 'A'. What I aim is to press 'A' and then press an enter button for the buzzer to make a sound and then press stop if I don't want it to make a sound and press the reset button to press a new key to make other commands again. The problem now is how to code Enter, Reset, and stop buttons.
#include <Keypad.h>
#include <Stepper.h>
#include <Servo.h>
const byte ROWS = 4;
const byte COLS = 4;
Servo myservo;
int pos = 0;
const int buzzer = A0;
const int stepsPerRevolution = 200;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Stepper myStepper(stepsPerRevolution, 9,10,11,12);
void setup(){
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
myservo.attach(A1);
myStepper.setSpeed(120);
}
void loop(){
char customKey = customKeypad.getKey();
if (customKey == 'A'){
tone(buzzer, 1000);
delay(1000);
noTone(buzzer);
delay(1000);
Serial.println(customKey);
}
if (customKey == 'B'){
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
Serial.println(customKey);
}
if (customKey == 'C'){
for (pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
Serial.println(customKey);
}
if (customKey == '1'){
Serial.println(customKey);
}
}