Prac3 Part 1 — Raspberry Pi Pico simulation

An interactive Raspberry Pi Pico circuit simulation you can run free in your browser on Velxio, by jgrobler-e.

Components

Sketch code

char keyMap[4][4] =
{
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
}; //Assign chars to the corresponding rows and columns
byte rowPins[4] = {14, 13, 12, 11}; //Keypad Row assignment
byte colPins[4] = {10, 9, 8, 7}; //Keypad Column assignment

const int segPins[7] = {0,1,2,3,4,5,6};

const bool digits[10][7] =
{
 {1,1,1,1,1,1,0}, //0
 {0,1,1,0,0,0,0}, //1
 {1,1,0,1,1,0,1}, //2
 {1,1,1,1,0,0,1}, //3
 {0,1,1,0,0,1,1}, //4
 {1,0,1,1,0,1,1}, //5
 {1,0,1,1,1,1,1}, //6
 {1,1,1,0,0,0,0}, //7
 {1,1,1,1,1,1,1}, //8
 {1,1,1,1,0,1,1}  //9
};

void showDigit(int num)
{
    for(int i = 0; i < 7; i++)
    {
        digitalWrite(segPins[i], digits[num][i] ? HIGH : LOW);
    }
}


void setup() 
{
  Serial.begin(115200);  
  for(int r = 0; r < 4; r++)
  {
    pinMode(rowPins[r], OUTPUT);
    digitalWrite(rowPins[r], HIGH);
  } //Sets Row Pins as outputs

  for(int c = 0; c < 4; c++)
  {
    pinMode(colPins[c], INPUT_PULLUP);
  } //Sets Column Pins as Inputs

  for(int i = 0; i < 7; i++)
    {
        pinMode(segPins[i], OUTPUT);
    }

}
char readKeypad() //Reads the input values of keypad
{
  for(int row = 0; row < 4; row++)
  {      
    for(int r = 0; r < 4; r++) // Set all rows HIGH
    {
      digitalWrite(rowPins[r], HIGH);
    }
       
    digitalWrite(rowPins[row], LOW); // Drive current row LOW

    for(int col = 0; col < 4; col++)
    {
      if(digitalRead(colPins[col]) == LOW) //Reads if the column is LOW
      {
        delay(20); // debounce
        while(digitalRead(colPins[col]) == LOW);
        {
          return keyMap[row][col]; //Returns the column and row of keymap
        }
      }
    }
  }
  return '\0'; //Returns a value not defined in the keymap
}

void loop()
{
  char key = readKeypad();
  if(key != '\0') //If the value returned is not in keymap it doesn't execute the code
  {
    Serial.print("Pressed:");
    Serial.println(key); //For simulation to indicate button presses
    showDigit(key - '0');
                 
  }
}