I've managed to put together a simple, crude program for Arduino that sends my PC keystrokes via five buttons. I've tested it and it works. I also added a very crude control for LEDs to be illuminated corresponding to each press, until another is selected. I'm sure there is a much more effective way to do that and actually haven't yet soldered the LEDs in place so it's untested.
This, if it works would be quite sufficient for my needs, a 5-button footswitch that sends out letters and shows which one was pressed last.
BUT there is one issue. Should I accidentally press the same button again, it messes up the logic at the PC end, instead of selecting a function, receiving the same command (letter) switches all five off. I can't affect the way it works on the PC so I would like to add a logic that repeated keypresses of the SAME button will be ignored, UNTIL another button has at some point been pressed. I've been able to find out how to do that with timers, but there can be no timer after which the button is again accepted, but it needs to be infinite, so even after a week pressing the same button again does absolutely nothing, BUT at the same time it allows me to press any buttons as rapidly as possible and send out the letters.
#include <Keyboard.h> //Sisällytetään näppäimistö-kirjasto
#include <Mouse.h> //Sisällytetään hiiri-kirjasto
#include <Arduino.h> //Sisällytetään arduinon käskyjä LEDeille
int pinA = 2; //Annetaan muuttujanimet pinneille
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
void setup() {
pinMode(pinA, INPUT_PULLUP); //Määritellään sisäiset vastukset
pinMode(pinB, INPUT_PULLUP); //sekä asetetaan kytkintila pinneille
pinMode(pinC, INPUT_PULLUP);
pinMode(pinD, INPUT_PULLUP);
pinMode(pinE, INPUT_PULLUP);
pinMode(7, OUTPUT); // asetetaan LED output pinnit
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
// muistiin: Button 1 = pin 2..jne..Button 5 = pin 6.
// PIn 7 = punainen LED (solo) VALKEA lanka
// PIn 8 = vihreä LED (metal) PUNAINEN lanka
// PIn 10 = sininen LED (clean) Vihreä lanka
// PIn 11 = keltainen LED vasen (rock) valkea lanka
// PIn 12 = keltainen LED oikea (lead) punainen
}
void loop() {
if (digitalRead(pinA) == LOW) //Tsekataan onko Pin 2 kytkintä painettu (vihreäF)
{
Keyboard.write('A'); //Sending the "A" character
digitalWrite(8, LOW); // Laitetaan muut ledit pois päältä
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(7, HIGH); // Laitetaan päälle LED pinnissä 7
delay(500);
}
if (digitalRead(pinB) == LOW) //Tsekataan onko Pin 3 kytkintä painettu (valkea G)
{
Keyboard.write('B'); //Sending the "B" character
digitalWrite(7, LOW); // Laitetaan muut ledit pois päältä
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(8, HIGH); // Laitetaan päälle LED pinnissä 8
delay(500);
}
if (digitalRead(pinC) == LOW) //Checking if the third switch has been pressed
{
Keyboard.write('C'); //Sending the "C" character
digitalWrite(7, LOW); // Laitetaan muut ledit pois päältä
digitalWrite(8, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(10, HIGH); // Laitetaan päälle LED pinnissä 10
delay(500);
}
if (digitalRead(pinD) == LOW) //Checking if the fourth switch has been pressed
{
Keyboard.write('D'); //Sending the "D" character
digitalWrite(7, LOW); // Laitetaan muut ledit pois päältä
digitalWrite(8, LOW);
digitalWrite(10, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH); // Laitetaan päälle LED pinnissä 11
delay(500);
}
if (digitalRead(pinE) == LOW) //Checking if the fifth switch has been pressed
{
Keyboard.write('E'); //Sending the "E" character
digitalWrite(7, LOW); // Laitetaan muut ledit pois päältä
digitalWrite(8, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH); // Laitetaan päälle LED pinnissä 12
delay(500);
}
This structured array contains all information about the pins and a service to debounce the pins and a service to detect state changes.
The results of the status change detection can be further processed to transmit letters as specified.
I did do a few basic courses in C++ waaaaay back in my 20s during the 1990s....haven't coded anything since, forgotten most of it. But that basic idea of how code works has certainly helped me to take some examples and figure out how to create at least something.
The thing is, I don't want to spend way too much time&effort on this, because I don't really know if I will ever need Arduino for anything else besides this project for making this controller. Possibly improved versions of, but that's about it I normally use MIDI and for that there are lots of controllers. This one music project however proved to be next to impossible to program MIDI for, but very easy on the keyboard, so it dawned on me I could probably use Arduino for this.
Very cool that this forum is very helpful indeed!
And oh, an Arduino Micro. Bought that one, because it works directly as a HID device in Windows, and it was indeed easy enough to program the basic commands.
I had looked at the structured array functions, but the logic seemed a little too complex to decipher with various example codes.
Heh, no prob, we got it sorted in no time. Incredible to me that you got it almost perfect just like that with no testing even, and it works perfectly now!
I'd post pics of the whole thing but this seems to not allow pictures easily. Anyway thanks again!