#include <avr/io.h>
#include <avr/interrupt.h>
#include <TimeLib.h>
#include <math.h>
//mostmár ez is jónak tűnik
//TODO: set up seven segment decoder pins as outputs
//and bit combinations for them, and since no register is enought to host
//all of them u need to use 2 registers' pins
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(){
//TODO: setting up an analog pin for the PWM for the LED
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
Serial.begin(9600);
sei();
}
ISR(INT0_vect){
//debouncing for falling edge event
//checking with !(PIND&1<<PD2) if we had a falling edge->
//this checks if PIND is 0 in the PD2 bit if yes we had falling edge
currentTime0=millis();
if(currentTime0-triggerTime0>debounceTime&&stateOfPD2&&luminosity>0){
Serial.print("curr time0: ");
Serial.print(currentTime0);
Serial.print(" ");
/*if(luminosity>0)*/luminosity--;
Serial.println(luminosity);
}
//debouncing for rising edge event
//if PIND has 1 in PD2 bit it was a rising edge trigger event
//we need this so the buttons are debounced if we didnt let go of them in time
//so when we let them go the bouncing doesnt trigger a falling edge event
/*else if(currentTime-triggerTime0>debounceTime&&(PIND&1<<PD2))
{triggerTime0=millis();}*/
triggerTime0=millis();
}
//the else if is not needed if i update the trigger time in the end of the ISR
//outside of every if statement at just every interrupt event
//plus i check the current time before any statement cuz the millis func
//maybe fcking up the thing
ISR(INT1_vect){
currentTime1=millis();
if(currentTime1-triggerTime1>debounceTime&&stateOfPD3&&luminosity<9){
Serial.print("curr time1: ");
Serial.print(currentTime1);
Serial.print(" ");
/*if(luminosity<9)*/luminosity++;
Serial.println(luminosity);
}
/*else if(millis()-triggerTime1>debounceTime&&(PIND&1<<PD3))
{triggerTime1=millis();}*/
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,pow(1.851,luminosity)-0.5);//the task was to do 9 steps of 28 but this way brightness increases somewhat linearly
}