Can't read PINx with Arduino Mega 2560

I created a class to manipulate a 16 channel mux/demux ( Cd74hc4067), but I want to use register manipulation for it. I define if I'm creating a mux or demux when the object is instanciated, and the mux is working fine, but the demux is not. When using it as a demux, the arduino should use the value read from the PINx register to find which button was pressed, and if any button was pressed, the function press() should return the number of the button+1, but it allways return 0.

Here is the code:



typedef decltype(&PORTB) PortPointer;  //detects the type of the pointer that points to a register

class multiplex{
/*
  O Mux/DeMux deve ser conectado a um registrador de 8 bits específico, nessa ordem:
  The Mux/DeMux mus be conected to a specific 8 bit register:
  |B7|B6|B5|B4|B3|B2|B1|B0| reg
  |X  |X   |X  |SG|S3 |S2|S1 |S0| mux/demux

*/  
  public:
  PortPointer p_port; //the p_port variable needs to receive the ADDRESS of a PORT register
  PortPointer p_ddr;  
  PortPointer p_pin;  
  bool estado; //controls whether it is a multiplexer (1) or demultiplexer (0)
  byte m = 0;

  multiplex( PortPointer add, PortPointer prt, PortPointer pin, bool est){ //add and prt should receive register addresses, i.e. something like &PORTC
    p_ddr = add;
    p_port = prt;
    p_pin = pin;
    estado = est;
    
    if(estado){ //sets the state of the register pins for the multiplexer
      *p_ddr = (*p_ddr)|(0b00011111);  
    }
    else{ //if demux, the sig pin must be set as INPUT_PULLUP 
      *p_ddr = (*p_ddr)&(0b11110000);
      *p_ddr = (*p_ddr)|(0b00010000);
      *p_port = (*p_port)|(0b00010000);
    }
    
  }

  byte press(){
    *p_port = (*p_port)&(0b11110000);
    for(byte i=0; i<16; i++){
     
      m = ~((*p_pin>>4)|0xFE);

      if(m){ 
        delay(100);
        return (i+1); 
      }//if
      
      *p_port++;
    }

    return 0;
    
  }//press


  void aciona(byte qual){
    *p_port = (0b00010000)|qual;
  }//aciona

  void desliga(){
    *p_port = 0;
  }
  
}; // class

multiplex mux1(&DDRA, &PORTA, &PINA, 0);

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

}

void loop() {

  static byte a = 0;
  a = mux1.press();
  
  if(a!=0) Serial.println(a);
  

}

Cd74hc4067 is analog mux/demux, its output signal is an analog value. You can't use PIN register to read the demux value, so your demux code is completely incorrect.

It shouldn't be a problem, since I'm setting the sig pin as INPUT_PULLUP, and the signal that is sent by pressing the buttons is GND. So, the PINx sig bit can only be LOW or HIGH

If I'm not wrong in the demux port initialization you have to invert the value of the first 5 bits in the port direction register:
as this sets bit 0-3 as output and bit 4 as input

P.S.
The fact that are using an analog multiplexer should not be a problem

      *p_ddr = (*p_ddr)&(0b11101111);  // set bit4 as input
      *p_ddr = (*p_ddr)|(0b00001111);  // set bit0-3 as output

Thanks, that was really wrong in my code. But still didn't work

Indeed. There is no such thing as a digital signal.

Lazy, I thought chatGPT might blow hard better than I.

The prompt: an engineer said there is no such thing as a digital signal in a computer. what did she mean?

chatGPT response

It sounds like the engineer was referring to the idea that while we often talk about digital signals in the context of computing, at the most fundamental level, everything in a computer is still based on analog principles. Here’s a breakdown of what might be meant:

  1. Physical Reality of Digital Signals: At the physical layer, signals in a computer are transmitted as electrical voltages or currents. These signals are not perfectly discrete but vary continuously. Even though we think of digital signals as having distinct values (e.g., 0 and 1), the actual voltages or currents that represent these values can be subject to noise and fluctuations. So, what we call "digital" signals are actually analog in nature.

  2. Quantization and Representation: In digital systems, we approximate continuous signals by quantizing them into discrete values. For example, a digital signal might be represented by a voltage level that is interpreted as either a high or low state. The process of quantizing and interpreting these levels as discrete values (0s and 1s) is what makes the signal "digital" from a functional perspective, but the underlying representation is still analog.

  3. Error and Noise: Digital systems use various techniques to handle noise and errors, like error detection and correction, to ensure that the signals are correctly interpreted despite the inherent analog nature of the hardware. This means that while the system operates digitally, it relies on analog processes and components.

So, the engineer might be highlighting that despite our digital abstractions and interpretations, the physical implementation and transmission of these signals are ultimately analog.

The prompt (I had said THX and CU): sorry, me again. could you cut the size of that in half or so?

chatGPT compact reply

Of course! Here’s a more concise explanation:

When the engineer said there's no such thing as a digital signal in a computer, they were likely pointing out that while we think of digital signals as discrete (like 0s and 1s), the actual signals are represented as varying electrical voltages or currents, which are inherently analog. Even though these signals are used in digital systems, their physical manifestation is still continuous and subject to noise. Essentially, digital signals are a practical abstraction built on underlying analog principles.

a7

Ok, for the press function you can use something like this:

  byte press(){
    for(byte i=0; i<16; i++){
      *p_port=((*p_port)&(0b11110000))|i;
      m = (*p_pin) & 0x10;
      if(!m){ 
        delay(100);
        return (i+1); 
      }//if      
    }
    return 0;    
  }//press

Soory, I dont understand... Why do you invert the signal?

Because the input has a pullup and the op says it uses it to read buttons connected to ground.
It returns the first pressed button