Button brick example
August 28, 2010
I just recieved this brick from seeedstudio.com
And here is how you easily can get started using it…
//Define the values for your button brick, values can be found on the LCD after A: #define btn1 176 #define btn2 402 #define btn3 766 #define btn4 950 #define btn5 1022 void setup() { Serial.begin(9600); //Start serial communication for the LCD Serial.print("$CLEAR\r\n"); //Clear LCD } int pBtn(int input) { int tolerance = 10; //This is to make up for changing resistance or voltage, this will allow ex. 176 to be down to 166 or up to 186, and still trigger as button 1 int button = 0; if (input <= btn1 + tolerance && input >= btn1 - tolerance) button = 1; else if (input <= btn2 + tolerance && input >= btn2 - tolerance) button = 2; else if (input <= btn3 + tolerance && input >= btn3 - tolerance) button = 3; else if (input <= btn4 + tolerance && input >= btn4 - tolerance) button = 4; else if (input <= btn5 + tolerance && input >= btn5 - tolerance) button = 5; return button; } void loop() { int btnIn = analogRead(1); Serial.print("$GO 1 1\r\n"); //Go to the top left corner Serial.print("$PRINT B: "); //Print a B: Serial.print(pBtn(btnIn)); //Ask the funciton for what button is pressed, based on the btnIn value from analog 1 Serial.print(" \r\n"); //Make sure no old characters are left Serial.print("$GO 2 1\r\n"); //Go to line two Serial.print("$PRINT A: "); //Print an A: Serial.print(btnIn); //Print the btnIn value Serial.print(" \r\n"); //Make sure no old characters are left }
Another test without the LCD, and just using Serial Monitor
//Define the values for your button brick, values can be found with the serial monitor Button: x (value) #define btn1 176 #define btn2 402 #define btn3 766 #define btn4 950 #define btn5 1022 void setup() { Serial.begin(9600); Serial.print("ADKey module example"); } int pBtn(int input) { int tolerance = 10; //This is to make up for changing resistance or voltage, this will allow ex. 176 to be down to 166 or up to 186, and still trigger as button 1 int button = 0; if (input <= btn1 + tolerance && input >= btn1 - tolerance) button = 1; else if (input <= btn2 + tolerance && input >= btn2 - tolerance) button = 2; else if (input <= btn3 + tolerance && input >= btn3 - tolerance) button = 3; else if (input <= btn4 + tolerance && input >= btn4 - tolerance) button = 4; else if (input <= btn5 + tolerance && input >= btn5 - tolerance) button = 5; return button; } void loop() { int btnIn = analogRead(1); Serial.print("Button: "); Serial.print(pBtn(btnIn)); //Ask the funciton for what button is pressed, based on the btnIn value from analog 1 Serial.print(" ("); Serial.print(btnIn); Serial.println(")"); delay(500); }