Pages: [1]   Go Down
Author Topic: Detecting button press outside of loop()  (Read 522 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if it possible to detect button presses outside of loop()?
Of course. The digitalRead() function doesn't care where it is called from.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 50
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46266
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {
  // setup interrupts
  attachInterrupt(BUTTON, displayMenu, RISING);
Logged

Pages: [1]   Go Up
Jump to: