complete Arduino noob here.
I am on ubuntu 22.10 and using arduino ide 2.2.1
I trying to create a button box for elite dangerous and wanted to use on-off-on switches and many other types.
Is the button matrix i have seen the only way to make a button box work?
My btnbx will have seperate led's for each switch/button to indicate status, but i was also wanting to use firmata or pyfirmata so the game could give feedback to indicate the status of those functions.
for example, in ED the landing gear. if i flick the switch to lower the landing gear, i wanted the LED for that switch to only light up once the landing gear sequence is finished.
Similar to this
So that comes back to my question. Is a button matrix the only way to make a button box work?
One reason for using a matrix of keys is to reduce the number of pins used. For instance a 5 x 5 matrix of 25 keys only needs 10 pins
However, as you are using a Mega2560, you should have plenty of pins to allow you to connect switches to them individually. How many switches do you envisage using ?
You mention controlling LEDs. If you use individual LEDs then each will require their own dedicated pin on the Mega but you could consider using addressable LEDS which would allow you to control dozens of LEDs from a single pin
The task can easily be realised with an object.
A structured array contains all information, such as pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?
Thanks for replying UKHeliBob.
I am looking at using 4 on-off SPST switches, 20 Mom korry buttons, 1x 3 axis joystick, and 2 on-off-on switches.
My design will have Seperate leds for each and most will have a second led for firmata to control from feedback from the game.
I guess the all the leds can use a common negative connection, and the positive legs connected to the switches, as for the firmata leds they will have a common leg and i will only need pins for the positive, Maybe use a mux for them.
At the moment i can only get ubuntu to see the arduino as a joystick using UnoJoy.
Not sure if there is another option for linux.
Also every button box i have seen is using windows. which makes finding stuff for linux a little difficult.
If any of the LEDs are to be controlled only by the associated switch being open of closed then, of course, they do not need to be conncted to their own pin.
How are you going to deal with the situation whereby there is a conflict between the physical state of an SPST switch and the state of the control in the game ?
Have you considered using simple pushbuttons throughout, with the ones that require the action to be latched in a state, ie the SPST switches, being implemented in software as push once to turn on the associated output and push a second time to turn it off. The current state would be indicated by an LED controlled by the Mega rather than the actual switch and could, therefore, be used to indicate the current state whether it was set by pressing the button or from an action in the game
The feedback from firmata will only control the second led to let me know the state of that function.
The will need to dig deeper into firmata, but i don't think the switch function will conflict with firmata as they are essentially on separate circuits.
Firmata (from what i understand) will only give notification feedback from the status.json file from ED. so if i flick a switch then the game should update and firmata will then change the led.
I know firmata can do a lot more, but i just need it to give feedback on the status.
I think of it like the landing gear on a plane, The pilot toggle the landing gear to active and a sec or two later the 3 little lights turn on when they are locked in place.
I did think about using tactile buttons and I understand that buttons would be easier to code for, and they can simulate the toggle function, but i wanted the tactile feel of an actual toggle switch.
So if the button matrix is only for reducing the number of pin connections needed, How do you get a statement like this (below), to react on your PC as a button click?
I am only in the testing phase to understand what i need in the code so when i map the toggle it responds correctly.
Firstly, in my code (listed below) I have some println lines to verify that the switch is behaving correctly, ie. not bouncing.
/*
This script is for testing a On-OFF-ON Toggle Switch.
*/
int switchPin1=2; //One side of the switch
int switchPin2=4; //Other side of the switch
int LED1=5; //Led connected to switchpin 1
int LED2=6; //Led connected to switchpin 2
int Button=8;
int switchState1 =0; //state for switchpin 1
int switchState2 =0; //state for switchpin 2
int BtnState=0;
int lastSwitchState1 = 0; //State for switchpin 1
int lastSwitchState2 = 0; //State for switchpin 2
int lastBtnState=0;
void setup()
{
pinMode(switchPin1,INPUT_PULLUP); //Switchpin1 using pullup so High is off
pinMode(switchPin2,INPUT_PULLUP); //Switchpin2 using pullup so High is off
pinMode(LED1,OUTPUT); //Set LED1
pinMode(LED2,OUTPUT); //Set LED1
Serial.begin(9600); //Open Serial
}
void loop() {
//delay(200);
switchState1=digitalRead(switchPin1); //Read SwitchPin1
switchState2=digitalRead(switchPin2); //Read SwitchPin2
BtnState=digitalRead(Button);
if (switchState1 != lastSwitchState1 ) //Check SwitchState
{
delay(100);
lastSwitchState1 = switchState1; //Update LastSwitchState
if (switchState1 == LOW ) //Check SwitchState1. Low is Closed circuit
{
Serial.println("A ON");
digitalWrite(LED1,HIGH); //Acivate LED1
}
if (switchState1 == HIGH ) //Check switch 1 state
{Serial.println("A OFF");
digitalWrite(LED1,LOW); //Deacivate LED1
}
}
if (switchState2 != lastSwitchState2 ) //Check SwitchState
{
delay(100);
lastSwitchState2 = switchState2;
if (switchState2 == LOW ) //Check SwitchState2. Low is Closed circuit
{
Serial.println("B ON");
digitalWrite(LED2,HIGH); //Acivate LED2
}
if (switchState2 == HIGH ) //Check switch 2 state
{Serial.println("B OFF");
digitalWrite(LED2,LOW); //Deacivate LED2
}
}
//Serial.println(switchState);
}
As the println are not needed ingame they will be removed.
And when i tested this the system mapping didn't react at all, like no key was pressed.
When the println are present, i get a bunch of commands like i mashed the keys at the same time.