Hi there,
i would like a little advice from someone who already has used these kind of products.
I am able to control every single LED as i want.
I still have a little trouble to detect which button is pressed.
concerning the buttons we've got:
4 Rows named "SWITCH"
4 Cols named "SWITCH GND"
This is what i'm doing ,to check if i am able to detect when the first row is pressed :
digitalWrite(SWr1,HIGH); //SW(row1) is written HIGH
delay(5);
stateSWc1 = digitalRead(SWc1); //I read the corresponding SWITCH GND
if ( (!(stateSWc1 == lastStateSWc1 )) && (stateSWc1 == HIGH)){
lastStateSWc1 = HIGH; //this is my general debounce, seems to work well on other apps
controlChange(CHANNEL1,1,1); //this is a midi message that can be used to debugg
delay(50);
}
The result is that:
when i turn my midi serial device, i constantly receive the midi message. Which could mean that the button is always detected "as pressed" (at least according to how i think they are supposed to react, that is:
switch row X HIGH -> if switch col Y HIGH --> we pressed the button (X,Y)
so i maybe thought they could react the opposite way and change my debounce to the opposite:
if ( (!(stateSWc1 == lastStateSWc1 )) && (stateSWc1 == LOW) -> lastStateSwc1 = LOW
and then else if ( (!(stateSWc1 == lastStateSWc1 )) && (stateSWc1 == LOW) ) -> lastStateSwc1 = HIGH
but stil getting the same result.
it seems i am misunderstanding how the buttons are suppose to work. Could anyone give me some advice, this is the very last step for my application..
thanks
You should post your complete sketch and a scematic (hand drawn will be fine) or some well focussed closeup pics. Also you should put code tags around any long sections of code.
From what you have said so far my guess would be that the input is floating when the switch is not pressed and so just happens to be reading high. Try using input_pullup, set all rows high except the row you want to test (set that row only to low). You will then get a zero when the button is pressed and high when not pressed.
thanks for your help,
this is a drawn schematic of the device with the exact configuration i am using.
i put the digital output pin of the "switch row" at HIGH,
and expect to read the GND corresponding, for the simple example, i'm only reading the 1st column.
Yes, that link is the input_pullup I was referring to, but I did not know that you had an external pull-down resistor until I saw your diagram. I would now say that it should work. Post your full sketch and a picture, and a link to the sparkfun product.
Note that of one row is high and all the others low, if you press more that one key at a time you effectively short an output which is high with an output which is low. This will damage the output pins. You need a diode in each row to stop this from happening.
Alternatively you can use charleyplexing to use only four I/O lines and eight diodes.
Ok first of all, thanks for helping me and giving me advices.
I would rather use a diode so i do not have to rewrite my code to handle the INPUT/OUTPUT variations.
To test the simplest situation i proceeded like this: (see the drawn schematic again)
int stateSWc1 = 0;
int lastStateSWc1 = 0;
/// SWITCHES
void checkSw(){
stateSWc1 = digitalRead(SWc1);
// check if what is read is different from the previous state and if what is read is a LOW state
if ( (!(stateSWc1 == lastStateSWc1 )) && (stateSWc1 == LOW)){
// on a pressé le bouton de la colonne1
lastStateSWc1 = LOW;
controlChange(CHANNEL1,10,1); // CC6-9 //midi serial message
LedOn(1,1); // turn on led 1,1 (works well)
delay(300);
LedOff();
}
//check if what is read is different from the last state but if it is a high, do nothing, but the previous state will now be high
else if ( (!(stateSWc1 == lastStateSWc1 )) && (stateSWc1 == HIGH) ){
lastStateSWc1 = HIGH;
}
}
Still getting nothing, no serial message neither led turned on.
I Tried to invert the debounce but didn't get anything.
Is this exactly how you wanted to proceed?
Concerning the code, this is what i did at first, except that the debounce is inverted: a button is pressed when we read a LOW.
Concerning the hardware, at first i used to put the row i wanted to read @+5V and the other @GND.
I would rather use a diode so i do not have to rewrite my code to handle the INPUT/OUTPUT variations.
Wow that makes a change, normally people will do anything to avoid adding any extra parts.
That circuit is all wrong and I don't think you have the idea straight about what you want / need to do.
The rows should be connected to outputs (via a diode) and the columns should be connected to inputs along with a pull down resistor.
Then the code should put one row to high and all the others low (or to be inputs if you don't want the diodes) and read all the column bits. If any bit is high that indicates that a key is being pressed. You can tell what key it is from what column bit was high and what row was high at the time.