Multiple Inputs?

Started by RuggoMuggo, May 10, 2019, 01:25:34 AM

RuggoMuggo

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.

Felix

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").

RuggoMuggo

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.

Felix

Why don't you start from an example like the TXRXBlinky which does just that?

RuggoMuggo

#4
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.

TomWS

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?


RuggoMuggo

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.

TomWS

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.

RuggoMuggo

Where do you recommend I do this. The lowpowerlab website section on programming and libraries and moteino programming does not explain what "attachInterrupt" does.

Felix

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.

I hope that helps.

RuggoMuggo

#10
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. 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 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. Or would I just need to use normal arduino button inputs?

TomWS

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.