Keyboards-MultipleButtons
源自於
https://arduino-info.wikispaces.com/Keyboards-MultipleButtons
5-Button Keyboard: Uses series resistors and switches to decide a variety of voltages to send to an Arduino Analog Input.
You can use this same technique for multiple buttons you mount in other ways or orientations.
Diagram (right):
You can use this same technique for multiple buttons you mount in other ways or orientations.
Diagram (right):
Below: Example code of a 5-button keyboard that uses resistors to connect to a single Analog Input Pin:
5-button keyboard connected to Analog Input 0
terry@yourduino.com */
/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
#define keyboard_AnalogInput 0
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnENTER 4
#define btnNONE 5
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
int adc_key_in = 0;
int buttonPressed;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
Serial.println("Test 5-button keyboard");
} //--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
buttonPressed = read_keyboard();
if (buttonPressed != btnNONE)
{
Serial.print("OK - That looks like you pressed ");
Serial.println(buttonPressed, DEC);
}
delay(500);
} //--(end main loop )---
/*-----( Declare User-written Functions )-----*/
// read the buttons
int read_keyboard()
{
adc_key_in = analogRead(keyboard_AnalogInput);
// read the value from the sensor
// buttons when read are centered at these values: 0, 144, 329, 504, 741
// add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnENTER;
return btnNONE; // when all others fail, return this...
}
//*********( THE END )***********
沒有留言:
張貼留言