#include <avr/io.h>
#include <util/delay.h>
volatile int count = 0;
volatile int lumi;
volatile int time1 = 0;
volatile int time2 = 0;
ISR(INT0_vect){
if (lumi < 252){
if ((millis() - time1) > 100){
lumi = lumi + 28;
count += 1;
time1 = millis();
}
}
}
ISR(INT1_vect){
if (lumi >= 28){
if ((millis() - time2) > 100){
lumi = lumi - 28;
count -= 1;
time2 = millis();
}
}
}
void displayDigit(int digit) {
// Define the segment codes for numbers 0-9
// For a common cathode 7-segment display
switch(digit){
case 0: PORTB = 0b00000000, PORTD |= 1 << PD4;
break;
case 1: PORTB = ~0b00000110, PORTD |= 1 << PD4;
break;
case 2: PORTB = ~0b00011011, PORTD &= ~(1 << PD4);
break;
case 3: PORTB = ~0b00001111, PORTD &= ~(1 << PD4);
break;
case 4: PORTB = ~0b00100110, PORTD &= ~(1 << PD4);
break;
case 5: PORTB = ~0b00101101, PORTD &= ~(1 << PD4);
break;
case 6: PORTB = ~0b00111101, PORTD &= ~(1 << PD4);
break;
case 7: PORTB = ~0b00000111, PORTD |= 1 << PD4;
break;
case 8: PORTB = ~0b00111111, PORTD &= ~(1 << PD4);
break;
case 9: PORTB = ~0b00101111, PORTD &= ~(1 << PD4);
break;
default: PORTB = 0xFF, PORTD |= 1 << PD4; // Display nothing for invalid input
break;
}
}
void setup(){
Serial.begin(9600);
DDRB = 0xFF;
PORTB = 0b000000000;
DDRD = 0xFF;
PORTD |= (0 << PD0);
PORTD = 0b000000000;
DDRD &= ~(1 << PD2); // Set PD2 as input
DDRD &= ~(1 << PD3); // Set PD3 as input
PORTD |= (1 << PD2) | (1 << PD3);
EIMSK |= (1 << INT0) | (1 << INT1); // Enable both INT0 and INT1
EICRA |= (0 << ISC01) | (0 << ISC11);
sei();
}
int main(){
init();
setup();
int bright= 0;
while(1){
bright = map(lumi,0,252,0,255);
analogWrite(6,bright);
displayDigit(count);
Serial.println(count);
}
}