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
}