dimmer_code

//Below is the commented code for the IGBT light dimmer.  It was written to run on an Arduino Uno with the setup specified in the schematic section of this wiki.
/*
IGBT Light Dimmer Aaron, Kyle, Mike, Nik
 
 */
 
int pot=A0; //input from pot
int potval10=0;  //10 bit input from adc
int potval9=B0; //8 bit val
int clk=2;  //refer to DAC chip pins
int data=3; //refer to DAC chip pins
int load=4; //refer to DAC chip pins
int ldac=5; //refer to DAC chip pins
int hilo=6; //refer to DAC chip pins
int bintemp=B0; //post mask value
int bitmask=B10000000; //mask start state
int hilomode=B0; // to determine what hilow mode it is
int modeswitch1 = 7; // digital pin 7
int modeswitch2 = 8; // digital pin 8
int zero=A1; //input from zero cross
 
void setup() {               
  // initialize the digital pin as an output.
  pinMode(2, OUTPUT);    //clock
  pinMode(3, OUTPUT);    //data
  pinMode(4, OUTPUT);    //load
  pinMode(5, OUTPUT);    //ldac
  pinMode(6, OUTPUT);    //hilo
  pinMode(7, INPUT);     //modeswitch1
  pinMode(8, INPUT);     //modeswitch2
 
}
void loop() {
digitalWrite(clk,HIGH);
digitalWrite(data,LOW);
digitalWrite(load,HIGH); 
digitalWrite(ldac,HIGH); 
bitmask=B10000000;
bintemp=B00000000;
 
potval10=analogRead(pot);
 
// mode switch check
if (digitalRead(modeswitch1) == HIGH && digitalRead(modeswitch2) == LOW) {  // low light = opposite function
  potval10 = (1023 - potval10);
} else if (digitalRead(modeswitch1) == LOW && digitalRead(modeswitch2) == HIGH) { // "cutoff" mode... turn all the way on at voltage threshold
  if (potval10 >= 487) {
    potval10 = 1023;
  } else {
    potval10 = 0;
  }
} else {
  // do nothing!
}
//if (digitalRead(modeswitch1) == HIGH && digitalRead(modeswitch2) == HIGH) // Normal Mode
  //{potval10=potval10;)
 
 
potval9=unsigned(potval10) >> 1; //shifts 10 bit number to the right 1 so that the 1 lsb is lost and it is 9 bit
 
for (int countadd=0; countadd<3; countadd++) //sets address to 000
{digitalWrite(clk,HIGH);
digitalWrite(data,LOW);
digitalWrite(clk,LOW);}
 
digitalWrite(clk,HIGH);
digitalWrite(data,HIGH); //sets range to high
digitalWrite(clk,LOW);
 
 
//hilo compute
if (potval9<=255)
{hilomode=0;
potval9=potval9-5;//calibration
if (potval9<0)
{potval9=0;}
}
else
{hilomode=1;
potval9=(potval9-256);//subtracts 256 from number >256 (so that range is set and DAC gets valid values
potval9=potval9+180;//calibration
if (potval9>255)
{potval9=255;}}
//end hilo compute
 
potval9=unsigned(potval9) >> 1; //shifts 9 bit number to the right 1 so that the 1 lsb is lost and it is 8 bit
//potval9 = ~potval9; //invert for backwards functioning transistor
 
//DAC level output
for (int countadd=0; countadd<8; countadd++) //sets outputs for 8 bit string for voltage out based upon 8 bit potval
  {bintemp=bitmask&potval9; //uses mask to determine if a particular bit is 1 or 0
  digitalWrite(clk,HIGH);
 
    if (bintemp>B0) //if masked bit is 1
      {digitalWrite(data,HIGH);}
    else
      {digitalWrite(data,LOW);}
    digitalWrite(clk,LOW);
    bitmask=unsigned(bitmask) >>1;}
//end DAC level ouput 
 
digitalWrite(load,LOW);//sends output to register
digitalWrite(load,HIGH);
 
zerodetect:
if (analogRead(zero)<=B001)//makes sure we only switch on zero-cross for triac
{
//FINAL OUTPUT STAGE
 
digitalWrite(ldac,LOW); //enables output
digitalWrite(ldac,HIGH);
//hilo set
if (hilomode==B1)//outputs based on mode calculation from above
{delay(1);//flicker reduction
else
{digitalWrite(hilo,LOW);
}
//end hilo set
// END FINAL OUTPUT STAGE
 
}
else
{goto zerodetect;}//goes into loop until zero-cross is detected.
}

digitalWrite(hilo,HIGH);}