AnalogRead to DigitalWrite programming format advise

I connected a keypad to my Uno board using the voltage divider and AnalogRead method in order to have my digital pins available to control my 4-channel relay board by pressing a specified key. I am able to read which key is pressed through my serial monitor. My experience in programming is limited and I am not quite sure how or where to add the PinMode and DigitalWrite commands. I would like to be able to send and latch a low signal to all 4 relay modules with each having its own key assigned. Below is the program I am using to read the keypad on my serial monitor. Any advise or example is appreciated.

int keypressed = 0;
int keyboardPin = 0; // Analog input pin that the keypad is attached to
int keyboardValue = 0; // value read from the keyboard

void setup(){
Serial.begin(9600); //hardware serial to PC

}

void loop(){

keyboardValue = analogRead(keyboardPin); // read the keyboard value (0 - 1023)
while (keyboardValue < 25){
//do nothing until a key is pressed
keyboardValue = analogRead(keyboardPin);
delay(50);
}//end of do nothing till a key is pressed

readkeyboard(); //get the value of key being pressed “keypressed” i.e. 0-9

}

//read the keyboard routine
void readkeyboard(){
keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
if (keyboardValue <25){keypressed = 0;}
if ((keyboardValue >25) && (keyboardValue < 67)){keypressed = 1;}
if ((keyboardValue >67) && (keyboardValue < 108)){keypressed = 2;}
if ((keyboardValue >108) && (keyboardValue < 162)){keypressed = 3;}
if ((keyboardValue >162) && (keyboardValue < 253)){keypressed = 4;}
if ((keyboardValue >253) && (keyboardValue < 361)){keypressed = 5;}
if ((keyboardValue >361) && (keyboardValue < 479)){keypressed = 6;}
if ((keyboardValue >479) && (keyboardValue < 619)){keypressed = 7;}
if ((keyboardValue >619) && (keyboardValue < 765)){keypressed = 8;}
if ((keyboardValue >765) && (keyboardValue < 819)){keypressed = 9;}
if ((keyboardValue >819) && (keyboardValue < 889)){keypressed = 0;}
if ((keyboardValue >889) && (keyboardValue < 938)){keypressed = 0;}
if (keyboardValue >938){keypressed = 0;}

while (keyboardValue > 25) {
delay (100);
keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
}//wait until key no longer being pressed before continuing

Serial.println(keypressed); // print the value back to the Serial view window on your PC
delay(1000); // wait 1000 milliseconds before the next loop
}

So, write what you want to do in English first and then the program will flow out of it.

IF the key pressed is 1, THEN you want to write a relay pin HIGH.

if(keypressed == 1){
     digitalWrite(relayPin, HIGH);
}

pinMode almost always goes in setup since you only need to set that once.

Welcome to the forum!

First thing I notice is that you read the keypad 4 times but you only use it in your readkeyboard() function. maybe this is because using delay() is interfering with reading the keypad?

I got rid of the extra reads and the delays()

also you had 4 if statements setting keypressed = 0 it can be reduced to 1

Changed void readkeyboard() to byte readkeyboard() and moved keyboardvalue and keypressed to be local to that function and let the function return the keypressed variable which is stored in a new key variable in void loop().

finally put in a state machine (switch statement) that can be used to determine what happens after a key is pressed.

int keyboardPin = 0;    // Analog input pin that the keypad is attached to
byte key;

void setup(){
  Serial.begin(9600);  //hardware serial to PC
 
}

void loop(){
  
  key = readkeyboard(); //get the value of key being pressed "keypressed" i.e. 0-9
    switch (key)
  {
    case 0:
    Serial.println "State Machine = 0"
    // if you dont want anything to happen here dont put anything here
    break;
    case 1:
    Serial.println "State Machine = 1"
    buttonOneFunction();
    break;
    case 2:
    Serial.println "State Machine = 2"
    buttonTwofunction();
    break;
    // etc. etc. etc...........
  }
}

//read the keyboard routine
byte readkeyboard(){
   byte keypressed = 0;
   int keyboardValue = 0;   // value read from the keyboard
   keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
   keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
   if ((keyboardValue >25) && (keyboardValue < 67)){keypressed = 1;}
   if ((keyboardValue >67) && (keyboardValue < 108)){keypressed = 2;}
   if ((keyboardValue >108) && (keyboardValue < 162)){keypressed = 3;}
   if ((keyboardValue >162) && (keyboardValue < 253)){keypressed = 4;}
   if ((keyboardValue >253) && (keyboardValue < 361)){keypressed = 5;}
   if ((keyboardValue >361) && (keyboardValue < 479)){keypressed = 6;}
   if ((keyboardValue >479) && (keyboardValue < 619)){keypressed = 7;}
   if ((keyboardValue >619) && (keyboardValue < 765)){keypressed = 8;}
   if ((keyboardValue >765) && (keyboardValue < 819)){keypressed = 9;}
   if ((keyboardValue < 25) || (keyboardValue > 819)){keypressed = 0;}
   Serial.println(keypressed);      // print the value back to the Serial view window on your PC
   return(keypressed);

   
}

This

if (keyboardValue <25){keypressed = 0;}
   if ((keyboardValue >25) && (keyboardValue < 67)){keypressed = 1;}
   if ((keyboardValue >67) && (keyboardValue < 108)){keypressed = 2;}
   if ((keyboardValue >108) && (keyboardValue < 162)){keypressed = 3;}

can be simplified to

if (keyboardValue <25){
     keypressed = 0;
   }
   else if (keyboardValue < 67){
       keypressed = 1;
   }
   else if (keyboardValue < 108){
      keypressed = 2;
   }
   else if (keyboardValue < 162) {
      keypressed = 3;
   }

Putting things on different lines is just a personal preference to aid reading.

…R

late posting.

from what the others have already said.

heres a example of a way to deal with the keyboard

const byte relayPin = 13;
int keyboardPin = 0;    // Analog input pin that the keypad is attached to
//int keyboardValue = 0;   // value read from the keyboard
byte prevkey = 0;

void setup() {
  Serial.begin(9600);  //hardware serial to PC
  pinMode(relayPin, OUTPUT);
}

void loop() {
  byte key = readkeyboard(); //get the value of key being pressed "keypressed" i.e. 0-9
  //value of key is now equal to the key being pressed

  //if you only want to do this once use a state detection if not remove lines using
  //prevkey

  if (key != prevkey) {

    //the switch is like writting 10 ifs but it allows the processor to sort faster

    switch (key) {
      case 0:
        Serial.println("no key pressed");
        digitalWrite(relayPin, LOW);//if LOW is off relay shield would be other way around
        break;
      case 1:
        Serial.println("key one pressed");
        digitalWrite(relayPin, HIGH);//if HIGH is on relay shield would be other way around
        break;
      case 2:
        Serial.println("key two pressed");
        break;
      case 3:
        Serial.println("key three pressed");
        break;
      case 4:
        Serial.println("key four pressed");
        break;
      case 5:
        Serial.println("key five pressed");
        break;
      case 6:
        Serial.println("key six pressed");
        break;
      case 7:
        Serial.println("key seven pressed");
        break;
      case 8:
        Serial.println("key eight pressed");
        break;
      case 9:
        Serial.println("key nine pressed");
        break;
    }
  }
  prevkey = key;
}


//read the keyboard routine
byte readkeyboard() {
  int keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
  keyboardValue = analogRead(keyboardPin); // read the value (0-1023)
  //double read of keyboardValue done on purpose to get a better read
  //may not be required

  static byte keypressed = 0;
  if (keyboardValue < 25) {
    keypressed = 0;
  }
  if ((keyboardValue >= 25) && (keyboardValue < 67)) {
    keypressed = 1;
  }
  if ((keyboardValue >= 67) && (keyboardValue < 108)) {
    keypressed = 2;
  }
  if ((keyboardValue >= 108) && (keyboardValue < 162)) {
    keypressed = 3;
  }
  if ((keyboardValue >= 162) && (keyboardValue < 253)) {
    keypressed = 4;
  }
  if ((keyboardValue >= 253) && (keyboardValue < 361)) {
    keypressed = 5;
  }
  if ((keyboardValue >= 361) && (keyboardValue < 479)) {
    keypressed = 6;
  }
  if ((keyboardValue >= 479) && (keyboardValue < 619)) {
    keypressed = 7;
  }
  if ((keyboardValue >= 619) && (keyboardValue < 765)) {
    keypressed = 8;
  }
  if ((keyboardValue >= 765) && (keyboardValue < 819)) {
    keypressed = 9;
  }
  if ((keyboardValue >= 819) && (keyboardValue < 889)) {
    keypressed = 0;
  }
  if ((keyboardValue >= 889) && (keyboardValue < 938)) {
    keypressed = 0;
  }
  if (keyboardValue >= 938) {
    keypressed = 0;
  }
  return keypressed;
}