Go Down

Topic: can't figure out Interrupt for Tiny (Read 280 times) previous topic - next topic

Bwanna

I've have been struggling for days trying to figure out this and could really use some help.

In short I have a working sketch for UNO that uses "attachInterrupt(digitalPinToInterrupt(3), checkButton, CHANGE);" successfully.

I'd like to move this over to the ATtiny85 board, but am unable to figure out how to do convert this instruction.

Please, if anyone has a basic sketch that works, I'd very much appreciate the help.

Basically, the sketch supports a button activating a routine when it goes to GND.  I am looking to have the button be an interrupt so as it immediately activates the routine without waiting to cycle back around in the Void.


INTP

You're using an ATTiny85 "board"? Which one?

Int 0 is on pin 2, aka IC chip leg 7

jremington

#2
Jul 01, 2017, 06:46 am Last Edit: Jul 01, 2017, 06:48 am by jremington
Google is your friend.
https://thewanderingengineer.com/2014/08/11/pin-change-interrupts-on-attiny85/
Also explained in the ATtiny datasheets.

Bwanna

#3
Jul 01, 2017, 01:56 pm Last Edit: Jul 01, 2017, 02:09 pm by Bwanna
Thanks for the replies.  Yes, have tried the link suggested.  While it provides a start, I've still not been successful... hence the post for help.

This is a Digispark kickstarter ATTiny85 shown here: http://www.ebay.com/itm/222226862152

More specifics for my current design and apologize for not putting in the OP:

-Pins 5, 6, & 7 are OUTPUTS that drive relays.
-Pins  2 & 3 are INPUTS that (when go LOW) call to various subroutines that trigger the relays.

I'm trying to find the interrupt coding that will allow the INPUTS to be acted upon when they go LOW.  I intend to pull these INPUT pins HIGH via internal pullups.

Maybe I've chosen the wrong pins?  I've read there are maybe issues with certain pins affecting the startup of the Tiny?

ALSO, I've noticed that the device will respond to various coding attempts ONLY after it is uploaded and still plugged into USB.  If I remove it from USB and run it via battery, then no response.  (Battery voltage input is tested good).

Thanks Again!!


Here's test code I've been using:

#include "interrupt.h"
#define Relay PB2   // pin 7

void setup()
{
 GIMSK = 0b00100000;    // turns on pin change interrupts
 PCMSK = 0b00011000;    // turn on interrupts on pins PB3, PB4
 sei();                              // enables interrupts
 pinMode(Relay, OUTPUT);
}

void loop()
{
}

ISR(PCINT0_vect)
{
 for (int i=0; i <= 500; i++){
 digitalWrite(Relay, HIGH);
 delayMicroseconds(500);
 digitalWrite(Relay, LOW);
 }
}

jremington

#4
Jul 01, 2017, 04:51 pm Last Edit: Jul 01, 2017, 04:55 pm by jremington
Post your circuit diagram. Always use code tags when posting code.

I suspect a power problem, which you can test by getting rid of the relay and lighting an LED (with current limiting resistor) with the relay pin instead.

PB3 and 4 are used by the USB port, which will prevent their use when plugged in.

Finally, do not use delay in an interrupt. It won't work. Just set a global flag (declared "volatile") and exit the interrupt. The loop() function should do the work.

Bwanna

Post your circuit diagram. Always use code tags when posting code.

I suspect a power problem, which you can test by getting rid of the relay and lighting an LED (with current limiting resistor) with the relay pin instead.

PB3 and 4 are used by the USB port, which will prevent their use when plugged in.

Finally, do not use delay in an interrupt. It won't work.
Thanks for the advice. 

How do I add "code tags"?  That's the part I couldn't find.

The relay doesn't 'seem' to be an issue as I've been able to have a single interrupt trigger a short flip-flop of a relay output.

Understand about the delay in interrupt... found that after posting.  The final circuit doesn't use delays, it was just to create a flip-flop in the called function so I would know it was occurring.  Also, thought it was OK to use delayMicroseconds() with interrupts?  maybe I read it wrong. 

Bwanna

#6
Jul 01, 2017, 05:57 pm Last Edit: Jul 01, 2017, 06:13 pm by Bwanna
Here's a full coding for the schematic shown.

The first ISR(PCINT0_vect) function does respond to when I put PB4 to HIGH, or PB3 to LOW (weird).

IF I comment out this function it doesn't work at all using the attachInterrupt coding.

I'd like to call separate functions (RELAY1 & RELAY2) correlating to which button is pressed (BUTTON1 & BUTTON2).

While I could code this to be a dilgitalRead(), would much prefer to be done using two interrupts.

As mentioned, the coding is working on the UNO if I use "attachInterrupt(digitalPinToInterrupt(Button1), RELAY1, CHANGE);"   ... it's converting it to the ATTiny85 that's the challenge.


Code: [Select]
#include "interrupt.h"
#include <util/delay.h>
#define Relay1 PB1   // pin 6
#define Relay2 PB2   // pin 7
int ButtonPin1 = 3;      //physical pin
int ButtonInt1 = 4;      //PB4

int ButtonPin2 = 2;      //physical pin
int ButtonInt2 = 3;      //PB3


void setup()
{
 GIMSK = 0b00100000;    // turns on pin change interrupts
 PCMSK = 0b00011000;    // turn on interrupts on pins PB3, PB4
 sei();                 // enables interrupts
 
 pinMode(Relay1, OUTPUT);
  digitalWrite(Relay1, HIGH);
 pinMode(Relay2, OUTPUT);
  digitalWrite(Relay2, HIGH);  
  
 pinMode(ButtonPin1, INPUT);
  digitalWrite(ButtonPin1, HIGH);
 pinMode(ButtonPin2, INPUT);
  digitalWrite(ButtonPin2, HIGH);
 
//  attachInterrupt(ButtonInt1, Relay1funct, LOW);
//  attachInterrupt(ButtonInt2, Relay2funct, LOW);
 
}

void loop()
{
}

ISR(PCINT0_vect)    //        WORKS
{
 for (int i=0; i <= 1; i++){
    digitalWrite(Relay1, HIGH);
    delayMicroseconds(900000);
//    _delay_ms(100);      // delay 250 ms
    digitalWrite(Relay1, LOW);
    delayMicroseconds(900000);
//    _delay_ms(100);      // delay 250 ms                
  }
}



jremington

#7
Jul 01, 2017, 07:47 pm Last Edit: Jul 01, 2017, 08:09 pm by jremington
The maximum safe value of current into or out of a pin is 20 mA.

If your relay coil draws more than this (which is extremely likely), you will destroy the ATTiny. Use a transistor to activate the relay coil.

The schematic shows no pullup or pulldown resistors on the buttons, and you don't enable internal pullups, so it is hard to see how the circuit could ever work.

For the "pin change" interrupts, you cannot specify a level. A change in the pin condition activates the interrupt.

Bwanna

The maximum safe value of current into or out of a pin is 20 mA.

If your relay coil draws more than this (which is extremely likely), you will destroy the ATTiny. Use a transistor to activate the relay coil.

The schematic shows no pullup or pulldown resistors on the buttons, and you don't enable internal pullups, so it is hard to see how the circuit could ever work.

For the "pin change" interrupts, you cannot specify a level. A change in the pin condition activates the interrupt.
-I'll check the relay coils again.  thanks for pointing that out

-You're correct on the code I gave that it didn't have INPUT_PULLUP... the code I'm using does include that and still no improvement

-I didn't realize only 'change' state worked in the attachInterrupt() function.


Thanks!

Go Up