Journal #5 2017-03-09

e000067_featuredjpg_1024x1024

This is a sample code that I have done using the arduino program, essentially it is the program for recognizing the keyswitches as inputs when pushed. This code was done with help[ from rakosi2. While I can not determine the effectiveness of the code right now, it will remain as a base prototype that I can continue to tweak once I finish building they keypad.

#include <Bounce2.h>

//Keyboard report buffer from http://mitchtech.net/arduino-usb-hid-keyboard/
uint8_t buf[8] = { 0 };

//Pins
#define key1 6
#define key2 7
#define key1v 29 //http://www.win.tue.nl/~aeb/linux/kbd/scancodes-14.html
#define key2v 27
#define key3v 28
#define key4v 29

//states
boolean state1 = true;   //if button1 has not been pushed
boolean state2 = true;   //if button2 has not been pushed

//creating bounce objects
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();

void setup()
{
Serial.begin(9600);

pinMode(key1, INPUT_PULLUP);
pinMode(key2, INPUT_PULLUP);

debouncer1.attach(key1);
debouncer2.attach(key2);

//time
debouncer1.interval(3);
debouncer2.interval(3);

}

void loop() {
//check button push state
debouncer1.update();
debouncer2.update();

if (debouncer1.read() == LOW & state1) { // Send keypress
buf[2] = key1v;//starts from array 2
Serial.write(buf, 8);
digitalWrite(led1, HIGH);
state1 = false;
}
if (debouncer1.read() == HIGH & !state1) { // Release key
buf[2] = 0;
Serial.write(buf, 8);
digitalWrite(led1, LOW);
state1 = true;
}

if (debouncer2.read() == LOW & state2) { // Send keypress
buf[3] = key2v;
Serial.write(buf, 8);
digitalWrite(led2, HIGH);
state2 = false;
}
if (debouncer2.read() == HIGH & !state2) { // Release key
buf[3] = 0;
Serial.write(buf, 8);
digitalWrite(led2, LOW);
state2 = true;
}

if (debouncer2.read() == LOW & state2) { // Send keypress
buf[3] = key3v;
Serial.write(buf, 8);
digitalWrite(led2, HIGH);
state2 = false;
}
if (debouncer2.read() == HIGH & !state2) { // Release key
buf[3] = 0;
Serial.write(buf, 8);
digitalWrite(led2, LOW);
state2 = true;
}

if (debouncer2.read() == LOW & state2) { // Send keypress
buf[3] = key4v;
Serial.write(buf, 8);
digitalWrite(led2, HIGH);
state2 = false;
}
if (debouncer2.read() == HIGH & !state2) { // Release key
buf[3] = 0;
Serial.write(buf, 8);
digitalWrite(led2, LOW);
state2 = true;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s