How to put reset, stop, enter key on 4x4 keypad

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

}

Looks like pin 9 is being used for both the keypad and stepper :thinking: .

Hi! is there a way to stop the loop with another button? I'm using a keypad and i aim to have a 'stop' button whenever i press the 'A' button to execute the piezo buzzer sound.

#include <Keypad.h>
const byte ROWS = 4; 
const byte COLS = 4; 

const int buzzer = A0;

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

void setup(){
  Serial.begin(9600);
  
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  while(customKey == 'A'){  
  tone(buzzer, 1000); 
  delay(1000);        
  noTone(buzzer);     
  delay(1000);   
  Serial.println(customKey);
 
  }

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.