Teensy and Inmijo Dimmer

Hi folks

Below is some code a colleague wrote whom now no longer works with us and has gone radio silent. Its is a teensy controlled sound sensitive light dimmer with a switch using the inmojo AC dimmer. The unit has three states, ‘Full -on’, ‘Dimming’ (sound sensitive) and ‘Full off’… each press of the switch cycles through each of the states

I need to change the code so that it only has 2 states ‘Full-on’ and ‘Dimming’ and i’m afraid to say i cant figure it out for the life of me…

I know it can’t be too hard but i’m a bit of a basic arduino user… Any help would be greatly appreciated.

Thanks.

FTL

/*

 AC sound to Light Dimmer
   AC Light Dimmer v.2 - Inmojo 
   AC Voltage dimmer with Zero cross detection
   
   Author: Charith Fernanado http://www.inmojo.com charith@inmojo.com 
   License: Released under the Creative Commons Attribution Share-Alike 3.0 License. 
   http://creativecommons.org/licenses/by-sa/3.0
 
 Target:  Teensy
 
 Instructions:
 Attach the Zero cross pin of the module to Teensy Interrupt pin 2
 Attach the Load control to pin 3
 
 To cycle states attach a wire from one leg of a push to make switch
 to ground ( and a wire from the other to pin 4. Solder to the veroboard 
 not the teensy itself.
 
 */
 
 
#define  fullOn    10
#define  fullOff   127
#define  FQ_50      1 // in case of 50Hz
#define  FQ_60      0 // in case of 50Hz
#define  VER       "2.0"

int inbyte;
int ZCR = 2;
int AC_LOAD = 3;    // Output to Opto Triac pin
int SWITCH = 4;
int dimming = fullOff;  // Dimming level (0-128)  0 = ON, 128 = OFF

boolean frq = FQ_50;     // change the frequency here. 
boolean countUp = false;
boolean simulate = true; 

volatile int switchState = 0;
volatile long switchTime = 0;

void setup()
{
  pinMode(ZCR, INPUT);
  pinMode(AC_LOAD, OUTPUT);	      // Set the AC Load as output
  pinMode(SWITCH, INPUT_PULLUP);
  attachInterrupt(ZCR, zero_crosss_int, RISING);  // Choose the zero cross interrupt # from the table above
  attachInterrupt(SWITCH, changeState, FALLING);
  Serial.begin(115200);
}

void zero_crosss_int()  // function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation
  // 50Hz-> 10ms (1/2 Cycle) → (10000us - 10us) / 128 = 78 (Approx)
  // 60Hz-> 8.33ms (1/2 Cycle) → (8333us - 8.33us) / 128 = 65 (Approx)
  int dimtime = 0;
  float propTime = 0;
  if(frq){
    dimtime = (77*dimming);
    propTime = 10.0; 
  }
  else{
    dimtime = (65*dimming);
    propTime = 8.33; 
  }
  delayMicroseconds(dimtime);    // Off cycle
  digitalWrite(AC_LOAD, HIGH);   // triac firing
  delayMicroseconds(propTime);         // triac On propogation delay
  digitalWrite(AC_LOAD, LOW);    // triac Off
  //Serial.println("h");

}

void changeState()
{
  if (millis() - switchTime > 100) {
    switchState++;
    switchTime = millis();
    if (switchState==3)
    {
      switchState = 0;
    }
  }
}

void loop()
{
  /*
  if(simulate){
    if(countUp)
      dimming++; 
    else
      dimming--;
  }

  if(dimming < fullOn)
    countUp = true;
  else if (dimming > fullOff)
    countUp = false;  
  */
  int sensorValue = analogRead(A0);
  int mapped = map(sensorValue, 0, 100, fullOff, fullOn);
  
  switch(switchState) {
    case 0:
      
      dimming = constrain(mapped, fullOn, fullOff);
      
      Serial.println(dimming);  
     
      delay(20);
      break;
    case 1:
      dimming = fullOn;
      Serial.println(dimming);  
      delay(20);
      break;
    case 2:
      dimming = fullOff;
      Serial.println(dimming);  
      delay(20);
      break;
  }
    
  /*
  if (Serial.available() > 0)
    _serial_int();
  */
  
}



void displayMenu() {
  Serial.println(" -------- InMojo Digital Dimmer v.2 | inmojo.com -------- ");
  Serial.println("");
  Serial.println("[m] Menu");
  Serial.println("[d] Simulate Diming");
  Serial.println("[1] Turn ON Light");
  Serial.println("[0] Turn OFF Light");
  Serial.println("[q] Query Dimming Amount");
  Serial.println("[v] Version");
  Serial.println("");
}



void _serial_int(){
  while (Serial.available() > 0) {
    inbyte = Serial.read();

    switch (inbyte) {
    case 'd':
      attachInterrupt(ZCR, zero_crosss_int, RISING);
      if(simulate)
        simulate = false;
      else
        simulate = true;
      break;
    case 'm':
      displayMenu(); 
      break;
    case '1':
      simulate = false;
      detachInterrupt(0); 
      digitalWrite(AC_LOAD, HIGH); 
      break;      
    case '0':
      simulate = false;
      detachInterrupt(0); 
      digitalWrite(AC_LOAD, LOW); 
      break;   
    case 'q':
      Serial.print("Dimming: "); 
      Serial.println(dimming);
      break;        
    case 'v':
      Serial.println(VER);
      break;
    }    

  }
}

No one? :frowning:

It's it as simple as change 3 to 2 in this line?

    if (switchState==3)

Thank you so much!!!

FTL

fasterthanlight:
Thank you so much!!!

For responding or giving the correct answer?

Initially for responding, but now i have tested the edited code and it is working... so twice thank you sir!!

FTL