Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more buttons #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
46 changes: 0 additions & 46 deletions KappaPad/CapacitiveKey.h

This file was deleted.

48 changes: 0 additions & 48 deletions KappaPad/KappaPad.ino

This file was deleted.

111 changes: 111 additions & 0 deletions Main/CapacitiveKey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class CapacitiveKey
{
/*
Class attributes
Add more key variables if you are adding more modes
*/
public:
CapacitiveSensor* sensor;
bool keyReleased = true;
char key1;
char key2;
char key3;
unsigned int releaseDelay=20;
unsigned int releaseTimer;
unsigned int treshold;
unsigned int sample;
/*
Constructor of the class
Add more key parameters if you need more modes.
*/
CapacitiveKey(uint8_t sendPin, uint8_t receivePin, unsigned int capacitiveTreshold, char k1,char k2,char k3)
{
sensor = new CapacitiveSensor(sendPin, receivePin);
treshold = capacitiveTreshold;
key1 = k1;
key2 = k2;
key3 = k3;
}

~CapacitiveKey()
{
delete sensor;
}

/* Add more modes accordingly*/
void keyUpdate(int MODE)
{
sample = sensor->capacitiveSensorRaw(1);
if (sample > treshold)
{
if (keyReleased)
{
switch(MODE)
{
case (0):
if(key1!=0) Keyboard.press(key1);
if(RGB_LED_MODE==1) setRgbLedColor(20,0,20);
break;

case (1):
if(key2!=0) Keyboard.press(key2);
if(RGB_LED_MODE==1) setRgbLedColor(0, 1, 0);
break;

case (2):
if(key3!=0) Keyboard.press(key3);
if(RGB_LED_MODE==1) setRgbLedColor(0, 0, 20);
break;

//if you have more modes, add them here.

case (NUMBER_OF_MODES-1):
if(RGB_LED_MODE==1) setRgbLedColor(20, 0, 0);
break;

default:
setRgbLedColor(255,255,255); //You should never get here.
break;
}
keyReleased = false;
}
releaseTimer = releaseDelay;
}
else
{
if (!keyReleased)
{
if (releaseTimer == 0)
{
switch(MODE)
{
case (0):
Keyboard.release(key1);
break;

case (1):
Keyboard.release(key2);
break;

case (2):
Keyboard.release(key3);
break;

//if you have more modes, add them here.

case (NUMBER_OF_MODES-1): /* Not needed to do anything*/
default:
break;

}
if(RGB_LED_MODE==1) setRgbLedColor(0, 0, 0);
keyReleased = true;
}
else
{
releaseTimer--;
}
}
}
}
};
158 changes: 158 additions & 0 deletions Main/Main.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//Dependencies
#include <CapacitiveSensor.h>
#include <Keyboard.h>

/*
In my case, I used an arduino leonardo, so if you need help with the pin numbers and properties,
here's a pinout of it: https://ja-bots.com/wp-content/uploads/2019/06/arduino-leonardo4.png
*/

//Pin definitions
const int STATUS_LED_PIN = 13; /* PWM pin preferably*/
const int SWITCH_PIN = 12;

/* PWM pins preferably aswell*/
extern const int RGB_RED_LED_PIN = 9;
extern const int RGB_GREEN_LED_PIN = 10;
extern const int RGB_BLUE_LED_PIN = 11;

/* If you don't connect the LEDS to PWM pins on the arduino,
you must change the analogWrite functions to digitalWrite
(Ctrl+F in both this file and the CapacitiveKey.h one)
and set the second parameter to LOW or HIGH for OFF and ON respectively */

//Other variables
extern const int NUMBER_OF_MODES = 4; // Defines the number of modes that the keypad will have
extern const int RGB_LED_MODE = 0; // Defines the mode of the RGB LED (only changeable via here, code reupload needed)
int MODE=0; // Control variable that cycles between the modes of the keypad (changeable by the switch)

/* The variables declared with extern are global, used in the CapacitiveKey.h file aswell */

/*
The RGB LED MODES are defined as it follows:
by default in all modes :
The led briefly shines an unique color the moment the mode is changed, as to show which mode we are on
0 - NONREACTIVE > The led only does the default behaviour
1 - REACTIVE > The led briefly shines an unique color the moment a key is pressed
*/

//Functions
extern void setRgbLedColor(int red, int green, int blue){
analogWrite(RGB_RED_LED_PIN, red);
analogWrite(RGB_GREEN_LED_PIN, green);
analogWrite(RGB_BLUE_LED_PIN, blue);
}


//Here we include the file, after we declare all of the global variables (for scoping reasons)
#include "capacitiveKey.h"

//Keys ^

/* CapacitiveKey parameters in order
(SendPin,Receive/SensePin,Threshold,KeyCode1,KeyCode2,KeyCode3) */

/* Refer to this link https://github.com/arduino-libraries/Keyboard/blob/master/src/Keyboard.h for more keyCodes */

CapacitiveKey key1 = CapacitiveKey(22,1 ,20 ,'z' ,'z' ,'z' );
CapacitiveKey key2 = CapacitiveKey(21,2 ,20 ,'x' ,'x' ,'x' );
CapacitiveKey key3 = CapacitiveKey(19,4 ,20 ,KEY_KP_0 ,'c' ,'c' );
CapacitiveKey key4 = CapacitiveKey(7,6 ,20 ,KEY_KP_DOT ,'v' ,'v' );
CapacitiveKey key5 = CapacitiveKey(23,0 ,20 ,KEY_F7 ,KEY_F7 ,'a' );
CapacitiveKey key6 = CapacitiveKey(20,3 ,20 ,KEY_F2 ,KEY_F2 ,'d' );
CapacitiveKey key7 = CapacitiveKey(18,5 ,20 ,KEY_ESC ,KEY_ESC ,KEY_ESC );

/* Uncomment the #define line to enable testing mode
This enables serial monitor output, to check the key readings values
The keypad starts disabled, so no inputs will be sent unless you change the mode again via the switch */

//#define TESTING

//Custom function to detect when 2 keys are pressed at the same time
void keyCollision(int vala, int valb,int r, int g, int b){
if((vala>8) && (valb>8)){
setRgbLedColor(r, g, b);
}else{
setRgbLedColor(0, 0, 0);
}
}

void setup()
{
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(STATUS_LED_PIN, OUTPUT);
pinMode(RGB_RED_LED_PIN, OUTPUT);
pinMode(RGB_BLUE_LED_PIN, OUTPUT);
pinMode(RGB_GREEN_LED_PIN, OUTPUT);
Keyboard.begin();

#ifdef TESTING
Serial.begin(115200);
MODE=NUMBER_OF_MODES-1;
#endif
}

void loop()
{
MODE%=NUMBER_OF_MODES; //Cycles the mode number from 0 to NUMBER_OF_MODES-1.

/* Check if the switch has been pulsated, we use a pullup resistor so we check for LOW */
if (digitalRead(SWITCH_PIN)==LOW)
{
MODE+=1; //Increase the mode by 1
// Defult behaviour of RGB LED
switch(MODE)
{
/* If the LEDS are connected to PWM pins, you can modify their brightness,
changing the parameters from 0 to 255, where 0 is off and 255 is the brightest */

case (1):
setRgbLedColor(0, 1, 0);
break;

case (2):
setRgbLedColor(0, 0, 20);
break;

//if you have more modes, add some more colors here to know when you change to them, or else you will see default color

case (NUMBER_OF_MODES-1):
setRgbLedColor(1, 0, 0);
break;

default:
setRgbLedColor(20,0,20); //You will see this color when you cycle back to state 0,
break;
}
delay(500); //Delay as to only increase the mode by 1
setRgbLedColor(0,0,0);
}

#ifdef TESTING
Serial.print("Mode #");Serial.print(MODE);Serial.print(" - ");
Serial.print(key1.sample);Serial.print(" ");
Serial.print(key2.sample);Serial.print(" ");
Serial.print(key3.sample);Serial.print(" ");
Serial.print(key4.sample);Serial.print(" ");
Serial.print(key5.sample);Serial.print(" ");
Serial.print(key6.sample);Serial.print(" ");
Serial.print(key7.sample);Serial.println();
#endif

// Comment any of the keys if you need to disable them, but leave them instantiated on the Keys ^ part
key1.keyUpdate(MODE);
key3.keyUpdate(MODE);
keyCollision(key1.sample,key3.sample,125,0,0);
key2.keyUpdate(MODE);
key4.keyUpdate(MODE);
keyCollision(key2.sample,key4.sample,0,0,20);

key5.keyUpdate(MODE);
key6.keyUpdate(MODE);
key7.keyUpdate(MODE);


//Control of the Status LED
if(MODE==NUMBER_OF_MODES-1){analogWrite(STATUS_LED_PIN, 15);}
else{analogWrite(STATUS_LED_PIN, 0);}
}
Loading