Giter Site home page Giter Site logo

Comments (6)

damellis avatar damellis commented on May 30, 2024

Access to the pin change interrupts is something that should be added to the Arduino core: https://github.com/arduino/Arduino/issues/248. We'd then pick it up for the ATtiny.

from attiny.

Schtrudel avatar Schtrudel commented on May 30, 2024

What do you mean by "should be added to the Arduino core"? It's part of the Arduino API, isn't it? (At least, it's working for mine :-) )

from attiny.

NicoHood avatar NicoHood commented on May 30, 2024

Are you talking about pin interrupts or pin change interrupts?

from attiny.

eried avatar eried commented on May 30, 2024

Hey Schtrudel, what it is working? I cant attach interrupts on my tiny85 :/

from attiny.

Schtrudel avatar Schtrudel commented on May 30, 2024

Hi eried.

I love these discussions with 3 messages in 3 years... :-) Reminds me of this joke: http://jokesareawesome.com/joke/869

Anyway, I don't remember how I solved it then, but later, I switched to another approach: I don't use attachInterrupt at all and use a few lines with the "native" mechanism. In my case, it detects the change on any of 2 buttons, and I detect which one in the ISR. Quick and dirty, but works very well for my use. Most of it shamelessly copied and adapted from https://bigdanzblog.wordpress.com/2014/08/10/attiny85-wake-from-sleep-on-pin-state-change-code-example/ [Thanks Big Dan]
For reference, the relevant parts of the code. I hope it can help.
////////////////////////////////////////////////////////////////////////////////////////////

#include <avr/sleep.h>
#include <avr/wdt.h>

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

const int button1 = 4;    
const int button2 = 3;
//...

volatile byte button2Pressed = 0;     // Don't forget to declare your global handled in ISR as volatile

ISR(PCINT0_vect) {
    if (digitalRead(button2))
      button2Pressed = 1;        
}


void setup()
{
//...
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
//...
}

void loop(){
     //...
     sleep();
     if (button2Pressed) {
       run_fun2();
     }
     else{
      run_fun1();
     }
    //...
}


void sleep() {
    GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
    PCMSK |= _BV(button1);                   // Use button1 as interrupt pin
    PCMSK |= _BV(button2);                  // Use button2 as interrupt pin

    ADCSRA &= ~_BV(ADEN);                   // ADC off
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);    // replaces above statement

    sleep_enable();                         // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
    sei();                                  // Enable interrupts
    sleep_cpu();                            // sleep

    cli();                                  // Disable interrupts
   PCMSK &= ~_BV(button1);
   PCMSK &= ~_BV(button2);
    sleep_disable();                        // Clear SE bit
    ADCSRA |= _BV(ADEN);                    // ADC on

    sei();                                  // Enable interrupts
} // sleep

from attiny.

eried avatar eried commented on May 30, 2024

Hahaha! I was thinking to reply in 1 more year but I am going to forget to do that, so thanks for the code! Iยดll use it that way too, it was 3 am and I was very frustrated led debugging the tiny85

from attiny.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.