1

I'm currently having problems with my code it has several modules within the main one is my current keypad one working of a mcp23017. what I needed it to do is count how long a key is pressed and upon button release it sets that variable so other modules can read it and complete an action they are required to do for example a seven segment to display a number. However, as you will see currently in cases 1 - 12 as soon as the button has been released it sets the varaible back to notPRESSED. Code below:

{ // module keypad


  switch (module_keypad_i) {

    case 0:

      setRow(0); // Pull row 0 Low

      if (!(ColumnNum & B00000100)) { // 1 detected
        keypadDebounceTime = millis();
        module_keypad_i = 1;
        break;
      }

      if (!(ColumnNum & B00000010)) { // 2 detected
        keypadDebounceTime = millis();
        module_keypad_i = 2;
        break;
      }

      if (!(ColumnNum & B00000001)) { // 3 detected
        keypadDebounceTime = millis();
        module_keypad_i = 3;
        break;
      }

      rowSet(1); // Pull row 1 Low

      if (!(nColumnNum & B00000100)) { // 4 detected
        keypadDebounceTime = millis();
        module_keypad_i = 4;
        break;
      }

      if (!(ColumnNum & B00000010)) { // 5 detected
        keypadDebounceTime = millis();
        module_keypad_i = 5;
        break;
      }

      if (!(ColumnNum & B00000001)) { // 6 detected
        keypadDebounceTime = millis();
        module_keypad_i = 6;
        break;
      }

      setRow(2); // Pull row 2 Low

      if (!(ColumnNum & B00000100)) { // 7 detected
        keypadDebounceTime = millis();
        module_keypad_i = 7;
        break;
      }

      if (!(ColumnNum & B00000010)) { // 8 detected
        keypadDebounceTime = millis();
        module_keypad_i = 8;
        break;
      }

      if (!(ColumnNum & B00000001)) { // 9 detected
        keypadDebounceTime = millis();
        module_keypad_i = 9;
        break;
      }

      setRow(3); // Pull row 3 Low

      if (!(ColumnNum & B00000100)) { // * detected
        keypadDebounceTime = millis();
        module_keypad_i = 10;
        break;
      }

      if (!(ColumnNum & B00000010)) { // 0 detected
        keypadDebounceTime = millis();
        module_keypad_i = 11;
        break;
      }

      if (!(ColumnNum & B00000001)) { // # detected
        keypadDebounceTime = millis();
        module_keypad_i = 12;
        break;
      }

      else lcd.setCursor(0, 1);
      break;

    case 1: // Read button 1 press   
      key = 1;
      Keypad_Debounce_Time(0);
      setRow(0);

      if (ColumnNum & B00000100) {
        //keypad_state = notPRESSED;
        //Serial.println(keypad_state);
        keypadStatus[0] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 2: // Read button 2 press      
      key = 2;
      Keypad_Debounce_Time(1);
      setRow(0);

      if (ColumnNum & B00000010) {
        keypadStatus[1] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 3: // Read button 3 press 
      key = 3;
      Keypad_Debounce_Time(2);
      setRow(0);

      if (ColumnNum & B00000001) {
        keypadStatus[2] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 4: // Read button 4 press
      key = 4;
      Keypad_Debounce_Time(3);
      setRow(1);

      if (ColumnNum & B00000100) {
        keypadStatus[3] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 5: // Read button 5 press
      key = 5;
      Keypad_Debounce_Time(4);
      setRow(1);

      if (ColumnNum & B00000010) {
        keypadStatus[4] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 6: // Read button 6 press
      key = 6;
      Keypad_Debounce_Time(5);
      setRow(1);

      if (ColumnNum & B00000001) {
        keypadStatus[5] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 7: // Read button 7 press
      key = 7;        
      Keypad_Debounce_Time(6);
      setRow(2);

      if (ColumnNum & B00000100) {
        keypadStatus[6] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 8: // Read button 8 press
      key = 8;
      Keypad_Debounce_Time(7);
      setRow(2);

      if (ColumnNum & B00000010) {
        keypadStatus[7] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 9: // Read button 9 press
      key = 9;
      Keypad_Debounce_Time(8);
      setRow(2);

      if (ColumnNum & B00000001) {
        keypadStatus[8] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 10: // Read button * press
      Keypad_Debounce_Time(9);
      setRow(3);

      if (ColumnNum & B00000100) {
        keypadStatus[9] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 11: // Read button 0 press
      key = 0;
      Keypad_Debounce_Time(10);
      setRow(3);

      if (ColumnNum & B00000010) {
        keypadStatus[10] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    case 12: // Read button # press
      Keypad_Debounce_Time(11);
      setRow(3);

      if (ColumnNum & B00000001) {
        keypadStatus[11] = notPRESSED;
        module_keypad_i = 0;
      }
      break;

    default: module_keypad_i = 0;
  }
}


void Keypad_Debounce_Time(int status_num) {  { 
n = millis() - keypadDebounceTime;
Serial.println(n);
if (n < 300)keypad_status[status_num] = partialPRESSED;
else if (300 <= n && n < 1000) keypad_status[status_num] = normalPRESSED;
else if (1000 <= n && n < 5000) keypad_status[status_num] = heldPRESSED;
else keypad_status[status_num] = stuckPRESSED;
  }
}

void setRow(byte RowNum){
//Set row
Wire.beginTransmission(MCP_ADDRESS);
Wire.write(MCP_GPIOA);
Wire.write(sRowPattern[RowNum]);
Wire.endTransmission();

// Read the column value
Wire.beginTransmission(MCP_ADDRESS);
Wire.write(MCP_GPIOA);
Wire.endTransmission();
Wire.requestFrom(MCP_ADDRESS, 1);
nColumn = Wire.read();
}

The keypad status for each is set up like this:

enum button_type { notPRESSED, partialPRESS, normalPRESS, heldPRESS, stuckPRESS};
button_type keypadStatus[12];

So all my segment module is doing is reading each key pressed.

I've tried adding some simple non blocking delays in the cases to try and delay it and give the other module time to react to the change like below for one of my cases:

case 12: // Display button # presses
      n = millis() - keypadDebounceTime;

      SetRow(3);

      if (nColumn & B00000001) {
        pressDuration(11);
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {
          previousMillis = currentMillis;
          keypadStatus[11] = notPRESSED;
          module_keypad_i = 0;
        }
      }
      break;

The pressDuration function works similar to the keypadDebounceTime but just calculates what kind of press it is sets it when the button is released then waits a set interval to set it back to notPRESSED. However, even when I have set my interval to 5 seconds I get no response from the segment display which all it is doing is waiting for the different button press states. I have older code that does the action on button release but all that does is set it to the next number not set the number wait some time then clear it. Is there something fundamental I am missing I have been scratching my head at this for a couple days now.

Apple
  • 11
  • 2

0 Answers0