LowPowerLab Forum

Hardware support => General topics => Topic started by: RuggoMuggo on May 10, 2019, 01:25:34 AM

Title: Multiple Inputs?
Post by: RuggoMuggo on May 10, 2019, 01:25:34 AM
Hello! I was just wondering if it is possible to have more than one input on a Moteino Mega. I currently am using the TxRxBlinky example from the RFM69 examples folder, and was just wondering if it was possible to have multiple inputs. I couldn't seem to get another input to work, and I also tried changing the pin number for the one input that did work. None other than pin 11 (the default from the example) worked and I was wondering if this was the only input pin I could create or if I was just doing something wrong.

I attached the sketch if that would be beneficial.
Title: Re: Multiple Inputs?
Post by: Felix on May 10, 2019, 09:05:43 AM
This is a very basic question about how to use a pin as an input. You have to debounce it if it's a button. The given sketch uses a pin with an attached interrupt.

Any pin can be an input including the "analog" pins (they can be used as digital). If you need an interrupt then choose an interrupt pin (one is used by the RFM radio, the other is used in the sketch "TEMP1").
Title: Re: Multiple Inputs?
Post by: RuggoMuggo on May 13, 2019, 11:53:07 AM
So I was just trying to get two different inputs that would send the notification to the other chip. How would I do that? I cannot seem to figure out what to write in the code.
Title: Re: Multiple Inputs?
Post by: Felix on May 13, 2019, 07:30:07 PM
Why don't you start from an example like the TXRXBlinky (https://github.com/LowPowerLab/RFM69/blob/master/Examples/TxRxBlinky/TxRxBlinky.ino) which does just that?
Title: Re: Multiple Inputs?
Post by: RuggoMuggo on May 14, 2019, 01:23:56 AM
Okay I used the txrxblinky example. However I have run into some small issues. One, my second input doesn't actually send anything to the chip when the 2nd input is pressed. However the first input still works fine ie: input 11.  Also when changing the first input to pin 12 (without the 2nd input, basically just taking the sample code and changing the input pin number) the code does not seem to work either.
Title: Re: Multiple Inputs?
Post by: TomWS on May 14, 2019, 08:32:29 AM
In the code:

void handleButton()
{
  mainEventFlags |= FLAG_INTERRUPT;
}

byte LEDSTATE=LOW; //LOW=0
void loop() {
  //******** THIS IS INTERRUPT BASED DEBOUNCING FOR BUTTON ATTACHED TO D3 (INTERRUPT 1)
  if (mainEventFlags & FLAG_INTERRUPT)
  {
   ...

How do you suppose 'handleButton()' gets called?

Title: Re: Multiple Inputs?
Post by: RuggoMuggo on May 14, 2019, 11:29:38 AM
Well it seems that it is a method that gets used when attaching the interrupt thing earlier in the code. I don't see how that correlates to the BUTTON_PIN though.
Title: Re: Multiple Inputs?
Post by: TomWS on May 14, 2019, 03:42:51 PM
Quote from: RuggoMuggo on May 14, 2019, 11:29:38 AM
Well it seems that it is a method that gets used when attaching the interrupt thing earlier in the code. I don't see how that correlates to the BUTTON_PIN though.
Learn what the functions do and all will become clear.
Title: Re: Multiple Inputs?
Post by: RuggoMuggo on May 14, 2019, 10:50:41 PM
Where do you recommend I do this. The lowpowerlab website section on programming and libraries and moteino programming does not explain what "attachInterrupt" does.
Title: Re: Multiple Inputs?
Post by: Felix on May 14, 2019, 11:04:25 PM
Quote from: RuggoMuggo on May 14, 2019, 10:50:41 PM
Where do you recommend I do this. The lowpowerlab website section on programming and libraries and moteino programming does not explain what "attachInterrupt" does.
@RuggoMuggo:

Again, that is an Arduino generic question. The main purpose of LowPowerLab and examples in RFM69 library is not to teach C++, programming 101 concepts or Arduino specific functionality. The arduino website (and the greater internet) does that at length.

For instance, if you search on google for attachInterrupt, the first link you get is to arduino's official documentation for that function (https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/).

I hope that helps.
Title: Re: Multiple Inputs?
Post by: RuggoMuggo on May 15, 2019, 12:10:44 AM
Thank you very much! Although I actually just used two links found on a  forums post about debouncing that you made a long time ago (https://lowpowerlab.com/forum/general-topics/button-debouncing-in-an-interrupt). Then I also realized that BUTTON_INT correlated to interrupt (on the board), not an integer value like I first thought pertaining to the amount of inputs. Then I went to the schematics (https://lowpowerlab.com/guide/moteino/design-files/) and found which board I was using.

Lastly I would like to ask, is it possible to add another interrupt input (more than two)? It seems that only two pins have INT next to them (https://gyazo.com/89483830594dd68676864e3ed05922ad). Or would I just need to use normal arduino button inputs?
Title: Re: Multiple Inputs?
Post by: TomWS on May 15, 2019, 07:50:57 AM
I highly recommend that you use the Bounce2 library (look for Bounce2 in your IDE library manager).  This will manage multiple switches and hide all the details about how you monitor a pin for an interrupt.   Their example library is good and includes 'bounce2buttons.ino', which shows you exactly how to do what you want.