#include <avr/interrupt.h>
#include <TimeLib.h>
const int debounceTime=20;
const int ledPin=5;
unsigned long int currentTime0=0;
unsigned long int currentTime1=0;
volatile unsigned long int triggerTime0=0;
volatile unsigned long int triggerTime1=0;
volatile int luminosity=0;
bool stateOfPD2=true;
bool stateOfPD3=true;
void setup(){
DDRB|=(1<<PB5|1<<PB4|1<<PB3|1<<PB2|1<<PB1|1<<PB0);//outputs for segments A B C D E F
DDRD|=(1<<PD7|1<<PD5);//output for G segment
DDRD&=~(1<<PD2|1<<PD3);//configuring pin 2 and 3 as inputs
PORTB=(1<<PB5|1<<PB4|1<<PB3|1<<PB2|1<<PB1|1<<PB0);//default will be 0
PORTD&=~(1<<PD7|1<<PD5);//setting G segment to 0
PORTD|=1<<PD2|1<<PD3;//pullup resistor for both pins
EIMSK|=0b00000011;//configuring both INT0 and INT1 interrupts
EICRA|=0b00000101;//configuring interrupt for any change so if we hold down the buttons for some time we can debounce the event when we let go of them aswell
sei();
}
ISR(INT0_vect){
currentTime0=millis();
if(currentTime0-triggerTime0>debounceTime&&stateOfPD2&&luminosity>0){
luminosity--;
}
triggerTime0=millis();
}
ISR(INT1_vect){
currentTime1=millis();
if(currentTime1-triggerTime1>debounceTime&&stateOfPD3&&luminosity<9){
luminosity++;
}
triggerTime1=millis();
}
int main(){
init();
setup();
while(true){
stateOfPD2=(PIND&1<<PD2);
stateOfPD3=(PIND&1<<PD3);
updateLuminosity();
}
}
void updateLuminosity(){
switch (luminosity){
case 0:
PORTB=1<<PB5|1<<PB4|1<<PB3|1<<PB2|1<<PB1|1<<PB0;
PORTD&=~(1<<PD7);
break;
case 1:
PORTB=1<<PB4|1<<PB3;
PORTD&=~(1<<PD7);
break;
case 2:
PORTB=1<<PB5|1<<PB4|1<<PB2|1<<PB1;
PORTD|=1<<PD7;
break;
case 3:
PORTB=1<<PB5|1<<PB4|1<<PB3|1<<PB2;
PORTD|=1<<PD7;
break;
case 4:
PORTB=1<<PB4|1<<PB3|1<<PB0;
PORTD|=1<<PD7;
break;
case 5:
PORTB=1<<PB5|1<<PB3|1<<PB2|1<<PB0;
PORTD|=1<<PD7;
break;
case 6:
PORTB=1<<PB5|1<<PB3|1<<PB2|1<<PB1|1<<PB0;
PORTD|=1<<PD7;
break;
case 7:
PORTB=1<<PB5|1<<PB4|1<<PB3;
PORTD&=~(1<<PD7);
break;
case 8:
PORTB=1<<PB5|1<<PB4|1<<PB3|1<<PB2|1<<PB1|1<<PB0;
PORTD|=1<<PD7;
break;
case 9:
PORTB=1<<PB5|1<<PB4|1<<PB3|1<<PB2|1<<PB0;
PORTD|=1<<PD7;
break;
}
analogWrite(ledPin,luminosity*28);
}