https://forum.arduino.cc/index.php?topic=650716.0
I will put the code again
#include <EEPROM.h>
#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#define setbit(port, bit) (port) |= (1<< (bit))
#define clearbit(port, bit) (port) &=~(1<< (bit))
#define POWER_LED_ON() clearbit(PORTD, PD7)
#define POWER_LED_OFF() setbit(PORTD, PD7)
uint8_t table[10] = {141, 125, 109, 94, 78, 62, 47, 31, 16, 1};
const int iRReceivePin = PD2;
IRrecv irrecv(iRReceivePin);
decode_results results;
/* iniialise speed to zero, and also store the set value in EEPROM
on every change to be read from it on power recycle.
*/
char speedVal = 0;
char num = 0;
uint8_t fan_on = 0;
uint8_t lastPowerState = 0;
void setup() {
// put your setup code here, to run once:
initialize();
//Initialize the zero crossing detector INT(1)
MCUCR |= ((1 << ISC11) | (1 << ISC10)); //INT in Rising edge
GICR |= (1 << INT1); //Enable INT1
//Set Timer 2
TCCR2 |= (1 << WGM21); //CTC
TIMSK |= (1 << OCIE2); //Enable OCI2
sei();
//Zero Crossing Detect.
/*
ISR(INT1_vect)
{
if (!fan_on)
{
PORTD |= (1 << PD5); //High = TRIAC Off
return;
}
if (9 == speedVal)
{
PORTD &= (~(1 << PD5)); //low = TRIAC ON
return;
}
PORTD |= (1 << PD5); //High = TRIAC Off
OCR2 = table[speedVal];
TCCR2 |= ((1 << CS22) | (1 << CS21) | (1 << CS20)); //Start Timer prescaler =1024
TCNT2 = 0x00;
}
ISR(TIMER2_COMP_vect)
{
PORTD &= (~(1 << PD5)); //low = TRIAC ON
TCCR2 &= (~((1 << CS22) | (1 << CS21) | (1 << CS20))); //Stop Timer
}
*/
}
void loop() {
while (1)
{
if (irrecv.decode(&results)) {
switch (results.value)
{
case 0xFF48B7: // NEC code for UP Key
{
if (speedVal < 9)
speedVal++;
Display (speedVal);
EEPROM.put(5, speedVal);
}
break;
case 0XFF609F: //NEC Code for DOWN Key
{
if (speedVal > 0)
speedVal--;
Display (speedVal);
EEPROM.put(5, speedVal);
}
break;
case 0xFFAA55: // NEC code for enter key
{
if (fan_on) {
POWER_LED_OFF();
fan_on = 0;
lastPowerState = fan_on;
EEPROM.put (3, lastPowerState);
}
else
{
POWER_LED_ON();
fan_on = 1;
lastPowerState = fan_on;
EEPROM.put (3, lastPowerState);
}
}
break;
}
irrecv.resume(); // Receive the next value
}
}
}
/* Seven Segment Display Function */
void Display(char num)
{
if (num > 9)
return;
switch (num)
{
case 0:
PORTC = 0B00000000;
PORTB = 0B00000010;
break;
case 1:
PORTC = 0B00111001;
PORTB = 0B00000010;
break;
case 2:
PORTC = 0B00100100;
PORTB = 0B00000000;
break;
case 3:
PORTC = 0B00110000;
PORTB = 0B00000000;
break;
case 4:
PORTC = 0B00011001;
PORTB = 0B00000000;
break;
case 5:
PORTC = 0B00010010;
PORTB = 0B00000000;
break;
case 6:
PORTC = 0B00000010;
PORTB = 0B00000000;
break;
case 7:
PORTC = 0B00111000;
PORTB = 0B00000010;
break;
case 8:
PORTC = 0B00000000;
PORTB = 0B00000000;
break;
case 9:
PORTC = 0B00010000;
PORTB = 0B00000000;
break;
}
}
void initialize()
{
irrecv.enableIRIn(); // Start the IR receiver
/* PD7 as output */
POWER_LED_OFF();
DDRD |= (1 << PD7);
/* Set PD2 and PD3 as inputs */
DDRD &= ~(1 << PD2);
DDRD &= ~(1 << PD3);
// Port C pins 0 to 5 as output for seg a b c d e f
// Port B pin 1 as output for segment g
DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4) | (1 << PC5);
DDRB |= (1 << PB1);
/* Sets all segments a to g in the display off */
setbit (PORTC, PC0);
setbit (PORTC, PC1);
setbit (PORTC, PC2);
setbit (PORTC, PC3);
setbit (PORTC, PC4);
setbit (PORTC, PC5);
setbit (PORTB, PB1);
EEPROM.get (5, speedVal);
Display (speedVal);
EEPROM.get (3, lastPowerState);
fan_on = lastPowerState;
if (lastPowerState) {
POWER_LED_ON();
}
else {
POWER_LED_OFF();
}
/* Output - blocking gate signal to opto isolator */
setbit(PORTD, PD5); //PD5 High = Triac off
DDRD |= (1 << PD5); //set DDR for port BIT 5
}
where can the problem be?
Thanks in anticipation.