save and overwrite data into keypad

we are developing a low cost spectrophotometer , the device use 4x4 keypad , arduino uno, keypad, 2 servo motor (one to place sample and another one for grading), bluetooth module
abcd is use to choose sample
1,2,3,4 is use to change the grading degree

we have pre-installed data in the keypad ( button : 1,2,3,4,5) but we need the buttons to be able to overwrite and save it

can someone help us with this problem. thank you.

#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void clearlcd(int x) //clear lcd function
{
lcd.setCursor(0,x);
for(int i=0; i<16; ++i)
{
lcd.write(' ');
}
}

int angle=0;
int redPin= A3;
int greenPin = A2;
int bluePin = A1;
const int ledPin = 5;
const int buzzerPin = 4;
const int ldrPin = A0;

const byte rows = 4; //four rows since the keypad is full
const byte columns = 4; //four columns, same as above
char buttons[rows][columns] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

int bluetoothTx = 0; //bluetooth tx to 0 pin
int bluetoothRx = 1; //bluetooth rx to 1 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); //for bluetooth 

byte rowP[rows] = {13, 12, 11, 10}; //row pins of the keypad
byte columnsP[columns] = {9, 8, 7, 6}; //column pins of the keypad
Keypad pad = Keypad( makeKeymap(buttons), rowP, columnsP, rows, columns );    //create keypad
Servo ServoG; 
Servo ServoS;// 
//LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module

void setup(){
    ServoS.attach(2);
    ServoG.attach(3);  // attaches the servo on pin 9 to the servo object

 //lcd
    lcd.begin(16, 2);
    lcd.backlight();
    lcd.setCursor(0,0);
  for (int positionCounter = 0; positionCounter < 35; positionCounter++) {
    {
      lcd.setCursor(16,0);
      lcd.print(" Welcome to Group 5 ");
      lcd.setCursor(17,1);
      lcd.print("spectrophotometer");
    }
      lcd.scrollDisplayLeft(); // scroll one position left:
      delay(200); // wait a bit:
  }
      lcd.clear();
      //lcd.setCursor(4,0);
      //lcd.print("For KEYPAD");
      delay (1000);
      for (int positionCounter1 = 0; positionCounter1 < 25; positionCounter1++) {
      lcd.setCursor(0,0);
      lcd.print(" Choose Sample : Press A - D ");
      lcd.setCursor(0,1);
      lcd.print(" Choose Grating : Press 1 - 6 ");
      lcd.scrollDisplayLeft(); // scroll one position left:
      delay(200); // wait a bit:
      }
      lcd.clear();
 
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);

      Serial.begin(9600);
      pinMode(ledPin, OUTPUT);
      pinMode(buzzerPin, OUTPUT);
      pinMode(ldrPin, INPUT);

     bluetooth.begin(9600);
  
}

void loop(){

  buzzerldrled();

  char key = pad.getKey();  //get the char from the keypad
  if(bluetooth.available()>0 ) //receive number from bluetooth
  {
    {
    key = bluetooth.read(); //save the received number to toSend
    }
  }


  
      if(key == 'A')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(0);
      lcd.setCursor(1,0);
      lcd.print("Sample 1");
      }
      if(key == 'B')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(45);
      lcd.setCursor(1,0);
      lcd.print("Sample 2");
      }
      if(key == 'C')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(90);
      lcd.setCursor(1,0);
      lcd.print("Sample 3");
      }
      if(key == 'D')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(45);
      lcd.setCursor(1,0);
      lcd.print("Sample 4"); 
      }

      if(key == '0')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 0;
      ServoG.write(angle);
      delay (1000);
      //servograting.write(0);
      lcd.setCursor(0,0);
      lcd.print("          ");
      lcd.setCursor(0,1);
      lcd.print("reset");
      setColor(0 ,0, 0); // Green Color
      //delay(3000);
     // lcd.clear();

      }
      
      if(key == '1')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 0;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:25");
      setColor(0, 255, 0); // Green Color
      //delay(3000);
     // lcd.clear();
     
      }
      
      if(key == '2')
      {
        
      Serial.println("key=");
      Serial.print(key);
      angle = 120;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:120");
     setColor(0, 0, 255); // Blue Color
      //delay(3000);
     //lcd.clear();
     
      }
      
      if(key == '3')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 50;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:50");
      setColor(170, 0, 255); // Purple Color
     // delay(3000);
      //lcd.clear();
      
      }
 
      if(key == '4')
      {
      Serial.println("key=");
      Serial.print(key);
      angle =90;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:90");
      setColor(255, 255, 255); // White Color
     // delay(3000);
     // lcd.clear();
     
      }
      
      if(key == '5')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 78;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:78");
      setColor(255, 255, 0); // Yellow Color
      //delay(3000);
      //lcd.clear();
     
      }

      if(key == '*'){
             if (angle <180){ 
                  angle+=1;
                  ServoG.write(angle);
                  lcd.clear(); 
                 // lcd.print(angle);
                 lcd.setCursor(0,1);
                 lcd.print("Angle:");
                 lcd.print(angle);
             }
}

            
        if(key == '#'){
             if (angle >0){ 
                  angle-=1;
                  ServoG.write(angle);
                  lcd.clear(); 
                  //lcd.print(angle);
                  lcd.setCursor(0,1);
                  lcd.print("Angle:");
                  lcd.print(angle);
             }

        }
      
     // increment();

/*        //Read from bluetooth and write to usb serial
  if(bluetooth.available()>0 ) //receive number from bluetooth
  {
    {
    int toSend = bluetooth.read(); //save the received number to toSend
    Serial.print(toSend); //serial print toSend current number received from bluetooth
    //ServoG.write(toSend); //to transfer input from mobile app to servo2
    ServoS.write(toSend); //to transfer input from mobile app to servo2
  }
  {
    int toSend = bluetooth.read(); //save the received number to toSend
    Serial.print(toSend); //serial print toSend current number received from bluetooth
    ServoG.write(toSend); //to transfer input from mobile app to servo2
    //ServoS.write(toSend); //to transfer input from mobile app to servo2
  }
      
}
*/
}

void buzzerldrled() {

      int ldrStatus = analogRead(ldrPin);
      if (ldrStatus >= 400) {
      tone(buzzerPin, 100);
      digitalWrite(ledPin, HIGH);
      delay(100);
      noTone(buzzerPin);
      digitalWrite(ledPin, LOW);
      delay(100);
      Serial.println("----------- ALARM ACTIVATED -----------");
      lcd.setCursor(0,1);
      lcd.print(" ALARM ACTIVATED !!!!!");
      delay(500);
      lcd.clear();
      }

    else {
      noTone(buzzerPin);
      digitalWrite(ledPin, LOW);
      //Serial.println("ALARM DEACTIVATED");
      //lcd.setCursor(0,1);
      //lcd.print("ALARM DEACTIVATED");
      }
}      

 
void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

mrah96:
we have pre-installed data in the keypad ( button : 1,2,3,4,5) but we need the buttons to be able to overwrite and save it

I don't understand. I am not aware of any keypads that have the ability to store data.

...R

Can you please satisfy my queries by providing answers to the following questions:?

1. You have 4x4 keypad. You have another item in the list – keypad. What is this?

2. You are using labels a b c d as command keys; but, the labels in your actual keypad are seen as A B C D. Why are they different? We hope that the ASCII codes of the charcaters a b c d are different from that of A B C D.

3. You are also using 1 2 3 4 as command keys for specific tasks. What are these buttons: 1 2 3 4 5?

4. Can you clarify the meaning of this statement: ‘we have pre-installed data in the keypad ( button : 1,2,3,4,5) but we need the buttons to be able to overwrite and save it’ using 2 or 3 sentences.

5. We hope that you are well known of the fact that hardware UART Port of UNO is permanently engaged with IDE and Serial Monitor. If so, why have you used the same UART Port for the Bluetooth Module? Even it works, the veterans term it as a bad practice?

It looks like you have assigned meanings to keys '1', '2', '3', '4', and '5'

  • 1 = Angle:25
  • 2 = Angle:120
  • 3 = Angle:50
  • 4 = Angle:90
  • 5 = Angle:78

And you have buttons to increase and decrease the angle. Now you want to change the angles assigned to the five buttons.

What I would do is store the assigned angles in EEPROM. Then you can read the assignments out of EEPROM in setup() and use the assigned values instead of constants.

Then I would change the sketch to switch to the assigned angle when the button is RELEASED. If the button is pressed and HELD for more than two seconds, assign the current angle to that button.

To change a button, select an angle by using one of the pre-assigned buttons and the UP/DOWN buttons to get to the desired angle, then hold down the button to which you want to assign the value. After two seconds the value is assigned to that button and written to EEPROM. When the button is released, the newly assigned angle is used.

Use an array of 'byte' (0-255) to store the angles. The 'byte' type can easily be saved and restored from EEPROM.

apologies for the confusion.
what i would like to do is to be able for the keypad (buttons 1,2,3,4,5) to be assigned a new value although there are already pre-assigned value in them.
pls help me with this.
thank you.

#include <Keypad.h>
#include <Servo.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <EEPROM.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void clearlcd(int x) //clear lcd function
{
lcd.setCursor(0,x);
for(int i=0; i<16; ++i)
{
lcd.write(' ');
}
}

int angle=0;
int redPin= A3;
int greenPin = A2;
int bluePin = A1;
const int ledPin = 5;
const int buzzerPin = 4;
const int ldrPin = A0;

const byte rows = 4; //four rows since the keypad is full
const byte columns = 4; //four columns, same as above
char buttons[rows][columns] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

int bluetoothTx = 0; //bluetooth tx to 0 pin
int bluetoothRx = 1; //bluetooth rx to 1 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); //for bluetooth 

byte rowP[rows] = {13, 12, 11, 10}; //row pins of the keypad
byte columnsP[columns] = {9, 8, 7, 6}; //column pins of the keypad
Keypad pad = Keypad( makeKeymap(buttons), rowP, columnsP, rows, columns );    //create keypad
Servo ServoG; 
Servo ServoS;// 
//LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module

void setup(){
    ServoS.attach(2);
    ServoG.attach(3);  // attaches the servo on pin 9 to the servo object

 //lcd
    lcd.begin(16, 2);
    lcd.backlight();
    lcd.setCursor(0,0);
  for (int positionCounter = 0; positionCounter < 35; positionCounter++) {
    {
      lcd.setCursor(16,0);
      lcd.print(" Welcome to Group 5 ");
      lcd.setCursor(17,1);
      lcd.print("spectrophotometer");
    }
      lcd.scrollDisplayLeft(); // scroll one position left:
      delay(200); // wait a bit:
  }
      lcd.clear();
      //lcd.setCursor(4,0);
      //lcd.print("For KEYPAD");
      delay (1000);
      for (int positionCounter1 = 0; positionCounter1 < 25; positionCounter1++) {
      lcd.setCursor(0,0);
      lcd.print(" Choose Sample : Press A - D ");
      lcd.setCursor(0,1);
      lcd.print(" Choose Grating : Press 1 - 6 ");
      lcd.scrollDisplayLeft(); // scroll one position left:
      delay(200); // wait a bit:
      }
      lcd.clear();
 
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);

      Serial.begin(9600);
      pinMode(ledPin, OUTPUT);
      pinMode(buzzerPin, OUTPUT);
      pinMode(ldrPin, INPUT);

     bluetooth.begin(9600);
  
}

void loop(){

  buzzerldrled();

  char key = pad.getKey();  //get the char from the keypad
  if(bluetooth.available()>0 ) //receive number from bluetooth
  {
    {
    key = bluetooth.read(); //save the received number to toSend
    }
  }
  
      if(key == 'A')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(0);
      lcd.setCursor(1,0);
      lcd.print("Sample 1");
      }
      if(key == 'B')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(45);
      lcd.setCursor(1,0);
      lcd.print("Sample 2");
      }
      if(key == 'C')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(90);
      lcd.setCursor(1,0);
      lcd.print("Sample 3");
      }
      if(key == 'D')
      {
      Serial.println("key = ");
      Serial.print(key);
      ServoS.write(45);
      lcd.setCursor(1,0);
      lcd.print("Sample 4"); 
      }

      if(key == '0')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 0;
      ServoG.write(angle);
      delay (1000);
      //servograting.write(0);
      lcd.setCursor(0,0);
      lcd.print("          ");
      lcd.setCursor(0,1);
      lcd.print("reset");
      setColor(0 ,0, 0); // Green Color
      }
      
      if(key == '1')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 0;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:25 ");
      setColor(0, 255, 0); // Green Color
     
      }
      
      if(key == '2')
      {
        
      Serial.println("key=");
      Serial.print(key);
      angle = 120;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:120");
     setColor(0, 0, 255); // Blue Color
     
      }
      
      if(key == '3')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 50;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:50 ");
      setColor(170, 0, 255); // Purple Color
      
      }
 
      if(key == '4')
      {
      Serial.println("key=");
      Serial.print(key);
      angle =90;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:90 ");
      setColor(255, 255, 255); // White Color
    
     
      }
      
      if(key == '5')
      {
      Serial.println("key=");
      Serial.print(key);
      angle = 78;
      ServoG.write(angle);
      delay (1000);
      lcd.setCursor(0,1);
      lcd.print("Angle:78 ");
      setColor(255, 255, 0); // Yellow Color
      
     
      }

      if(key == '*'){
             if (angle <180){ 
                  angle+=1;
                  ServoG.write(angle);
                  lcd.clear(); 
                 EEPROM.read(angle);
                 lcd.setCursor(0,1);
                 lcd.print("Angle:");
                 lcd.print(angle);
             }
}

            
        if(key == '#'){
             if (angle >0){ 
                  angle-=1;
                  ServoG.write(angle);
                  lcd.clear(); 
                  EEPROM.read(angle);
                  lcd.setCursor(0,1);
                  lcd.print("Angle:");
                  lcd.print(angle);
             }

        }
        if(key == '8'){
          EEPROM.write(key,angle);
          lcd.setCursor(0,1);
          lcd.print(angle            );
          delay(1000);
        }
      
}

void buzzerldrled() {

      int ldrStatus = analogRead(ldrPin);
      if (ldrStatus >= 400) {
      tone(buzzerPin, 100);
      digitalWrite(ledPin, HIGH);
      delay(100);
      noTone(buzzerPin);
      digitalWrite(ledPin, LOW);
      delay(100);
      Serial.println("----------- ALARM ACTIVATED -----------");
      lcd.setCursor(0,1);
      lcd.print(" ALARM ACTIVATED !!!!!");
      delay(500);
      lcd.clear();
      }

    else {
      noTone(buzzerPin);
      digitalWrite(ledPin, LOW);
      }
}      

 
void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

mrah96:
what i would like to do is to be able for the keypad (buttons 1,2,3,4,5) to be assigned a new value although there are already pre-assigned value in them.
pls help me with this.

I described one way to do what you want. Was that not helpful?

what i would like to do is to be able for the keypad (buttons 1,2,3,4,5) to be assigned a new value

What are the new values?

How will the new values be entered?