need help

i made dimmer circuit.its working very well but when i control it using ir sensor its not working:(
plz help me
*/

#include <IRremote.h>
#include <TimerOne.h> // Avaiable from Arduino Playground - Timer1

const int RECV_PIN = 6;

IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long last = millis();

// Hex code for remote control buttons
#define POWER 0x10EFD827
#define A 0x10EFF807
#define B 0x10EF7887
#define C 0x10EF58A7
#define UP 0x10EFA05F
#define DOWN 0x10EF00FF
#define LEFT 0x10EF10EF
#define RIGHT 0x10EF807F
#define SELECT 0x10EF20DF

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 dim2 = 0; // led control
int dim = 128; // Dimming level (0-128) 0 = on, 128 = 0ff
int pas = 8; // step for count;
int freqStep = 65; // This is the delay-per-brightness step in microseconds. It allows for 128 steps
// If using 60 Hz grid frequency set this to 65

void setup() { // Begin setup
Serial.begin(9600);
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
irrecv.enableIRIn(); // Start the IR receiver

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

}

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 test_dimmer(){
dim+=inc;
if((dim_ac>=128) || (dim_ac<=0))
inc*=-1;
delay(18);
}

void loop() {

if (irrecv.decode(&results))
{

if (results.value == UP){
// If it's been at least 1/4 second since the last
if (millis() - last > 250) {
Serial.print("Dimming up --> ");
Serial.println(dim);
if (dim<127)
{
dim = dim + pas;
if (dim>127){
dim=128;
}
}
}
}

if (results.value == DOWN ){
// If it's been at least 1/4 second since the last
if (millis() - last > 250) {
Serial.print("Dimming down --> ");
Serial.println(dim);

if (dim>5)
{
dim = dim - pas;
if (dim<0){
dim=0;
}
}
}
}
} // loop

there's not dimming at all, IR receiver doesnt work with this code..

Do you think you can help me?

thank you sooo much!

marC:)

What do you see when you print results.value ?

Once you receive the first IR code, communication with the remote control is turned off. You never turn it back on. A call to irrecv.resume() needs to happen somewhere in your code.

Please read Nick Gammon's two posts at the top of this Forum about using this Forum correctly, especially about using code tags when posting.

  • L33T-SP3AK baby-talk
  • uncapitalised 'I'
  • no code tags
  • unhelpful two-word post title

And he's trying to write interrupt-driven code.

i made dimmer circuit.its working very well but when i control it using ir sensor its not working:(

Ok. So you can write code that works the dimmer, but you cannot get the IR sensor working.

Cool.

Write a separate sketch that works with the IR remote and prints debugging information to Serial. Start small - detect one button, detect a button up/down.

if (irrecv.decode(&results))

Does this function ever return a nonzero? If not - maybe it's your sensor or something. If it does sometimes return that something happened, well - what kinds of results are you getting back? Is it matching what you expect?

Ideally, what you would do is package up the results of your research in a C++ class so you can use it without having to think about it.