154 lines
3.6 KiB
C++
154 lines
3.6 KiB
C++
//Recommend reading: https://www.arduino.cc/reference/en/
|
|
|
|
//Buttons go 1-4 from Top to bottom
|
|
|
|
//This project uses an Arduino Pro Micro (also known as an Arduino Leonardo)
|
|
|
|
//Includes
|
|
//These can be installed in the arduino IDE by clicking on tools in the top bar, then manage libraries.
|
|
#include <EasyButton.h> //Used to define a button and debounces it
|
|
#include "HID-Project.h" //Lets us simulate keyboard presses.
|
|
|
|
|
|
//Pin Definitions. These numbers refer to the physical GPIO pins on the board
|
|
//LEDS
|
|
#define LED1 3
|
|
#define LED2 5
|
|
#define LED3 6
|
|
#define LED4 9
|
|
|
|
//Buttons
|
|
#define BUTTON1 A3
|
|
#define BUTTON2 A2
|
|
#define BUTTON3 A1
|
|
#define BUTTON4 A0
|
|
|
|
unsigned long ledTickTime = 0;
|
|
unsigned long lastPressTime = 0;
|
|
uint8_t ledBrightness = 100;
|
|
bool ledOn = false;
|
|
|
|
//Using the EasyButton library we create 4 buttons, with each button being connected to a GPIO pin as defined above, and having a 35ms debounce time. From memory, these do not have internal pullup resistors, and are not inverted. Hence the two falses.
|
|
EasyButton button1(BUTTON1, 35, false, false);
|
|
EasyButton button2(BUTTON2, 35, false, false);
|
|
EasyButton button3(BUTTON3, 35, false, false);
|
|
EasyButton button4(BUTTON4, 35, false, false);
|
|
|
|
//Standard Arduino setup function. Code that only needs to run once goes here
|
|
void setup() {
|
|
Keyboard.begin();
|
|
|
|
//LEDS - Setting the LED pins to be outputs. Normally, the buttons would be set as inputs, but they are handled by that library.
|
|
pinMode(LED1, OUTPUT);
|
|
pinMode(LED2, OUTPUT);
|
|
pinMode(LED3, OUTPUT);
|
|
pinMode(LED4, OUTPUT);
|
|
|
|
//Here we output a PWM signal to the LEDs, where 0 will be off, and 255 is brightest
|
|
analogWrite(LED1, ledBrightness);
|
|
analogWrite(LED2, ledBrightness);
|
|
analogWrite(LED3, ledBrightness);
|
|
analogWrite(LED4, ledBrightness);
|
|
|
|
//BUTTONS - setup buttons using library.
|
|
button1.begin();
|
|
button2.begin();
|
|
button3.begin();
|
|
button4.begin();
|
|
}
|
|
|
|
|
|
//Code that is run constantly goes here
|
|
void loop() {
|
|
|
|
//Read all the buttons
|
|
button1.read();
|
|
button2.read();
|
|
button3.read();
|
|
button4.read();
|
|
|
|
|
|
//This should be pretty self explanitory. Make sure to always release a button.
|
|
if(button1.isPressed())
|
|
{
|
|
Keyboard.press(KEY_LEFT_ALT);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
else if(button1.wasReleased())
|
|
{
|
|
Keyboard.release(KEY_LEFT_ALT);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
if(button2.isPressed())
|
|
{
|
|
Keyboard.press(KEY_LEFT_CTRL);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
else if(button2.wasReleased())
|
|
{
|
|
Keyboard.release(KEY_LEFT_CTRL);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
if(button3.isPressed())
|
|
{
|
|
Keyboard.press(KEY_LEFT_SHIFT);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
else if(button3.wasReleased())
|
|
{
|
|
Keyboard.release(KEY_LEFT_SHIFT);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
if(button4.isPressed())
|
|
{
|
|
Keyboard.press(KEY_SPACE);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
else if(button4.wasReleased())
|
|
{
|
|
Keyboard.release(KEY_SPACE);
|
|
lastPressTime = millis();
|
|
}
|
|
|
|
if((millis() - lastPressTime) > 10000)
|
|
{
|
|
ledOn = false;
|
|
}
|
|
else
|
|
{
|
|
ledOn = true;
|
|
}
|
|
|
|
if ((millis() - ledTickTime) > 10)
|
|
{
|
|
ledTickTime = millis();
|
|
if(!ledOn && (ledBrightness > 0))
|
|
{
|
|
ledBrightness=ledBrightness-1;
|
|
}
|
|
|
|
else if(ledOn && (ledBrightness < 100))
|
|
{
|
|
ledBrightness=ledBrightness+10;
|
|
}
|
|
|
|
analogWrite(LED1, ledBrightness);
|
|
analogWrite(LED2, ledBrightness);
|
|
analogWrite(LED3, ledBrightness);
|
|
analogWrite(LED4, ledBrightness);
|
|
}
|
|
|
|
// Serial.print("Led Brightness = ");
|
|
// Serial.print(ledBrightness);
|
|
// Serial.print("\t Led State = ");
|
|
// Serial.println(ledOn);
|
|
|
|
}
|