i have a set of pins that are set to INPUT_PULLUP
and a set of pins that are set to OUTPUT
to scan the keys i write the output pins 'low'
and read the input of the INPUT_PULLUP pins
how do i attatch diodes to prevent ghosting?
also, does output pins written low act as ground?
One way is to have a diode in series with every switch.
Yes, you can think of a pin set to OUTPUT and LOW as connected to ground inside the Arduino. OUTPUT and HIGH means the pin is connected to 5V inside the Arduino. A pin set to INPUT is not connected to either GND or 5V. A pin set to INPUT_PULLUP is connected to 5V with a resistor of around 50 K.
What are your OUTPUT pins set to when they are not LOW? Take care: if they are set to HIGH you could press multiple switches and cause a short damaging the Arduino. I would recommend you set those OUTPUT pins to INPUT when they are not LOW.
for(byte rowTemp=0;rowTemp<6;rowTemp++){
pinMode(rowPin[rowTemp],OUTPUT);
digitalWrite(rowPin[rowTemp],LOW); //make row pins work as ground one by one
for(byte colTemp=0;colTemp<15;colTemp++){
keystate[15rowTemp+colTemp]=!digitalRead(colPin[colTemp]); //read keys that are pressed, one row at a time
if(keystate[15rowTemp+colTemp]!=prevKeystate[15rowTemp+colTemp]){ //if the keystate has changed,
stateChanged[15rowTemp+colTemp]=millis(); //save the current time to debounce the key
}
}
pinMode(rowPin[rowTemp],INPUT); //reset the row pins to their former state
}