Detecting button press outside of loop()

Hey Guys, I was under the impression everything goes on from within the loop() function, but i've seen code where a guy is detecting button presses and rotary encoders and i can't see any relation to the functions he's using inside of loop().

if it possible to detect button presses outside of loop()?

cheers :slight_smile:

Interrupts? Without seeing the code, there's no way to know for sure.

if it possible to detect button presses outside of loop()?

Of course. The digitalRead() function doesn't care where it is called from.

oh ok, im only new at this game so I was under the impression it would only run code in loop() over and over again. is there anything i should be reading that shows me otherwise?

cheers :smiley:

I was under the impression it would only run code in loop() over and over again.

The loop function is called over and over. Don't you see examples, though, where calls to digitalRead(), analogRead(), digitalWrite(), analogWrite(), etc. are called?

You can define functions that you call from loop(). Those functions can call other functions, like digitalRead(), analogRead(), digitalWrite(), analogWrite(), etc., or other functions that you define, that call digitalRead(), analogRead(), digitalWrite(), analogWrite(), etc.

is there anything i should be reading that shows me otherwise?

Anything that progresses beyond the "See Spot Run" level of project.

yeah ive been calling functions ive make up from outside of loop, but for them to run they need to be referenced back into loop() somehow dont they?

yeah ive been calling functions ive make up from outside of loop, but for them to run they need to be referenced back into loop() somehow dont they?

A function "runs" when you call it. I have no idea what you mean by "referenced back into loop".

Without seeing the code, all we're doing here is wasting time guessing. So if you'd like a better answer of how it's done, then you need to show us the actual code you saw, wherever it was or is.

I was under the impression everything goes on from within the loop() function, but i've seen code where a guy is detecting button presses and rotary encoders and i can't see any relation to the functions he's using inside of loop().

yeah ive been calling functions ive make up from outside of loop, but for them to run they need to be referenced back into loop() somehow dont they?

KirAsh4 mention about "interrupts".
Yes, this can be done by "interrupts".

if you see in setup() you will find the relation.

ok i saw the idea on Arduino LCD Menu - YouTube and the code is shown on Code used on demo project shown at (http://www.youtube.com/watch?v=a1M5kirA2_8) and (http://www.youtube.com/watch?v=epJ3aSzrsXQ) Includes climate sensing, logging to SD card, HTTP server with JSON output, LCD display with settings menus. · GitHub

There is alot of code there but in the code for the loop, I cannot see any reference to the display menu() function.

here is the loop()

void loop() {
  ledStatus();
  populateData();
  displayData();
  runServer();
  if (!sound && !motionUp && !motionDn) autoBrightness();
  if (sound) ledColor(0,0,b_level);
  log();
  if (motionUp || motionDn) ledColor(r_level,0,0);  
}

and here is the display menu()

void displayMenu() {
  int selected = 3;
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Turn the knob to");
  lcd.setCursor(0,1);
  lcd.print("select an option");
  // display options
  while (!buttonR.pressed()) {
    selected += readEncoder();
    if (selected<0) selected=3;
    if (selected>3) selected=0;
    if (moved) {
      lcd.clear();
      lcd.setCursor(0,0);
      switch(selected) {
        case 0:
          lcd.print("Change Units    ");
          break;
        case 1:
          lcd.print("Backlight On/Off");
          break;
        case 2:
          lcd.print("Logging On/Off  ");
          break;
        case 3:
          lcd.print("Exit Settings   ");
          break;
      }
    }
  }
  // when button is pressed...
  lcd.clear();
  switch(selected) {
    case 0:
      settingsChangeUnits();
      break;
    case 1:
      settingsChangeBacklight();
      break;
    case 2:
      settingsChangeLogging();
  }
}

Theres more but i don't want to clog your screens.

It's invoked by an interrupt, which is set at the top of the setup function.

There is alot of code there but in the code for the loop, I cannot see any reference to the display menu() function.

The function is triggered by an interrupt:

void setup() {
  // setup interrupts
  attachInterrupt(BUTTON, displayMenu, RISING);