PSU Project #9 – Button and Matrix inputs

Hi All,

sorry for the delay adding this to my website, I’ve been busy with college. I’ve just uploaded part 10 and realized that I didn’t update the webpage for 9, so here it is.

In this episode I will be briefly covering using the button inputs on the Arduino. I briefly go over debouncing and then I introduce using a matrix keypad to allow more buttons to be used.

Arduino code for debounced button input:

#include <LiquidCrystal.h>
#define button_5 6
#define button_4 7
#define button_3 8
#define button_2 9
#define button_1 10
#define hold_limit 15000
int temp=0,tempold=1;
int b1=0,b2=0,b3=0,b4=0,b5=0;
int bs1=0,bs2=0,bs3=0,bs4=0,bs5=0;
int bc1=0,bc2=0,bc3=0,bc4=0,bc5=0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 // Print a message to the LCD.
 lcd.print("hello, world!");
}
void loop() {
 // set the cursor to column 0, line 1
 // (note: line 1 is the second row, since counting begins with 0):
b1 = digitalRead(button_1);
 b2 = digitalRead(button_2);
 b3 = digitalRead(button_3);
 b4 = digitalRead(button_4);
 b5 = digitalRead(button_5);
//////////// Button 1 ///////////////
 if ((b1==1)&&(bs1==0))
 {
 temp = 0; bs1=1; bc1=0;
 }
 if ((b1==1)&&(bs1==1))
 {
 bc1++;
 if (bc1>hold_limit)
 {
 bs1=0; bc1=0;
 }
 }
 if ((b1==0)&&(bs1==1))
 {
 bs1=0;
 }
//////////// Button 2 ///////////////
 if ((b2==1)&&(bs2==0))
 {
 temp--; bs2=1; bc2=0;
 }
 if ((b2==1)&&(bs2==1))
 {
 bc2++;
 if (bc2>hold_limit)
 {
 bs2=0; bc2=0;
 }
 }
 if ((b2==0)&&(bs2==1))
 {
 bs2=0;
 }
//////////// Button 3 ///////////////
 if ((b3==1)&&(bs3==0))
 {
 temp++; bs3=1; bc3=0;
 }
 if ((b3==1)&&(bs3==1))
 {
 bc3++;
 if (bc3>hold_limit)
 {
 bs3=0; bc3=0;
 }
 }
 if ((b3==0)&&(bs3==1))
 {
 bs3=0;
 }
//////////// Button 4 ///////////////
 if ((b4==1)&&(bs4==0))
 {
 temp-=10; bs4=1; bc4=0;
 }
 if ((b4==1)&&(bs4==1))
 {
 bc4++;
 if (bc4>hold_limit)
 {
 bs4=0; bc4=0;
 }
 }
 if ((b4==0)&&(bs4==1))
 {
 bs4=0;
 }
//////////// Button 5 ///////////////
 if ((b5==1)&&(bs5==0))
 {
 temp+=10; bs5=1; bc5=0;
 }
 if ((b5==1)&&(bs5==1))
 {
 bc5++;
 if (bc5>hold_limit)
 {
 bs5=0; bc5=0;
 }
 }
 if ((b5==0)&&(bs5==1))
 {
 bs5=0;
 }
/////////////////////////////////////
 if (temp!=tempold)
 {
 lcd.setCursor(0, 1); lcd.print(" ");
 lcd.setCursor(0, 1); lcd.print(temp);
 tempold=temp;
 }
}

Leave a Reply