Hi, I’m trying to create control box for Microsoft Flight Simulator 2020 for Xbox. I want to use Arduino micro as it allows to be keyboard emulator - Xbox requires ascii character. I will be using push buttons, toggle switches and LEDs. However I got a bit confused with something I want to program. I want to program push switch that sends signal to Xbox to change something in simulator - keyboard.print(‘a’) and turn LED on. Then I want to press that button again to turn previous function off and LED off. This can be easily achieved with toggle button but is it somehow possible with push button ? Again it would’ve been easy for me if there wan no LED involved but LED is needed as sn indicator on my panel. Hopefully someone will make some sense out of it. Thanks in advance.
So you want to send an 'a' and light a led when a button is pushed; and when the button is pushed again, you want to send the 'a' again and switch the led off?
You can have a look at the state change detection example that shows how to do something when a button becomes pushed.
You can read back the state of the led pin with digitalRead, invert it and set the the led pin with the resulting value.
Hello zebik
Post your sketch, well formated, with comments and in so called code tags "</>" and a none-Fritzing schematic to see how we can help.
Have a nice day and enjoy programming in C++ and learning.
By "push button" do you mean a momentary button? Look at the following tutorial:
StateChangeDetection
Here is a Non-blocking demo sketch that toggles the state of the built in LED and of a bool variable using the state change detection method linked by @ToddL1962.
// by C Goulding aka groundFungus
// attach push button switch to ground and digital input
const byte buttonPin = 2; // the pin to which the pushbutton is attached
const byte ledPin = 13; // the pin to which the LED is attached
bool buttonState = 0; // current state of the button
bool lastButtonState = 0; // previous state of the button
bool mode = false;
void setup()
{
// initialize the button pin as a input with internal pullup enabled
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(115200);
Serial.println("Select mode with push button");
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 50; // check switch 20 times per second
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the new buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// if the current state is LOW then the button
// went from off to on:
digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output (LED)
mode = !mode; // toggle state of variable
if (mode == true)
{
Serial.println("Manual mode\n");
}
else
{
Serial.println("Scan mode\n");
}
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}
Thank you for pointing me in the right direction. That would definitely work. One more question, if I have few buttons that would use the same principle can I put so many IF instructions in the main loop ? I know it could sound trivial but I’m pretty new to arduino programming.
Thank you
Thanks
You can nest as many ifs as you want but you may wish to create a switch case control, classes or just functions for button polling and actions. If you find yourself copying and pasting the same code many times with only minor variations then there is a better way to do it
Thank you
Hello zebik
I´ve found a similar solution in my sketch box.
This sketch uses a data structure containing all data neccessary.
BUTTON2XBOX button2xboxes[] {
{'a', ButtonPins[One], false, 20, 0},
{'b', ButtonPins[Two], false, 20, 0},
{'c', ButtonPins[Three], false, 20, 0},
{'d', ButtonPins[Four], false, 20, 0},
};
You can make changes to your project simply.
A service takes care and process these datas.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
Many thanks to LarryD
https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
https://forum.arduino.cc/t/push-button-and-led-programming/1013929/2
Tested with Arduino: Mega[x] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Push button and LED programming"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte ButtonPins[] {A0,A1,A2,A3}; // portPin o---|button|---GND
// CONSTANT DEFINITION
enum InputOutput {One, Two, Three, Four};
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
// -- objects -----------------------------------------
struct TIMER { // has the following members
unsigned long duration; // memory for interval time
unsigned long stamp; // memory for actual time
};
struct BUTTON { // has the following members
byte pin; // port pin
int statusOld; // current state
TIMER scan; // see timer struct
};
struct BUTTON2XBOX {
char ascii;
BUTTON pushButton;
};
BUTTON2XBOX button2xboxes[] {
{'a', ButtonPins[One], false, 20, 0},
{'b', ButtonPins[Two], false, 20, 0},
{'c', ButtonPins[Three], false, 20, 0},
{'d', ButtonPins[Four], false, 20, 0},
};
// -------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
// https://www.learncpp.com/cpp-tutorial/for-each-loops/
for (auto ButtonPin : ButtonPins) pinMode(ButtonPin, INPUT_PULLUP);
}
void loop () {
currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
for (auto &button2xbox : button2xboxes) {
if (currentTime - button2xbox.pushButton.scan.stamp >= button2xbox.pushButton.scan.duration) {
button2xbox.pushButton.scan.stamp = currentTime;
int statusNew = !digitalRead(button2xbox.pushButton.pin);
if (button2xbox.pushButton.statusOld != statusNew) {
button2xbox.pushButton.statusOld = statusNew;
if (statusNew) Serial.print(button2xbox.ascii);
}
}
}
}
Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.
Thanks a lot. Looks complex for me but will try to work my way through. Thank you again.
Here is an example using arrays to toggle 4 LED's states using 4 push button switches. Uses the same state change detection method. Can easily be expanded to more button/LED pairs.
// by C Goulding aka groundFungus
const byte buttonPins[] = {2, 3, 4, 5};
const size_t NUM_BUTTONS = sizeof(buttonPins);
const byte ledPins[NUM_BUTTONS] = {8, 9, 10, 11};
bool buttonStates[NUM_BUTTONS]; // current state of the button
bool lastButtonStates[NUM_BUTTONS]; // previous state of the button
void setup()
{
// initialize the button pins as inputs with internal pullups enabled
for (byte n = 0; n < NUM_BUTTONS; n++)
{
pinMode(buttonPins[n], INPUT_PULLUP);
}
// initialize the LEDs as an outputs
for (byte n = 0; n < NUM_BUTTONS; n++)
{
pinMode(ledPins[n], OUTPUT);
}
// initialize serial communication:
Serial.begin(115200);
Serial.println("Toggle LED states with push buttons");
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 50; // check switch 20 times per second
if (millis() - timer >= interval)
{
timer = millis();
// read the pushbutton inputs:
for (byte n = 0; n < NUM_BUTTONS; n++)
{
buttonStates[n] = digitalRead(buttonPins[n]);
// compare the new buttonState to its previous state
if (buttonStates[n] != lastButtonStates[n])
{
if (buttonStates[n] == LOW)
{
// if the current state is LOW then the button
// went from off to on:
digitalWrite(ledPins[n], !digitalRead(ledPins[n])); // toggle the output
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonStates[n] = buttonStates[n];
}
}
}
Thank you.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.