Go Down

Topic: Detecting button press outside of loop() (Read 664 times) previous topic - next topic

Dylfish

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 :)

KirAsh4

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

PaulS

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

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

Dylfish

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 :D

PaulS

Quote
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.

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

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

Dylfish

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?

PaulS

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

KirAsh4

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.

Cybernetician

Quote

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


Quote

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.
From Idea To Invention

Dylfish

ok i saw the idea on http://youtu.be/a1M5kirA2_8 and the code is shown on https://gist.github.com/3783602

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()

Code: [Select]
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()

Code: [Select]

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.

wildbill

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

PaulS

Quote
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:
Code: [Select]
void setup() {
  // setup interrupts
  attachInterrupt(BUTTON, displayMenu, RISING);

Go Up