Getting pins working

Good morning!

I am building a flight sim and I have switches setup on my Arduino Micro for D2-D13. I saw that there is A0-A5 also but when using the code I had been previously utilizing, one way on the switch seems to work fine, if I switch it the other way it rapidly keeps swapping between on and off in the sim. Should I not be using these pins or is there a way for me to still use these? I'm only a few pins short on the micro for what I need right there so I hoping I can use these so I don’t need an entire other arduino for a few pins. Thanks!

Do you have a code to illustrate it?

It sounds like you may be using the pins with analogRead() rather than digitalRead(). Either way, do you have pullup or pulldown resistors on the inputs to keep them in a known state at all times or are they floating at an unknown voltage, sometimes HIGH, sometimes LOW ?

Consider using INPUT_PULLUP in pinMode() for your input pins and have the switches take the inputs to GND when turned on and detect a LOW state as being on

Please post your code and a schematic of your project. A picture of a hand drawn circuit is good enough

Ty for the quick reply! I will be home in a few hours and post the code.

Code is as follows:

#include <arduino.h>
#include <Keyboard.h>
#include <XPLDirect.h> // include file for the X-plane direct interface
XPLDirect Xinterface(&Serial); // create an instance of it

const int buttonPin2 = 2; // the number of the pushbutton pin
const int buttonPin3 = 3; // the number of the pushbutton pin
const int buttonPin4 = 4; // the number of the pushbutton pin
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;
const int buttonPin8 = 8;
const int buttonPin9 = 9;
const int buttonPin10 = 10;
const int buttonPin11 = 11;
const int buttonPin12 = 12;
const int buttonPin13 = 13;

int buttonState = 0; // variable for reading the pushbutton status

bool shiftqdoneFlag = false;
bool shiftwdoneFlag = false;
bool shiftedoneFlag = false;
bool shiftrdoneFlag = false;
bool shifttdoneFlag = false;
bool shiftydoneFlag = false;
bool shiftudoneFlag = false;
bool shiftidoneFlag = false;
bool shiftodoneFlag = false;
bool shiftpdoneFlag = false;
bool shiftadoneFlag = false;
bool shiftsdoneFlag = false;
bool shiftddoneFlag = false;
bool shiftfdoneFlag = false;
bool shiftgdoneFlag = false;

void setup()
{
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);

Serial.begin(9600);
Keyboard.begin();

// Serial.begin(XPLDIRECT_BAUDRATE); // start serial interface. Baudrate is specified in the header, dont change
// Xinterface.begin("XPLDirect Command Demo"); // needed for initialization. Send a texual identifier of your device as a parameter.
}
void loop()
{
// Xinterface.xloop(); // needs to run every cycle

// BATTERY SWITCH // FLIGHT READY/POWER OFF & EXIT SWITCH
if (digitalRead (2) == 1 & shiftqdoneFlag == false)
{
shiftqdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Q');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftqdoneFlag = true;
}
if (digitalRead (2) == 0 & shiftqdoneFlag == true)
{
shiftqdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Q');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftqdoneFlag = false;
}
//GENERATOR SWITCH
if (digitalRead (3) == 1 & shiftwdoneFlag == false)
{
shiftwdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('W');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftwdoneFlag = true;
}
if (digitalRead (3) == 0 & shiftwdoneFlag == true)
{
shiftwdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('W');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftwdoneFlag = false;
}

// AUX BATT SWITCH
if (digitalRead (4) == 0 & shiftedoneFlag == false)
{
shiftedoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('E');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftedoneFlag = true;
}
if (digitalRead (4) == 1 & shiftedoneFlag == true)
{
shiftedoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('E');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftedoneFlag = false;
}
// GEN RESET BUTTON
if (digitalRead (5) == 0 & shiftrdoneFlag == false)
{
shiftrdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('R');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftrdoneFlag = true;
}
if (digitalRead (5) == 1 & shiftrdoneFlag == true)
{
shiftrdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('R');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftrdoneFlag = false;
}
// STARTER SWITCH MANUAL (DOUBLE MOMENTARY)
if (digitalRead (6) == 0 & shiftydoneFlag == false)
{
shiftydoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Y');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftydoneFlag = true;
}
if (digitalRead (6) == 1 & shiftydoneFlag == true)
{
shiftydoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('Y');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftydoneFlag = false;
}
// STARTER SWITCH AUTO/RESET (DOUBLE MOMENTARY)
if (digitalRead (7) == 0 & shifttdoneFlag == false)
{
shifttdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('T');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shifttdoneFlag = false;
}
// IGNITION SWITCH
if (digitalRead (8) == 1 & shiftudoneFlag == false)
{
shiftudoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('U');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftudoneFlag = true;
}
if (digitalRead (8) == 0 & shiftudoneFlag == true)
{
shiftudoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('U');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftudoneFlag = false;
}
// AUTO FUEL BAL
if (digitalRead (9) == 1 & shiftidoneFlag == false)
{
shiftidoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('I');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftidoneFlag = true;
}
if (digitalRead (9) == 0 & shiftidoneFlag == true)
{
shiftidoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('I');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftidoneFlag = false;
}
// MANUAL FUEL BAL - LEFT (ON OFF ON)
if (digitalRead (10) == 1 & shiftodoneFlag == false)
{
shiftodoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('O');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftodoneFlag = true;
}
if (digitalRead (10) == 0 & shiftodoneFlag == true)
{
shiftodoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('P');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftodoneFlag = false;
}
// MANUAL FUEL BAL - RIGHT (ON OFF ON)
if (digitalRead (11) == 1 & shiftodoneFlag == false)
{
shiftodoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('P');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftpdoneFlag = true;
}
if (digitalRead (11) == 0 & shiftpdoneFlag == true)
{
shiftpdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('O');
delay(100);
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftpdoneFlag = false;
}
// AVIONICS MASTER SWITCH
if (digitalRead (12) == 1 & shiftadoneFlag == false)
{
shiftadoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('A');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftadoneFlag = true;
}
if (digitalRead (12) == 0 & shiftadoneFlag == true)
{
shiftadoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('A');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftadoneFlag = false;
}
// BUS TIE SWITCH
if (digitalRead (13) == 1 & shiftsdoneFlag == false)
{
shiftsdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('S');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftsdoneFlag = true;
}
if (digitalRead (13) == 0 & shiftsdoneFlag == true)
{
shiftsdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('S');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftsdoneFlag = false;
}
// BUS TIE SWITCH
if (digitalRead (A0) == 1 & shiftddoneFlag == false)
{
shiftddoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('D');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftddoneFlag = true;
}
if (digitalRead (A0) == 0 & shiftddoneFlag == true)
{
shiftddoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('D');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftddoneFlag = false;
}
// BOOST PUMP SWITCH
if (digitalRead (A1) == 1 & shiftfdoneFlag == false)
{
shiftfdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('F');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftfdoneFlag = true;
}
if (digitalRead (A1) == 0 & shiftfdoneFlag == true)
{
shiftfdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('F');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftfdoneFlag = false;
}
// PMU SWITCH
if (digitalRead (A2) == 1 & shiftgdoneFlag == false)
{
shiftgdoneFlag = false;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('G');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftgdoneFlag = true;
}
if (digitalRead (A2) == 0 & shiftgdoneFlag == true)
{
shiftgdoneFlag = true;
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('G');
Keyboard.releaseAll();
Keyboard.end();
delay(1);
shiftgdoneFlag = false;
}
}

Bottom line, near the bottom I start trying to use the A pin slots similar to above and it wasn't working. If there are changes in there currently it's because I was trying different things. There are other pieces in the code that I can clean up as well, but didn't want to start doing that until I got everything working first. Picture of how the arduino is currently setup to follow. I don't have them plugged into the A pin slots currently because it was causing a lot of issues so I took it out (as you'll see in the picture)

A couple of comments

The first is about how you have posted your code

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Second is the logic of the code such as here (note the use of code tags and Auto Format in the IDE to make the code easier to read)

if (digitalRead (A2) == 0 & shiftgdoneFlag == true)
{
  shiftgdoneFlag = true;
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('G');
  Keyboard.releaseAll();
  Keyboard.end();
  delay(1);
  shiftgdoneFlag = false;
}

It won't do any harm, but what is the point of setting the shiftgdoneFlag variable to true when it is already true ?

More fundamentally, throughout the code you have used a single & instead of && to do a logical and. A single & performs a bitwise and

In passing I feel bound to point out that there is a much neater way to do what you are doing using arrays rather than basically repeating the same code over and over again just with different parameters

Yeah setting it to true doesn't do anything, I just finally got one of my switches to work so I kept replicating what I had at the time. I'm just learning some of this coding stuff, but didn’t want to go too crazy with cleaning things up until I got all the switches functioning.

The A0-A2 isn't currently plugged in, but when it was it was giving the results I originally described.

Pardon my ignorance, what will the & vs && change? Could that be the issue or?

And I do not have any resistors or anything installed between the switch and the board. The A pin slots thus far are the only ones I've had issues with getting to function correctly

Why aren't you you using INPUT_PULLUP with the analogue pins to keep them in a known state when using them as digital pins ?

The difference between using & and && may not matter in your use case as you are using boolean variables which can only have a value of 0 or 1 (false/true), but you should do it right and use && for logical comparisons

With values other than 0 and 1, which you may use in future sketches, they two provide different results

See & - Arduino Reference and && - Arduino Reference for the differences

I don’t really know, is that what I should be doing? I kinda pieced together a bit of code I saw in other locations and once I got one switch to work used that to make the other switches function. If I should be using something else that is a very real possibility, I just was 3 pins short of completing those panels for the cockpit so started looking into the A pins to see if I could stretch this one arduino out just a bit more to finish this side.

Are you still looking for help? If you don't follow the advice that is posted, it will discourage people from offering it.

Let's look at the evidence

Pins 2 to 13 use INPUT_PULLUP and they work
Pins A0 to A2 have no pinMode() set so default to INPUT and they don't work

What do you think that you should change ?

Thank you very much.... I thought I had setup all the stuff above for using the A0-A5 already. I thought you were saying I should be utilizing a different command not realizing that I hadn't actually setup the code for A0-A5 the same way as the others. Thank you very much, it is functioning correctly now. I also changed all my &'s to &&'s and deleted the first line of code since it was redundant. I appreciate the help. I wasn't trying to be difficult I just honestly didn't understand what you were saying as I'm too new/dumb at this. Thank you very much!

Please edit your post to add code tags.

See the "How to get the best out of the forum" post for instructions.

I am glad that you got it working and hope that you learnt something along the way

BTDTGTTS so don't beat yourself up

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.