Detecting values from 54 digital pins on Mega 2560

Hi everyone im a newbie on this arduino programming thing, and now i have a problem on detecting value changes from arduino digital pins, by the way i used DFRduino mega board for this project.

Here’s what i wanted to do,
I want to detect value changes from arduino digital pins like the basic example on this link

So i make a simple circuit like this :

I have succeed to detect value changes using my circuit, when i push the button the digitalread() method from pressed pin will give me the value of 1, and 0 if i dont press the button. But i only can detect that value changes from pin number 2 until 13 and it didnt work with the rest of the pin. The goal is i want to detect those value changes from all 54 pins, or 40 pins minimum for now.

Here’s my Code:

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);      // set up Serial library at 9600 bps
  for( int i = 2; i < 40;i++)
  {
    pinMode(i, INPUT);
  }
}

void loop()                       // run over and over again
{
  int buttonState;
  String msg = "";
  for( int j = 2; j < 40 ;j++ )
  {
    buttonState = digitalRead(j);
     if(buttonState == 1){
       msg +=  String(j) + ",";
    }
  }
  if(msg != "")
    {
        Serial.println(msg);
    }
  delay(500);
}

Sorry for my bad english. Any help would be appreciated, thank you guys…

How are switches wired? Pin connects to +5 when pressed, pulled low otherwise?

Try this:

void loop(){
for (byte j = 2; j<40; j=j+1){
if (digitalRead(j) == LOW){
digitalwrite ("0");
}
else {
digitalWrite("1");
}
}
Serial.println("");
}

Hi Crossroad thanks for you help, your code is working only for pin number 2 until 13. when i pressed on pin number 14 the value didnt change, its still zero (LOW). So the question is why the code is only work at certain pins? does another pins have another way to receive an input? Hmm.. I dont exactly understands how the cable are wired because its not me who wired the cable, but maybe you can look how the wire are wired through this picture, because its not a complicated wiring.

Does this image help? Thank you

Add this to setup: DDRA = 0xff; //set ports as outputs DDRB = 0xff; //set ports as outputs DDRC = 0xff; //set ports as outputs DDRD = 0xff; // set ports as outputs DDRE = DDRE | 0b11111100; // set ports as outputs, leave D0, D1 alone for Rx,Tx DDRF = 0xff; //set ports as outputs DDRG = 0xff; // set ports as outputs DDRH = 0xff; // set ports as outputs DDRJ = 0xff; //set ports as outputs DDRK = 0xff; // set ports as outputs DDRL = 0xff; // set ports as outputs

This picture isn't really helpfull. We need to see which pins are used. I'm only guessing than buttons are connected to pins 2 - 13 and 22 - 53 . Rest of them are not used. Post any drawing or tell us to which pins you connect buttons .

@Crossroad & waski

Big thankssss to both of youuuu!!! Now i can detect 40 digital sensorss… You was right Waski only pin number 2-13 and 22-53 was usedd… So i detect 11 sensor using pin number 2-13 and 31 sensors using pin number 22-53… Using simple code like this

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);      // set up Serial library at 9600 bps
  for( int i = 2; i < 54;i++)
  {
    pinMode(i, INPUT);
  } 
}

void loop()                       // run over and over again
{
  int buttonState;
  String msg = "";
  byte k;
    for(k = 2; k < 14;k++ )
  {
    buttonState = digitalRead(k);
    if(digitalRead(k) == HIGH)
    {
      msg += String(k) + ", ";
    }

  }
  
  for(k = 22; k < 55;k++ )
  {
    buttonState = digitalRead(k);
    if(digitalRead(k) == HIGH)
    {
      msg += String(k) + ", ";
    }

  }
  if(msg != "")
      Serial.println(msg);
      
  delay(500);
}

Thank you for your guess waski… :smiley:

@Crossroads :
you said that your code is use to set ports as output… Can you give me a little explanation about your code? what is the difference between DDRG = 0xff; and pinmode(1, OUTPUT) ? and what was DDRA-DDRF mean? Im sorry for my newbie question. Im very new and know nothing about this arduino can i ask some reference where can i learn more about this arduino? maybe you know some recommended ebooks for me to buy…

Read the datasheet, many answers are there:

Each port pin consists of three register bits: DDxn, PORTxn, and PINxn. As shown in ”Register description” on page 91, the DDxn bits are accessed at the DDRx I/O address, the PORTxn bits at the PORTx I/O address, and the PINxn bits at the PINx I/O address. The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input pin. If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running.

I used register access 'shortcut' to set 8 outputs at a time vs looping thru pinMode. Mega has 11 ports (88 IO), altho not all pins have 8 IO pins, and not all IO pins are brough out to connectors on the Mega2560 board; I don't know what's brought on your DFRduino, so I figured just set them all to outputs. I guess you needed them set to inputs tho, so I guess I got that backwards.