Push Button Intermittent

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Message
Author
bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Push Button Intermittent

#1 Post by bdcurry »

I'm using an Arduino Mega 2560 and trying to get a single push button to operate consistently. I have the Arduino connected with several LED's, HD44780 displays, can read/write datarefs -- everything works great except for a single pushbutton (the only one I've tried connecting). Sometimes it works, sometimes it doesn't, sometimes it works for a while, then doesn't and then starts working again -- very inconsistent. I've tried restarting the code several times, changing pins, shutting down everything and rebooting multiple times -- same result. I've researched through the forum but haven't found any info that would lead me to a solution. It seems pretty straight forward -- one side of the PB to a pin (I've used 2, 43, 42, 53, 51, 47,.....) and the other to ground. I'm assuming the internal Arduino pull up resistors are active but if not, tried an external pull up resistor (pin tied to 10k resistor to +5V), no luck. Any pointers appreciated!

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Push Button Intermittent

#2 Post by Sling »

What about changing the button for a known good one or at least a different one.

How are you determining the button is not working? If it’s expecting something to happen in the sim then it could be a Plugin communication issue. You can rule out the hardware by using a print in your code to indicate if the button press is working. If it’s reliable then it’s almost certainly a comms issue. In this case you should run through the troubleshooting steps listed on the wiki.

It would also be useful to see your code so that can be ruled out.

bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Re: Push Button Intermittent

#3 Post by bdcurry »

Thanks for the reply. I replaced the button (switch) with a piece of solid wire, figured that was the most direct check of the button. The button works, sometimes not -- it's intermittent. The rest of the code and devices work fine (led's, displays, reading, writing datarefs). I use the print statements to check that the value is what it should be (when it works) and the "hardware" tab actuation of the button works well so that tells me that it's somewhere in the hardware. If it was a code issue, I'd assume that it wouldn't work at all. I've read and reread the wiki but I'll take another pass through that. What's confusing is that it's intermittent, it does work, just not all the time. Thanks for taking the time to review this, appreciated.

Here's the code for the button (minus the dataref subscribe):
(apologies, not sure the best way to post code, new to the forum)

Code: Select all

global_oat = 0
function new_data19(knob_oat)
    oat = (knob_oat)
    print("oat")
    print(oat)
    global_oat = (oat)
end

function button_pressed()
    print ("timer button pressed")
    --print("test")
    print(global_oat)
    checkoat = (global_oat +1) % 3

    xpl_dataref_write ("laminar/c172/knob_OAT", "INT", checkoat)
end

hw_button_add("ARDUINO_MEGA2560_<A>_D42", button_pressed)

bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Re: Push Button Intermittent

#4 Post by bdcurry »

A couple of additional observations....to your comment about coms, checked port settings, re-flashed the Arduino. It worked for a bit and then stopped, consistent with the previous comments. I stripped everything from the board except a couple of LED's (that work consistently). I noticed that the transmit light on the board was blinking when operating the push button switch while it was operating and again when it wasn't (the sim no longer responded to the button but did work with the "push" on the hardware tab). I hit the "play" to restart and noticed the transmit light on the Arduino no longer flashed. I'm not that experienced at board level stuff and communications so I'm a bit out of my depths. Seems like a single push button hookup should be pretty straight forward and easy task but there's obviously something I'm missing between the Arduino and Air Manager.

User avatar
Ralph
Posts: 7924
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Push Button Intermittent

#5 Post by Ralph »

Code: Select all

function new_data19(knob_oat)
    oat = (knob_oat)
    print("oat")
    print(oat)
    global_oat = (oat)
end
I'm very curious about what this should do :lol: I would remove it if I were you, unless you had plans with it.

But back to the part that should do the magic.

Code: Select all

function button_pressed()
    print ("timer button pressed")
    --print("test")
    print(global_oat)
    checkoat = (global_oat +1) % 3

    xpl_dataref_write ("laminar/c172/knob_OAT", "INT", checkoat)
end
Why are you using checkoat? This variable isn't declared anywhere, so you'll get a nil the first time and it will always remain at 1. Why not use global_oat = (global_oat +1) % 3 ?
Then on to defining your pin number, you should not use < >, so it will not work like this. The correct way would be:

Code: Select all

hw_button_add("ARDUINO_MEGA2560_A_D42", button_pressed)

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Push Button Intermittent

#6 Post by Sling »

Ralph wrote: Tue Feb 16, 2021 7:08 am

Code: Select all

function new_data19(knob_oat)
    oat = (knob_oat)
    print("oat")
    print(oat)
    global_oat = (oat)
end
I'm very curious about what this should do :lol: I would remove it if I were you, unless you had plans with it.
Ralph,
It’s rather long winded but I think it’s a subscribe callback for the oat/volts button dataref. The subscribe just wasn’t posted.

bdcurry,
I’d suggest you do as Ralph suggested and report back on how that goes.

bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Re: Push Button Intermittent

#7 Post by bdcurry »

Correct, it’s a subscribe callback for the oat/volts button dataref. I'm a novice at coding (and even that might be an overstatement) so I'm sure some of what I write is more than a bit messy. I'll make the adjustments and report back. For reference, using the software "push" in the hardware tab with the code as written works correctly in the console and in the sim. Thanks.

bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Re: Push Button Intermittent

#8 Post by bdcurry »

Unfortunately, same result. Here's the order of events....

1) Cold boot on the machine, Arduino plugged into the USB port prior to cold boot of the computer.
2) Computer finishes load, start Air Manager, ensure Arduino is connect and shows in the device window.
3) Load the test instrument (click on the instrument in the "create/edit" tab), hit "run".
4) Start X-Plane (11, latest update)
5) Make the suggest code changes, confirm that things run by checking the LED I have hooked up, use the "push" in the lower right window to simulate the button push -- all works
6) First time hitting the physical button, it doesn't work (this button works the volts/oat button on the stock Laminar 172 chronometer).
7) I hit the "run" again, the console recycles and then the physical button works.
8) The physical button works continuously (~50 pushes).
9) I hit the "run" again to recycle the console and then the button no longer works.
10) I continue to hit the "run" to restart the code, at some point after many restarts, the physical button starts working again. (The other hardware (LED) continues to work in every instance).
11) If I hit "run" again, the physical button stops working and won't restart. Additionally, I notice the transmit led on the Arduino board is no longer flashing when I depress the physical button. At this point, I cold reboot and the cycle repeats.....

I know this sounds weird but that's what happens. Sometimes I have to hit the "run" button several times to get it to work, sometimes it doesn't. I checked the port settings: in X-Plane its set to COM3, 38400 baud; on the computer set to COM3, 38400 baud (again, everything but this physical switch works without issue). My only thought (and this is from a mechanical engineer so take it for what it's worth) is that the connection between the Arduino and Air Manager gets confused when the code is restarted (and restarted...) although this doesn't account for why it needs to be "restarted" when first launching AM/X-Plane. Thanks again for the help and input, nothing more frustrating than an intermittent.

...and a follow-up question: what's the difference between <A> and <> in the pin definition, couldn't find an explanation anywhere.

User avatar
Ralph
Posts: 7924
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Push Button Intermittent

#9 Post by Ralph »

Forget about the reproducing :) The problem is in your script. Try this.

Code: Select all

local global_oat = 0

function button_pressed()
    global_oat = (global_oat +1) % 3
    xpl_dataref_write("laminar/c172/knob_OAT", "FLOAT", global_oat)
end

hw_button_add("ARDUINO_MEGA2560_A_D42", button_pressed)

bdcurry
Posts: 20
Joined: Tue Feb 16, 2021 1:28 am

Re: Push Button Intermittent

#10 Post by bdcurry »

I put the code in and ran it again. Same results. However...I stripped every other device off the board (basically a bare Arduino and a single bare wire) and deleted all other code in the instrument with the exception of what you (Ralph) suggested (cloned the instrument, deleted everything else). It runs without a hitch, even after hitting the "run" and "stop" (restart the code) many, many times. Not sure exactly what it is, but I'll add things back in one-by-one and see what causes the issue. I had a single 44780 display and 4 LED's in addition to the push button so it won't be hard to backtrack. I am curious what the <A> versus <> does though. I appreciate the code help (now realize I didn't need to be reading that dataref to write what I wanted to it) and the direction towards the code. I'll work back through this and let you know what happens. Thanks!

Post Reply