1

I'm using the keypad library to return a number 1-9. (im fairly sure this returns an int) I have an array (data) showing the state of each button press. so i would like the number returned by the keypad (key) to be the index number that gets edited from 0 to 1 and from 1 to 0.

    //REMOTE KEYPAD TO CONTROL OUTPUTS OVER SERIAL

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},

};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int iAm = 1;                                    //this is device number 1
float checkSum = 0.00;                          //check sum to send as serial
float voltage = 5.26;                           //system voltage
bool data[8] {0, 0, 0, 0, 0, 0, 0, 0, 0};       //keypad output call

void setup() {
  Serial.begin(9600);
}


void loop() {

  int key = keypad.getKey();
  
  
  if (key) {
        data[key] = 1;
    
    //checkSum = voltage * (iAm + data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8] + data[9]); 
    //Serial.print(iAm);
    //Serial.print("/");
    Serial.print(data[1]);
    Serial.print("/");
    Serial.print(data[2]);
    Serial.print("/");
    Serial.print(data[3]);
    Serial.print("/");
    Serial.print(data[4]);
    Serial.print("/");
    Serial.print(data[5]);
    Serial.print("/");
    Serial.print(data[6]);
    Serial.print("/");
    Serial.print(data[7]);
    Serial.print("/");
    Serial.print(data[8]);
    Serial.print("/");
    Serial.print(data[9]);
    Serial.print("/");
    Serial.print(voltage);
    Serial.println("/");
    //Serial.println(checkSum);
      
  }
    
}

i can edit the index location by typing data[1] = 1 but not as a variable so i suspect an index cant be addressed as int? but i don't know what it should be addressed as or how to change it to that.

2
  • 3
    im fairly sure this returns an int -- Wrong. It returns a char. You need to then convert that to an integer before you can use it as an array index. Hint: subtract zero.
    – Majenko
    Commented Mar 13, 2021 at 20:04
  • {'1','2','3'}, {'4','5','6'}, {'7','8','9'} these are ASCII characters ... the array is equal to {49, 50, 51}, {52, 53, 54}, {55, 56, 57} ... take a wild guess what the array should be, if you want values of 1 to 9
    – jsotola
    Commented Mar 13, 2021 at 22:09

1 Answer 1

1

not going to pretend i understand why all my keypad numbers are 48 higher than the number printed on the keypad but:

int key2 = key -48;

works

1
  • 4
    One word to google: "ASCII".
    – Majenko
    Commented Mar 13, 2021 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.