pro micro

I made a project that includes 10 buttons that i wanna control with my pro micro, but i can’t seem to make a code that works. the wiring is as shown in the attachment. if anyone already has a working code for me or can hellp i would highly appreciate.

thanks , Dim

I have not used the pro micro before but let me have a go. Not the full code but some ideas I have used on different processor to read matrix keypads - quite similar to your switch wiring.

From you picture the pro micro uses the 32u4. That chip maps the ADC inputs to PORT F. A0 to A3 maps to bits 7 to 4 in PORT F. So initialise pins as follows:

void setup()  {
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH);

  pinMode(16, OUTPUT);
  digitalWrite(16, HIGH);

  DDRF &= 0x0F;      // Set bits 4 to 7 (A3 to A0) as input. (bit = 0 -> input)
  PORTF |= 0xF0;     // Enable internal pull-up for input bits 4 to 7

If we now read the PINF buffer we will get 0xF? since all the bits are high.

Pressing a switch while pulling it’s red line (your picture) low will then read as a low for the relevant bit. To see which button was pressed use something like the following code:

  digitalWrite(14, LOW);
  int nStatus = ~(PINF >> 4); // & 0x0F;      // Move Bits 4:7 to 3:0 (will normally be high)
                                              // invert to make them normally low
  digitalWrite(14, HIGH);
  if ( nStatus )  {                           // a button was pressed
    int nButton = 0;
    while ( (nStatus & (1 << nButton)) == 0 )
    return nButton + 1;

Use a loop to test all three red lines. You need to add code to give your button number for the loop iteration and nButton value.

Hope that gives you some ideas.