ATtiny85 relay controller

Hey Arduino Forums, I am working on a code that is intended to turn a relay on and off using an AT85. The code runs through the start up sequence but the button functions don’t seem to be working.

In short a prolonged button press is suppose to wake the AT from sleep and turn on the relay which would then turn on two LEDs. After the prolonged press the code goes into a switch statement that is suppose to switch between a flashing and steady state with secondary presses.

A prolonged press put the AT back to sleep.

Here is the code and a diagram of the circuit

Please help!

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

const int buttonPin = A2;
const int coilPin = 0;
const int coilPin1= 1;

uint8_t onOff;

volatile uint8_t buttonPushCounter = 0;
volatile uint32_t pressStart = 0;
volatile uint32_t pressEnd = 0;

void init_interrupt() {     // A function to set up the button press 
  GIMSK|=_BV(PCIE);         // interrupt. It fires on any change on the pin 
  PCMSK|=_BV(PCINT2);       // so on press or release of the button. 
  sei();
} // end of init_interrupt

ISR(PCINT0_vect) {                      // This is the function the interrupt triggers
  if(digitalRead(buttonPin) == LOW) {   // low will indicate the button press
    //buttonPushCounter += 1;           // increment the button press counter
    pressStart = millis();              // start/enable a 'soft' timer
  }
  else {                                // high indicates release
    buttonPushCounter += 1;             // or increment on release (better I think)
    pressEnd = millis();                //   
  }                                     // Plus it wakes the trinket up from sleep of course
} //end of ISR


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(coilPin, OUTPUT);
  pinMode(coilPin1, OUTPUT);
  onOff = LOW;
  init_interrupt();             // set up the button interrupt
  
  //Flash quick sequence so we know setup has started i.e. power applied
  for(int k=0;k<10;k=k+1){
    if(k%2==0){
      digitalWrite(coilPin,LOW);
      digitalWrite(coilPin1,HIGH);
    }
    else{
      digitalWrite(coilPin,HIGH);
      digitalWrite(coilPin1,LOW);
    }
    delay(50);
  } // end of flashing routine
} //  end of setup

void loop() {
  if(pressEnd) {                          // Button press released and can be measured
    if((pressEnd - pressStart) > 500){//if it was pressed longer than a second
      onOff = !onOff;                     // toggle device on or off
    }
    pressStart = 0;               // disable the 'soft' timer (see line 82)
    pressEnd = 0;
  }
  if(onOff) {                             // if switched on, run the switch statement
    switch(buttonPushCounter)
    {
      case 1:
        digitalWrite(coilPin,LOW);//full LED on 1st press
        digitalWrite(coilPin1,HIGH);
        break;
      case 2:
        digitalWrite(coilPin,HIGH);
        digitalWrite(coilPin1,LOW);
        delay(50);
        digitalWrite(coilPin,LOW);
        digitalWrite(coilPin1,HIGH);
        delay(50);
        break;
      default:
        buttonPushCounter = 1;              // another press or any other unforeseen roll over counter
        break;
    } // end of switch
  } // end of on functionality
  else {                                     // if switched off
    if(!pressStart) {                        // ...and not currently timing a button
      digitalWrite(coilPin,LOW);
      digitalWrite(coilPin1,HIGH);
      buttonPushCounter = 0;                 // counter back to 0
      set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // go to sleep
      sleep_enable();
      sleep_cpu();
  
      sleep_disable();
    }
  } // end of off functionality
}

Assuming the button really is connected to A2 aka digital pin 4 aka physical pin 3, try:

const int buttonPin = 4;
...
  PCMSK |= _BV(PCINT4);

Yea that is the pin. Tryed as const int buttonPin=4;

Perhaps I should do away with the sleep mode and switch mode momentarily and see if I can toggle the button press to turn the LED on and off when clicked and clicked a second time?

justliketheyusedto:
Yea that is the pin. Tryed as const int buttonPin=4;

You changed the PCMSK line though too, right?

justliketheyusedto:
Perhaps I should do away with the sleep mode and switch mode momentarily and see if I can toggle the button press to turn the LED on and off when clicked and clicked a second time?

Sure. Narrowing down a problem by simplification is always good.

Yeah I swapped both 4's no luck.

Your drawing is a bit unusual/incomplete. The other side of the switch is connected to ground, right?

Try toggling another unused pin inside of the interrupt handler on and off. Connect a voltmeter or a resistor&LED to the pin to determine if the interrupt handler is being called.