Dimmers Control

Hi There

Can any one suggest me the problem with the following code ?
It's not working. With single dimmer it is working OK. The operating voltage is 220V, 50Hz

/*
 AC Light Control
 Zero-cross - pin 2
 Lights dimmers on pins: 3, 4, 5, 6, 7, 8, 9, 10
 Buttons for changing the lights on pins: A0, A1 (up one/down one)
 Buttons for changing the dimm amount: A2, A3 (up level/down level)
  
*/

#include <TimerOne.h>           
int limit = 1500;               // changing the limit will affect the time that you have to keep the button pressed
int dim_increase = 0;
int dim_decrease = 0;

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[9] = {3, 4, 5, 6, 7, 8, 9, 10};                
int dim[9];                     // Dimming level (0-128)  0 = on, 128 = 0ff
int c[9];                       // Counter for each light
int light = 0;                  // The starting/current light pin (0=3)
int freqStep = 78;              // This is the delay-per-brightness step in microseconds.

void setup() 
{
  pinMode(A0, INPUT);                              
  pinMode(A1, INPUT); 
  pinMode(A2, INPUT); 
  pinMode(A3, INPUT); 
  for(int x=0; x<=8; x++)      // Set the Triac pin as output
  {  
    pinMode(AC_pin[x], 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);      

                                              
}

void zero_cross_detect() 
{    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  for(int x=0; x<=8; x++)      
  {  
    c[x]=0;
    pinMode(AC_pin[x], LOW);  
  }
}                                 

// Turn on the appropriate TRIAC at the appropriate time
void dim_check() 
{                   
  if(zero_cross == true)    //so a zero_cross was detected
  {
    for(int x=0; x<=8; x++)  //for each dim pin
    {    
        if(c[x]>=dim[x])           //if the time counter is bigger than the dim value
        {                     
          digitalWrite(AC_pin[x], HIGH);  // turn on light       
          c[x]=0;  // reset time step counter                         
        } 
        else 
        {
          c[x]++;  // increment time step counter                     
        }       
    }  
   zero_cross=false;    // reset zero cross detection    
  }                                  
}                                      

void loop() 
{ 
  if(digitalRead(A0) == HIGH && light < 8)
  {
    light++;
  }
  if(digitalRead(A1) == LOW && light > 0)
  {
    light--;
  }
  
  while( dim[light] > 0 || dim[light] < 128 )
  {
    if(digitalRead(A2)==HIGH)
    {
      dim_increase++;
    }
    if(digitalRead(A3)==HIGH)
    {
      dim_decrease++;
    }
    if(dim_decrease>limit)
    {
      dim[light];
      dim_decrease = 0;
    }
    if(dim_increase>limit)
    {
      dim[light];
      dim_increase = 0;
    }
  }
}

Harnam

It's not working

What does that mean, in this context?

What does that mean, in this context?

Sorry for the shortcomings in my question
I have tested a code for single dimmer with this configuration TRC output to pin 11, Dimming UP
pin 13 and Dimming Dn pin12. It works fine.

Now I wish to control 8 dimmers by using Analog pins.
Now the configuration for TRC out put is pins 3 (Dimmer1),4(Dimmer2),5(Dimmer3),6(Dimmer4),
7(Dimmer5),8(Dimmer6),9(Dimmer7) & 10(Dimmer8)
Pin A0 and A1 is for selecting the dimmer (1,2,3,4,5,6,7,8)
Pin A2 and A3 for dimming up and Dn.
I tested all the dimmers one by one , it does not work (No dimming taking place)
Am I in a right track to use these Analog pins ?
Kindly suggest to resolve this problem

Am I in a right track to use these Analog pins ?

You are not using any analog pins. You are using the digital pins on the analog side of the board. There is no problem with that.

Kindly suggest to resolve this problem

Add some Serial.print() statements to the code. Find out whether the switches are wired properly, and incrementing/decrementing light correctly.

      dim[light];

What is this supposed to do? What is really does is nothing.

int AC_pin[9] = {3, 4, 5, 6, 7, 8, 9, 10};

I count 8 pins there.

shouldn't this :

while( dim[light] > 0 || dim[light] < 128 )

be

while( dim[light] > 0 && dim[light] < 128 )

Thanks everyone for the suggestions

Add some Serial.print() statements to the code. Find out whether the switches are wired properly, and incrementing/decrementing light correctly.

I added the serial statements and checked in the serial monitor, It is working fine when I increase and decrease the dim with the help of A2 and A3.The problem lies with the A0 and A1 part, I isolated all the dimmers and tried one by one and by Reset the board . NO LUCK.
How do I proceed further ? Here is my modified sketch.

/*
 AC Light Control
 Zero-cross - pin 2
 Lights dimmers on pins: 3, 4, 5, 6, 7, 8, 9, 10
 Buttons for changing the lights on pins: A0, A1 (up one/down one)
 Buttons for changing the dimm amount: A2, A3 (up level/down level)
  
*/

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

int limit = 1500;               // changing the limit will affect the time that you have to keep the button pressed
int dim_increase = 0;           // this variables are used to lower the dimmer rate
int dim_decrease = 0;           // every time a button is pressed for dimming one of this values increase or decrease untill they reach the above limit... after they are reset to 0.
 
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[9] = {3, 4, 5, 6, 7, 8, 9, 10};   //vector that holds the dimmer pins - whitch will go to triacs           
int dim[9];                     // Dimming level (0-128)  0 = on, 128 = 0ff
int c[9];                       // Counter for each light
int light = 0;                  // The starting/current light pin (0=3)
int freqStep = 78;              // This is the delay-per-brightness step in microseconds.

void setup() 
{
  pinMode(A0, INPUT);           //Setting buttons as inputs                     
  pinMode(A1, INPUT); 
  pinMode(A2, INPUT); 
  pinMode(A3, INPUT); 
  for(int x=0; x<=8; x++)      // Setting all the Triacs pins as outputs
  {  
    pinMode(AC_pin[x], 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);      
Serial.begin(9600);
                                              
}

void zero_cross_detect() 
{    
  zero_cross = true;               // set the boolean to true to tell our dimming function that a zero cross has occured
  for(int x=0; x<=8; x++)      
  {  
    c[x]=0;
    pinMode(AC_pin[x], LOW);       //Setting all dimm pins to LOW
  }
}                                 

// Turn on the appropriate TRIAC at the appropriate time
void dim_check() 
{                   
  if(zero_cross == true)    //so a zero_cross was detected
  {
    for(int x=0; x<=8; x++)  //for each dimm pin
    {    
        if(c[x]>=dim[x])           //if the time counter is bigger than the dim value
        {                     
          digitalWrite(AC_pin[x], HIGH);  // turn on light       
          c[x]=0;  // reset time step counter                         
        } 
        else 
        {
          c[x]++;  // increment time step counter                     
        }       
    }  
   zero_cross=false;    // reset zero cross detection    
  }                                  
}                                      

void loop() 
{ 
  if(digitalRead(A0) == HIGH && light < 8)   //up one light from the button
  {
    light++;
  }
  if(digitalRead(A1) == LOW && light > 0)    //down one light from the button
  {
    light--;
  }
  
  while( dim[light] > 0 && dim[light] < 128 )  //if the dimming level for the appropriate light is betwen 0 and 128
 // while( dim[light] > 0 && dim[light] < 128 )
  {
    if(digitalRead(A2)==HIGH)            //if the button was pressed
    {
      dim_increase++;                    //increase the dim
    }
    if(digitalRead(A3)==HIGH)            //if the button was pressed
    {
      dim_decrease++;                    //decrease the dim
    }
    if(dim_decrease>limit)               //if reached the limit 
    {
      dim[light]--;        //decrease the actual dimm value
      Serial.println(dim[light]);
      dim_decrease = 0;                  //reset the decrease 
    }
    if(dim_increase>limit)               //if reached the limit
    {
      dim[light]++;                      //increase the actual dimm value
       Serial.println(dim[light]);
      dim_increase = 0;                  //reset the increase
    }
  }
}

The problem lies with the A0 and A1 part

but I'm not going to tell you what the problem is.

I isolated all the dimmers and tried one by one and by Reset the board . NO LUCK.

THERE"S NO LUCK INVOLVED. There is skill and logic and hardware, possibly faulty. The LUCK you are talking about is our ability to guess what your problem is.

Hi paulS

THERE"S NO LUCK INVOLVED. There is skill and logic and hardware, possibly faulty. The LUCK you are talking about is our ability to guess what your problem is.

OK I am sorry for mentioning about the LUCK. I have checked the hardware side countless time. It was
very helpful idea about the Serial debug.Thanks.
At least give me some hint to solve this problem

At least give me some hint to solve this problem

What problem? You need to explain what you are doing (I connected switches to pins x and x; I open the serial monitor; I press the switch attached to pin x) and what happens (the serial monitor shows yyyy).

PaulS:
[...]

I isolated all the dimmers and tried one by one and by Reset the board . NO LUCK.

THERE"S NO LUCK INVOLVED. There is skill and logic and hardware, possibly faulty. The LUCK you are talking about is our ability to guess what your problem is.

Ehhh... He probably meant "NO JOY."

There's definitely no joy in debugging :stuck_out_tongue_closed_eyes: