Im trying to make a custom keypad that acts like a keyboard but im having trouble with the code. ive only ever used python and this arduino stuff is fairly complicated to me.
wondering if anyone could me fix my code
#include <HID.h>
#include <Keyboard.h>
int pin_count = 6;
int pin_nums[] = {
2,3,4,5,9,10
};
int button_state = 0;
int previous_button_state = HIGH;
long last_debounce_time = 0;
const long debounce_delay = 50;
void setup()
{
//
Keyboard.begin();
for (int thispin = 0; thispin < pin_count; thispin++){
pinMode(pin_nums[thispin],INPUT_PULLUP);
digitalWrite(pin_nums[thispin], HIGH);
}
}
void loop()
{
for (int thispin = 0; thispin < pin_count; thispin++)
{
button_state = digitalRead(thispin);
if ((button_state != previous_button_state) && (button_state == HIGH))
{
if (pin_nums[thispin] == 2)
{
Keyboard.release('s');
}
if (pin_nums[thispin] == 3)
{
Keyboard.release('d');
}
if (pin_nums[thispin] == 10)
{
Keyboard.release(';');
}
if (pin_nums[thispin] == 5)
{
Keyboard.release('\'');
}
}
if ((button_state != previous_button_state) && (button_state == LOW))
{
if (pin_nums[thispin] = 2){
if ((millis() - last_debounce_time) > debounce_delay)
{
Keyboard.press('s');
delay(50);
last_debounce_time = millis();
}
}
if (pin_nums[thispin] == 3){
if ((millis() - last_debounce_time) > debounce_delay)
{
Keyboard.press('d');
delay(50);
last_debounce_time = millis();
}
}
if (pin_nums[thispin] == 10){
if ((millis() - last_debounce_time) > debounce_delay)
{
Keyboard.press(';');
delay(50);
last_debounce_time = millis();
}
}
if (pin_nums[thispin] == 5){
if ((millis() - last_debounce_time) > debounce_delay)
{
Keyboard.press('\'');
delay(50);
last_debounce_time = millis();
}
}
}
}
previous_button_state = button_state;
}
The aim is to have a series of switches which can either be pressed for single input or hold the key down for a continuous input, like a keyboard..This is the best i could come up with but the problem is that all this does is get stuck outputting "s" at full speed...What am i missing here? any advice would be massively appreciated
thanks in advanced, Yuxxxi