некоректная работа IRRemote с диммером

MXPain
Offline
Зарегистрирован: 13.06.2014

Всем привет, собрал модуль диммера по этой схемке http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circ... и решил прикрутить к нему ir пульт KEYES для управления нагрузкой, вот его пример подключения https://arduino-info.wikispaces.com/IR-RemoteControl. В итоге при включении диммера, пульт вообще переставал работать и в results.value приходил полный рандом, гугл выдал следующее решение, перевести IRRemote на Timer1 в файлике IRRemoteInt.h что я собственно и сделал.

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#else
  #define IR_USE_TIMER1   // tx = pin 9
  //#define IR_USE_TIMER2     // tx = pin 3
#endif

Но тут возникла следующая проблема, пульт работает корректно только на расстоянии около 1м, при отдалении от ик приемника опять рандом. Вот тестовый код:

#define UP 0x511DBB
#define DOWN 0xA3C8EDDB
#define BUTTON2 0x97483BFB

#include "IRremote.h"

#include <TimerOne.h>           // Avaiable from http://www.arduino.cc/playground/Code/Timer1

volatile int i=0;               // Variable to use as a counter of dimming steps. It is volatile since it is passed between interrupts
volatile boolean zero_cross=0;  // Flag to indicate we have crossed zero
int AC_pin = 3;                 // Output to Opto Triac
int buton1 = 4;                 // first button at pin 4
int buton2 = 5;                 // second button at pin 5
int dim2 = 0;                   // led control
int dim = 128;                  // Dimming level (0-128)  0 = on, 128 = 0ff
int pas = 8;                    // step for count;
int freqStep = 75;              // This is the delay-per-brightness step in microseconds. It allows for 128 steps
                                // If using 60 Hz grid frequency set this to 65
int receiver = 9; // pin 1 of IR receiver to Arduino digital pin 11

boolean isLusterOn = false;//выключена ли люстра
int dimtemp = 0;//настройки диммера при отключении люстры  

IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'

 
void setup() {  // Begin setup
  Serial.begin(9600);   
  pinMode(buton1, INPUT);  // set buton1 pin as input
  pinMode(buton2, INPUT);  // set buton1 pin as input
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  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);      // Go to dim_check procedure every 75 uS (50Hz)  or 65 uS (60Hz)
  // Use the TimerOne Library to attach an interrupt

  irrecv.enableIRIn(); // Start the receiver
}

void zero_cross_detect() {    
  zero_cross = true;               // set flag for dim_check function that a zero cross has occured
  i=0;                             // stepcounter to 0.... as we start a new cycle
  digitalWrite(AC_pin, LOW);
}                                 

// Turn on the TRIAC at the appropriate time
// We arrive here every 75 (65) uS
// First check if a flag has been set
// Then check if the counter 'i' has reached the dimming level
// if so.... switch on the TRIAC and reset the counter
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 flag
    } 
    else {
      i++;  // increment time step counter                     
    }                                
  }    
}                                      

void loop() {  
  
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR(); 
    irrecv.resume(); // receive the next value
  } 

}

void translateIR() // takes action based on IR code received

  if(results.value==UP && isLusterOn) //up
  {
    if (dim>5)  
    {
       dim = dim - pas;
       
    if (dim<0) 
      {
        dim=0;
      }
    }
  }

 if(results.value==DOWN && isLusterOn)//down
 {
     if (dim<127)  
    {
      dim = dim + pas;
      
      if (dim>127) 
      {
        dim=128;
      }
    }
 }
  
  dim2 = 255-2*dim;
  if (dim2<0)
  {
    dim2 = 0;
  }
  
  if(results.value==BUTTON2)//выключаем люстру если нажата 2
 {
   isLusterOn = !isLusterOn;
   //isLusterOn? dim = 0: dim = 128;
   if(isLusterOn)
   {
     //включаем люстру
    //Serial.println(dim);
     //dim = 0;
     dim = dimtemp;
   }
   else
   {
     //выключаем люстру
     dimtemp = dim;
     dim = 128;
   }
 }


}

Помогите плиз разобраться в чем трабл, заранее спасибо.

jeka_tm
jeka_tm аватар
Offline
Зарегистрирован: 19.05.2013

почитай получше про эти библиотеки. не одинаковый ли таймер они обе используют