ian-arduino/4button-keyboard.ino

440 lines
8.6 KiB
Arduino
Raw Normal View History

2016-06-03 16:11:10 -04:00
/*THE WORKING VERSION OF THE 4 BUTTON KEYBOARD
LAYOUT
RedWire->|
|
____________
| L1 L2 |
| |
| |
| L3 L4 |
| |
------------
| |
| |
----
L1 = buttonState3, b4, v1, var + 5
L2 = buttonState1, b2, v3, var - 1
L3 = buttonState, b1, v2, var + 1
L3 = buttonState2, b3, v4, var + 10
*/
//Ints for counting multipress
int v1 = 0;
int v2 = 0;
int v3 = 0;
int v4 = 0;
//ints for button pins (only b1 - 4 are in use)
const int b1 = 2;
const int b2 = 3;
const int b3 = 4;
const int b4 = 5;
const int b5 = 6;
const int b6 = 7;
//int for counting number and submit
int var = 0;
int subvar = 0;
//Not sure what this does, but don't want to delete it
int ptrue = 0;
//ints for counting button presses
int buttonState = 0;
int lastButtonState = 0;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int buttonState3 = 0;
int lastButtonState3 = 0;
//ints for debouncing each button
long debounceDelay = 50;
long lastDebounceTime = 0;
int state = 0;
long lastDebounceTime1 = 0;
int state1 = 0;
long lastDebounceTime2 = 0;
int state2 = 0;
long lastDebounceTime3 = 0;
int state3 = 0;
/* the pins are set up as inputs and then set high,
so that when the button is grounded it will notice it
and count it as a button press. this way, there are
no resistors needed for the curcuit.
The button should be connected from the pin, to the
button, to ground, like this:
PIN# --> Pin1 of button |--| -- Ground
^
Button |
*/
void setup() {
Serial.begin(9600);
Serial.println("Begin Typing");
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
pinMode(b4, INPUT);
pinMode(b5, INPUT);
pinMode(b6, INPUT);
digitalWrite(b1, HIGH);
digitalWrite(b2, HIGH);
digitalWrite(b3, HIGH);
digitalWrite(b4, HIGH);
digitalWrite(b5, HIGH);
digitalWrite(b6, HIGH);
}
void loop() {
state3 = digitalRead(b4); //read if grounded or not
if ((millis() - lastDebounceTime3) > debounceDelay) { //if the button has not been pressed within debouncetime, do...
buttonState3 = digitalRead(b4);
if (buttonState3 != lastButtonState3) { //if the button is different from before (it was pessed) do...
if (buttonState3 == HIGH) { //if the switch was released, do...
lastDebounceTime3 = millis();
v1 = 0; //reset the multipress counter, so it does not count it as a press after you release the button
}
else { //if switch pressed, do...
var = var + 5; //setting for what the button does
lastDebounceTime3 = millis(); //reset the last debounce
v1 = 1; //set multipress variable to one, or, pressed
}
}
}
//the same goes for this
state = digitalRead(b1);
if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = digitalRead(b1);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
lastDebounceTime = millis();
v2 = 0;
}
else {
var = var + 1;
lastDebounceTime = millis();
v2 = 1;
}
}
}
//and this
state1 = digitalRead(b2);
if ((millis() - lastDebounceTime1) > debounceDelay) {
buttonState1 = digitalRead(b2);
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
lastDebounceTime1 = millis();
v3 = 0;
}
else {
var = var - 1;
lastDebounceTime1 = millis();
v3 = 1;
}
}
}
//... and this
state2 = digitalRead(b3);
if ((millis() - lastDebounceTime2) > debounceDelay) {
buttonState2 = digitalRead(b3);
if (buttonState2 != lastButtonState2) {
if (buttonState2 == HIGH) {
lastDebounceTime2 = millis();
v4 = 0;
}
else {
var = var + 10;
lastDebounceTime2 = millis();
v4 = 1;
}
}
}
// this whole section controls what letters correspond to what numbers,
// and when to send the letter out
if (var == 1) { //if the variable is 1,
if (subvar == 1) { //and, if you pressed the submit button,
Serial.print('A'); //print the letter 'A' (A is the first letter in the alphabet)
subvar = 0; //reset the submit button so it doesnt happen forever
var = 0; //reset the variable, so you can count to the next word.
}
}
//the same goes for all the code below, untill the next comment.
if (var == 2) {
if (subvar == 1) {
Serial.print('B');
subvar = 0;
var = 0;
}
}
if (var == 3) {
if (subvar == 1) {
Serial.print('C');
subvar = 0;
var = 0;
}
}
if (var == 4) {
if (subvar == 1) {
Serial.print('D');
subvar = 0;
var = 0;
}
}
if (var == 5) {
if (subvar == 1) {
Serial.print('E');
subvar = 0;
var = 0;
}
}
if (var == 6) {
if (subvar == 1) {
Serial.print('F');
subvar = 0;
var = 0;
}
}
if (var == 7) {
if (subvar == 1) {
Serial.print('G');
subvar = 0;
var = 0;
}
}
if (var == 8) {
if (subvar == 1) {
Serial.print('H');
subvar = 0;
var = 0;
}
}
if (var == 9) {
if (subvar == 1) {
Serial.print('I');
subvar = 0;
var = 0;
}
}
if (var == 10) {
if (subvar == 1) {
Serial.print('J');
subvar = 0;
var = 0;
}
}
if (var == 11) {
if (subvar == 1) {
Serial.print('K');
subvar = 0;
var = 0;
}
}
if (var == 12) {
if (subvar == 1) {
Serial.print('L');
subvar = 0;
var = 0;
}
}
if (var == 13) {
if (subvar == 1) {
Serial.print('M');
subvar = 0;
var = 0;
}
}
if (var == 14) {
if (subvar == 1) {
Serial.print('N');
subvar = 0;
var = 0;
}
}
if (var == 15) {
if (subvar == 1) {
Serial.print('O');
subvar = 0;
var = 0;
}
}
if (var == 16) {
if (subvar == 1) {
Serial.print('P');
subvar = 0;
var = 0;
}
}
if (var == 17) {
if (subvar == 1) {
Serial.print('Q');
subvar = 0;
var = 0;
}
}
if (var == 18) {
if (subvar == 1) {
Serial.print('R');
subvar = 0;
var = 0;
}
}
if (var == 19) {
if (subvar == 1) {
Serial.print('S');
subvar = 0;
var = 0;
}
}
if (var == 20) {
if (subvar == 1) {
Serial.print('T');
subvar = 0;
var = 0;
}
}
if (var == 21) {
if (subvar == 1) {
Serial.print('U');
subvar = 0;
var = 0;
}
}
if (var == 22) {
if (subvar == 1) {
Serial.print('V');
subvar = 0;
var = 0;
}
}
if (var == 23) {
if (subvar == 1) {
Serial.print('W');
subvar = 0;
var = 0;
}
}
if (var == 24) {
if (subvar == 1) {
Serial.print('X');
subvar = 0;
var = 0;
}
}
if (var == 25) {
if (subvar == 1) {
Serial.print('Y');
subvar = 0;
var = 0;
}
}
if (var == 26) {
if (subvar == 1) {
Serial.print('Z');
subvar = 0;
var = 0;
}
}
// i know everything above could've been done with a map, but, i didn't do it like that.
//this next code part control all the multipress.
if (v1 == 1) { //if one of the two buttons in the multipress section was pressed,
if (v3 == 1) { //and the second button in the multipress section was pressed,
subvar = 1; //do what you want that multipres section to do. In this case, it was
//presss the top two buttons to submit the number.
v1 = 0; //reset multipress button stats
v3 = 0; //ya, again
var = var - 5; //take into account what would normall happen to the variable.
//the top two button can add + 5 and - 1, so doing - 5 and + 1 will reset the
//variable to what it would normally be.
var = var + 1; //the same here too
}
}
//the same goes for the below code, but with different buttons for
//multipress, and different actions based on that
if (v2 == 1) {
if (v4 == 1) {
Serial.print(" ");
v2 = 0;
v4 = 0;
var = var + 1;
var = - 5;
}
}
if (v3 == 1) {
if (v4 == 1) {
Serial.println("");
v3 = 0;
v4 = 0;
var = var + 1;
var = var - 10;
}
}
if (v1 == 1) {
if (v2 == 1) {
Serial.print(".");
v1 = 0;
v2 = 0;
var = var - 1;
var = var - 5;
}
}
//reset the button states. not sure if i still need these, but dont want to risk deleting them.
//
lastButtonState = buttonState;
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
}