Attiny85 ir dimmer

i need little help here i'm traying to making dimmer circuit with attiny85 base on ir
but i have problem with may be code because it's work fine for relay but not working for dimming ac appliances

#include <EEPROM.h>
#define relay 1
const int irPin = 4;            
int  load;
int spd;

#include <TimerOne.h>           
volatile int i=0;               // Variable to use as a counter
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3;                 // Output to Opto Triac

int dim2 = 0;                   // led control
int dim = 128;                  // Dimming level (0-128)  0 = on, 128 = 0ff                  
int freqStep = 75;    // This is the delay-per-brightness step in microseconds.

void setup() 
{
  //Serial.begin(9600);   
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  pinMode(relay, OUTPUT); 
  pinMode(irPin, INPUT);
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  Timer1.initialize(freqStep);                      // Initialize TimerOne library for the freq we need
  Timer1.attachInterrupt(dim_check, freqStep);
  digitalWrite(relay, LOW);
  load = EEPROM.read(1);
  dim = EEPROM.read(2);
}

void zero_cross_detect() 
{    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  i=0;
  digitalWrite(AC_pin, LOW);
}                                 
// Turn on the TRIAC at the appropriate time

void dim_check() 
{                   
  if(zero_cross == true) {              
    if(i>=dim) {                     
      digitalWrite(AC_pin, HIGH);  // turn on light       
      i=0;  // reset time step counter                         
      zero_cross=false;    // reset zero cross detection
    } 
    else {
      i++;  // increment time step counter                     
}}}                                      


void loop() 
{
int key = getIrKey();
{
  switch(key)
  {
  case 1778:  
    {
   
    if (dim<127)  
   {
    dim = dim + 8;
    EEPROM.write(2, dim);
    if (dim>127) 
    {
      dim=128; // in vechiul sketch era 127
      EEPROM.write(2, dim);
    }
    }
    }
    
    break;

  case 1523:  
    {
    
      {
  if (dim>5)  
  {
     dim = dim - 8;
     EEPROM.write(2, dim);
  if (dim<0) 
    {
      dim=0;  // in vechiul sketch era 1
      EEPROM.write(2, dim);
    } 
   }
      }
    }
    break;
      case 2798:  
        {
          if (key == 2798 && digitalRead(1) == 1)
          { // For Load1 On
            load = 0;
            EEPROM.write(1, load);
            digitalWrite(relay, load);
          }
          else if (key == 2798 && digitalRead(1) == 0)
          { // For Load1 Off
            load = 1;
            EEPROM.write(1, load);
            digitalWrite(relay, load);
        }

      }
    
    break;
  }
}
}

int getIrKey()
{
  int len = pulseIn(irPin,LOW);
  int key, temp;
  key = 0;
  //Serial.print("len=");
  //Serial.println(len);
  if(len > 5000) {
    for(int i=1;i<=32;i++){
      temp = pulseIn(irPin,HIGH);
      if(temp > 1000)
        key = key + (1<<(i-17));
    }
  }
  if(key < 0 )
    key = -key;
  
  //if(key)
    //Serial.println(key);

    delay(250);
  return key;
}

please help me to find where do i mistake in code circuit just fine works with my nano with different

Hi .
in this routine:
case 2798:

       if (key == 2798 && digitalRead(1) == 1)
       { // For Load1 On
         load = 0;
         EEPROM.write(1, load);
         digitalWrite(relay, load);
       }
       else if (key == 2798 && digitalRead(1) == 0)
       { // For Load1 Off
         load = 1;
         EEPROM.write(1, load);
         digitalWrite(relay, load);
       }

test key == 2798 && digitalRead(1) == 1
pin 1 (relay) is set to 0
load = 0;
digitalWrite(relay, load);
then
test key == 2798 && digitalRead(1) == 0
and since pin 1 is 0
the relay is set to 1
load = 1;
digitalWrite(relay, load);

The relay is one picosecond, it becomes 0, and then it becomes 1.

RV mineirin

1 Like

Relay is just work fine it's perfectly turning on and of without any issues just my my led is not dimming it's just stay at highest brightness