Last active
April 11, 2017 00:13
-
-
Save houmei/7c42c60a56e6f63c08467aaa3296722b to your computer and use it in GitHub Desktop.
3x4 matrix tenkey -> ADC input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// analog matrix-tenkey | |
// 20170410 | |
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to | |
int sensorValue = 0; // value read from the pot | |
char ankey; | |
void setup() { | |
// initialize serial communications at 9600 bps: | |
Serial.begin(9600); | |
} | |
void loop() { | |
// read the analog in value: | |
sensorValue = analogRead(analogInPin); | |
ankey = an3x4value(sensorValue); | |
// print the results to the serial monitor: | |
if (ankey!='\0') Serial.print(ankey); | |
// Serial.println(sensorValue); | |
// wait 2 milliseconds before the next loop | |
// for the analog-to-digital converter to settle | |
// after the last reading: | |
delay(2); | |
delay(100); | |
} | |
char an3x4value(int an) { // analog value = 1...1023 | |
if (1020<an && an<1024) return '#'; | |
if (942-3<an && an<942+3) return '9'; | |
if (865-3<an && an<865+3) return '6'; | |
if (767-3<an && an<767+3) return '3'; | |
if (561-3<an && an<561+3) return '0'; | |
if (527-3<an && an<527+3) return '8'; | |
if (491-3<an && an<491+3) return '5'; | |
if (445-3<an && an<445+3) return '2'; | |
if (346-3<an && an<346+3) return '*'; | |
if (325-3<an && an<325+3) return '7'; | |
if (304-3<an && an<304+3) return '4'; | |
if (275-3<an && an<275+3) return '1'; | |
return '\0'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment