Advertisement
SuperBrainAK

master controll front panel and sound processor, uncleaned

Dec 16th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.13 KB | None | 0 0
  1. /*this teensy++2.0 is controlling my powerbox, 3050, 3090, fans W/PWM, surround processor/EVC ic.
  2.  -takes the temp of 3050/3090 to enable auto fan speed.
  3.  -controlls a LV1116N surround processor&EVC. input and volume work with some gain issues.
  4.  -fan1 is a small computer fan2 is the xbox fans.(no longer connected)remote cox universal aux 0820.
  5.  -uses display board: 1wire buttons, rotary encoder, led's 1-5, no hope for the FLD(no datasheet)
  6.  By: SuperBrainAK
  7.  */
  8. #include <IRremote.h>                                       //includes libraries.
  9. #include <IRremoteInt.h>
  10. #include <Wire.h>
  11. //const pin assignments
  12. const byte irin = 27;
  13. const byte rtncdr1 = 18;
  14. const byte rtncdr2 = 19;
  15. const byte temp = 45;
  16. const byte pbox = 12;
  17. const byte red = 17;
  18. const byte green = 13;
  19. const byte led1 = 25;
  20. const byte led2 = 24;
  21. const byte led3 = 14;
  22. const byte led4 = 15;
  23. const byte led5 = 16;
  24. const byte v5 = 21;
  25. const byte v9 = 23;
  26. const byte fan1 = 26;
  27. //const byte fan2 = 25;
  28. const byte Buttons = 38;                                    //buttons on display board.
  29.  
  30. IRrecv irrecv(irin);                                        //ir recv stuff.
  31. decode_results results;
  32. unsigned long last = millis();                              //updates the time it has been
  33.  
  34. volatile int lastEncoded = 0;                               //rotary encoder stuff.
  35. volatile long ncdrvl = 0;
  36. volatile long lncdrvl = 0;
  37. long lastencoderValue = 0;
  38. int lastMSB = 0;
  39. int lastLSB = 0;
  40. float Map = 10;                                             //map variable = varmax/5.
  41. float ledvl = 0;                                            //decimal for the leds var/Map.
  42. int led = 0;                                                //the pwm state of the pin.
  43. float ledp = 0;                                             //the percent of pwm to the led.
  44.  
  45. boolean volctrl = 0;                                        //will keep controlling the volume.
  46. byte vol = 48;                                              //variable for volume.
  47. byte lvol = 0;                                              //last volume.
  48.  
  49.  
  50. boolean startup = 0;                                        //runs startup sequence if=0.
  51. boolean power = 0;                                          //my main power state.
  52. byte input = 3;                                             //input selection 0=mute 1 = A etc.
  53. byte linput = 0;                                            //last input #.
  54. boolean p5v = 0;                                            //my 5V power state.
  55. boolean p9v = 0;                                            //my 9V power state.
  56.  
  57. boolean spdcntrl = 0;                                       //will keep controlling the fan
  58. boolean start1 = 0;                                         //startup for fan1.
  59. byte speed1 = 0;                                            //speed of fan1.
  60. //byte speed2 = 0;                                            //speed of fan2.
  61. byte lspeed1 = 0;                                           //last speed of fan1.
  62. //byte lspeed2 = 0;                                           //last speed of fan2.
  63.  
  64. int bval = 0;                                               //button value.
  65. int lbval = 0;                                              //last button value.
  66. byte Button = 0;                                            //wich button.
  67. int tval = 0;                                               //temperature value.
  68. int ltval = 0;                                              //last temp. value.
  69.  
  70. void setup () {
  71.   pinMode(pbox, OUTPUT);                                    //pin modes
  72.   pinMode(red, OUTPUT);
  73.   pinMode(green, OUTPUT);
  74.   pinMode(led1, OUTPUT);
  75.   pinMode(led2, OUTPUT);
  76.   pinMode(led3, OUTPUT);
  77.   pinMode(led4, OUTPUT);
  78.   pinMode(led5, OUTPUT);
  79.   pinMode(v5, OUTPUT);
  80.   pinMode(v9, OUTPUT);
  81.   pinMode(fan1, OUTPUT);
  82.   //pinMode(fan2, OUTPUT);
  83.   pinMode(temp, INPUT);
  84.   pinMode(Buttons, INPUT_PULLUP);
  85.   pinMode(rtncdr1, INPUT);
  86.   pinMode(rtncdr2, INPUT);
  87.   attachInterrupt(7, updateEncoder, CHANGE);                //rotary encoder interupts
  88.   attachInterrupt(6, updateEncoder, CHANGE);
  89.   Serial.begin(9600);
  90.   Wire.begin();
  91.   irrecv.enableIRIn();
  92. }
  93.  
  94. void loop() {
  95.   if (!startup){                                            //sets the states of my sensitive outputs.
  96.     digitalWrite(red, HIGH);
  97.     digitalWrite(v5, LOW);
  98.     digitalWrite(v9, LOW);
  99.     Wire.beginTransmission(119);                            //begins transmitting to the chip.
  100.     Wire.send(1);                                           //the sub address for input.
  101.     Wire.send(0);                                           //puts it to mute.
  102.     Wire.endTransmission();                                 //ends the transmission.
  103.     startup = 1;
  104.   }
  105.   if (speed1 != lspeed1){                                   //controls the speed of fan1 if change.
  106.     delay (100);
  107.     lspeed1 = speed1;
  108.     if (speed1 <= 14){                                      //resets start1 if fan1 gets to slow to run.
  109.       start1 = 0;
  110.     }
  111.     if (start1 == 0){                                       //notices when to startup the fan.
  112.       if (37 > speed1 && speed1 > 14){
  113.         start1 = 1;
  114.       }
  115.     }
  116.     if (start1){                                            //starts the fan to access low speeds.
  117.       Serial.println("starting fan1");
  118.       analogWrite(fan1, 70);
  119.       start1 = 2;
  120.       delay (100);
  121.     }
  122.     if (speed1 > 14){
  123.       analogWrite(fan1, speed1);
  124.       Serial.print("fan1 speed is:");                       //says the fan1 speed.
  125.       Serial.println(speed1);
  126.     }
  127.     else {
  128.       Serial.print("fan1 unable to sustain: ");
  129.       Serial.println(speed1);
  130.       analogWrite(fan1, 0);
  131.     }
  132.   }
  133.   /*if (speed2 != lspeed2){                                   //writes the speed of fan2 if change.
  134.    delay (100);
  135.    lspeed2 = speed2;
  136.    analogWrite(fan2, speed2);
  137.    Serial.print("fan2 speed is:");                          //says the fan2 speed.
  138.    Serial.println(speed2);
  139.    }*/
  140.   if (millis() - last > 1000) {                             //only recieves an input every second.
  141.     bval = analogRead(Buttons);                             //updates the button value.
  142.     if (bval >= 300){                                       //no buttons pressed.
  143.       Button = 0;
  144.     }
  145.     if (300 > bval && bval >= 159){
  146.       Button = 6;
  147.     }
  148.     if (159 > bval && bval >= 116){
  149.       Button = 5;
  150.     }
  151.     if (116 > bval && bval >= 81){
  152.       Button = 4;
  153.     }
  154.     if (81 > bval && bval >= 53){
  155.       Button = 1;
  156.     }
  157.     if (53 > bval && bval >= 27){
  158.       Button = 2;
  159.     }
  160.     if (20 > bval){
  161.       Button = 3;
  162.     }
  163.     if (bval != lbval){                                     //if state changes update value.
  164.       Serial.print(Button);
  165.       lbval = bval;
  166.       Serial.print("Button value is:");
  167.       Serial.println(bval);
  168.     }                                                       //end of solid buttons.
  169.     if (irrecv.decode(&results)) {                          //recieves the ir signal.
  170.       if (results.value == 0xd87245ba) {                    //power button.
  171.         Serial.println("power button");
  172.         Button = 1;
  173.       }
  174.       else if (results.value == 0xffc03f){
  175.         Serial.println("display/setup");
  176.       }
  177.       else if (results.value == 0xff807f){
  178.         Serial.println("zoom");
  179.       }
  180.       else if (results.value == 0xff609f){
  181.         Serial.println("SUB");
  182.       }
  183.       else if (results.value == 0xff906f){
  184.         Serial.println("back");
  185.       }
  186.       else if (results.value == 0xfff807){
  187.         Serial.println("skip");
  188.       }
  189.       else if (results.value == 0xffb04f){
  190.         Serial.println("A-B");
  191.       }
  192.       else if (results.value == 0xffa857){
  193.         Serial.println("1/all");
  194.       }
  195.       else if (results.value == 0xd872748b){
  196.         Serial.println("up");
  197.         speed1 += 5;                                        //fan1 speed +5.
  198.       }
  199.       else if (results.value == 0xd872b44b){
  200.         Serial.println("down");
  201.         speed1 -= 5;                                        //fan1 speed -5.
  202.       }
  203.       else if (results.value == 0xd872f807){
  204.         Serial.println("left");
  205.         //speed2 -= 5;                                        //fan2 speed -5.
  206.       }
  207.       else if (results.value == 0xd87204fb){
  208.         Serial.println("right");
  209.         //speed2 += 5;                                        //fan2 speed +5.
  210.       }
  211.       else if (results.value == 0xd8720cf3){
  212.         Serial.println("select");
  213.       }
  214.       else if (results.value == 0xffe817){
  215.         Serial.println("play/pause");
  216.       }
  217.       else if (results.value == 0xff6897){
  218.         Serial.println("stop");
  219.       }
  220.       else if (results.value == 0xffb24d){
  221.         Serial.println("menu");
  222.       }
  223.       else if (results.value == 0xd872649b){                //cycle inputs.
  224.         Serial.println("input");
  225.         input += 1;
  226.       }
  227.       else if (results.value == 0xff58a7){
  228.         Serial.println("angle");
  229.       }
  230.       else if (results.value == 0xff40bf){
  231.         Serial.println("lcd mode");
  232.       }
  233.       else if (results.value == 0xffa05f){
  234.         Serial.println("title");
  235.       }
  236.       else if (results.value == 0xd872d02f){                //input1 selection.
  237.         Serial.println("1");
  238.         input = 1;
  239.       }
  240.       else if (results.value == 0xd872906f){                //input2 selection.
  241.         Serial.println("2");
  242.         input = 2;
  243.       }
  244.       else if (results.value == 0xd872f00f){                //input3 selection.
  245.         Serial.println("3");
  246.         input = 3;
  247.       }
  248.       else if (results.value == 0xd872b04f){
  249.         Serial.println("4");
  250.       }
  251.       else if (results.value == 0xd87252ad){                //5v power button.
  252.         Serial.println("5");
  253.         Button = 3;
  254.       }
  255.       else if (results.value == 0xd872d02f){
  256.         Serial.println("6");
  257.       }
  258.       else if (results.value == 0xd872708f){
  259.         Serial.println("7");
  260.       }
  261.       else if (results.value == 0xd872609f){
  262.         Serial.println("8");
  263.       }
  264.       else if (results.value == 0xd872a05f){                //9Vpower button.
  265.         Serial.println("9");
  266.         Button = 2;
  267.       }
  268.       else if (results.value == 0xd87240bf){
  269.         Serial.println("0");
  270.       }
  271.       else {                                                //tells you any unknown signal.
  272.         if (results.decode_type == NEC) {
  273.           Serial.print("Decoded NEC: ");
  274.         }
  275.         else if (results.decode_type == SONY) {
  276.           Serial.print("Decoded SONY: ");
  277.         }
  278.         else if (results.decode_type == RC5) {
  279.           Serial.print("Decoded RC5: ");
  280.         }
  281.         else if (results.decode_type == RC6) {
  282.           Serial.print("Decoded RC6: ");
  283.         }
  284.         Serial.println(results.value, HEX);
  285.       }
  286.     }
  287.     irrecv.resume();
  288.     tval = analogRead(temp);                                //updates the temperature.
  289.     if (tval > 450){                                        //cold temperature turns fan off if not already off.
  290.       if (speed1 != 0){
  291.         speed1 = 0;
  292.       }
  293.     }
  294.     if (450 >= tval && tval >= 300){                        //medium temperature auto equalize the fan.
  295.       if (tval > ltval){                                    //if the temp. falls slow down the fan.
  296.         speed1 --;
  297.       }
  298.       if (tval < ltval){
  299.         speed1 ++;                                          //if the temp. rises speed up the fan.
  300.       }
  301.     }
  302.     if (tval < 300){                                        //rather hot turns fan to max.
  303.       speed1 = 70;
  304.     }
  305.     if (tval < 250){                                        //hot and hotter tells you so.
  306.       Serial.println("heat sink is hot!");
  307.       digitalWrite(red, HIGH);
  308.       digitalWrite(green, HIGH);
  309.     }
  310.     if (tval != ltval){                                     //if temperature changes update value.
  311.       Serial.print(ltval);
  312.       ltval = tval;
  313.       Serial.print("temp value is:");
  314.       Serial.println(tval);
  315.     }
  316.     if (Button == 1){                                       //toggles my power outputs.
  317.       power = !power;
  318.       Serial.print("power: ");
  319.       (power) ? Serial.println("on") : Serial.println("off");
  320.       digitalWrite(pbox, power ? HIGH : LOW);
  321.       digitalWrite(green, power ? HIGH : LOW);
  322.       digitalWrite(red, power ? LOW : HIGH);
  323.       digitalWrite(v5, power ? HIGH : LOW);
  324.       digitalWrite(v9, power ? HIGH : LOW);
  325.       p5v = power;
  326.       p9v = power;
  327.       input = power;
  328.     }
  329.     if (Button == 2){                                       //toggles my 9V output.
  330.       p9v = !p9v;
  331.       Serial.print("9v power: ");
  332.       (p9v) ? Serial.println("on") : Serial.println("off");
  333.       digitalWrite(v9, p9v ? HIGH : LOW);
  334.     }
  335.     if (Button == 3){                                       //toggles my 5V output.
  336.       p5v = !p5v;
  337.       Serial.print("5v power: ");
  338.       (p5v) ? Serial.println("on") : Serial.println("off");
  339.       digitalWrite(v5, p5v ? HIGH : LOW);
  340.     }
  341.     if (Button == 4){                                       //toggles controll for fan1.
  342.       spdcntrl = !spdcntrl;
  343.       Serial.print("control fan speed: ");
  344.       (spdcntrl) ? Serial.println("yes") : Serial.println("no");
  345.       if (spdcntrl){
  346.         Map = 70/5;                                         //creats the map for speed.
  347.       }
  348.       else {
  349.         Map = 10;                                           //resets the map #.
  350.       }
  351.     }
  352.     if (Button == 5){                                       //cycles my inputs.
  353.       input ++;
  354.     }
  355.     if (Button == 6){                                       //toggles controll for v1.
  356.       volctrl = !volctrl;
  357.       Serial.print("control volume: ");
  358.       (volctrl) ? Serial.println("yes") : Serial.println("no");
  359.       if (volctrl){
  360.         Map = 48/5;                                         //creats the map for volume.
  361.       }
  362.       else {
  363.         Map = 10;                                           //resets the map #.
  364.       }
  365.     }
  366.     last = millis();
  367.   }                                                         //end of every second things.
  368.   /*if (digitalRead(Button6) == LOW){
  369.    speed2 -= 1;
  370.    delay (100);
  371.    }
  372.    if (digitalRead(Button7) == LOW){
  373.    speed2 += 1;
  374.    delay (100);
  375.    }*/
  376.   if (input > 3){                                         //loops the inputs.
  377.     input = 0;
  378.   }
  379.   if (lncdrvl != ncdrvl){                                    //handles the variables and led graph.
  380.     if (volctrl == 1){
  381.       if (ncdrvl > lncdrvl){
  382.         vol ++;
  383.       }
  384.       if (ncdrvl < lncdrvl){
  385.         vol --;
  386.       }
  387.       if (vol < 0){                                          //limits range of volume.
  388.         Serial.println("volume low");
  389.         vol = 0;
  390.       }
  391.       if (vol > 48){
  392.         Serial.println("volume high");
  393.         vol = 48;
  394.       }
  395.       ledvl = vol/Map;
  396.       Serial.print("volume is: ");
  397.       Serial.println(vol);
  398.     }
  399.     if (spdcntrl == 1){
  400.       if (ncdrvl > lncdrvl){
  401.         speed1 ++;
  402.       }
  403.       if (ncdrvl < lncdrvl){
  404.         speed1 --;
  405.       }
  406.       if (speed1 < 0){                                          //limits range of fan1.
  407.         Serial.println("fan1 low");
  408.         speed1 = 0;
  409.       }
  410.       if (speed1 > 70){
  411.         Serial.println("fan1 high");
  412.         speed1 = 70;
  413.       }
  414.       ledvl = speed1/Map;
  415.       Serial.print("speed is: ");
  416.       Serial.println(speed1);
  417.     }
  418.     if (ledvl == 0){
  419.       digitalWrite(led1, LOW);
  420.       digitalWrite(led2, LOW);
  421.       digitalWrite(led3, LOW);
  422.       digitalWrite(led4, LOW);
  423.       digitalWrite(led5, LOW);
  424.     }
  425.     if (1 > ledvl && ledvl > 0){
  426.       led = ledvl * 255;
  427.       analogWrite(led1, led);
  428.     }
  429.     if (ledvl >= 1){
  430.       digitalWrite(led1, HIGH);
  431.       digitalWrite(led2, LOW);
  432.     }
  433.     if (2 > ledvl && ledvl > 1){
  434.       ledp = ledvl - 1;
  435.       led = ledp * 255;
  436.       analogWrite(led2, led);
  437.     }
  438.     if (ledvl >= 2){
  439.       digitalWrite(led2, HIGH);
  440.       digitalWrite(led3, LOW);
  441.     }
  442.     if (3 > ledvl && ledvl > 2){
  443.       ledp = ledvl - 2;
  444.       led = ledp * 255;
  445.       analogWrite(led3, led);
  446.     }
  447.     if (ledvl >= 3){
  448.       digitalWrite(led3, HIGH);
  449.       digitalWrite(led4, LOW);
  450.     }
  451.     if (4 > ledvl && ledvl > 3){
  452.       ledp = ledvl - 3;
  453.       led = ledp * 255;
  454.       analogWrite(led4, led);
  455.     }
  456.     if (ledvl >= 4){
  457.       digitalWrite(led4, HIGH);
  458.       digitalWrite(led5, LOW);
  459.     }
  460.     if (5 > ledvl && ledvl > 4){
  461.       ledp = ledvl - 4;
  462.       led = ledp * 255;
  463.       analogWrite(led5, led);
  464.     }
  465.     if (ledvl == 5){
  466.       digitalWrite(led5, HIGH);
  467.     }
  468.     Serial.print("ledvalue is: ");
  469.     Serial.println(ledvl);
  470.     lncdrvl = ncdrvl;
  471.   }
  472.   if (input != linput){                                        //sees if it needs to change the input.
  473.     Wire.beginTransmission(119);
  474.     Wire.send(1);                                              //the sub address for input.
  475.     Wire.send(input);                                          //sets the input.
  476.     Wire.endTransmission();
  477.     linput = input;
  478.     Serial.print("input is: ");                                //says the input value.
  479.     Serial.println(input);
  480.   }
  481.   if (vol != lvol){                                            //sees if it needs to change the volume.
  482.     Wire.beginTransmission(119);
  483.     Wire.send(2);                                              //the sub address for volume.
  484.     Wire.send(vol + 192);                                      //sets the volume(the 192 comes from controlling both channels)
  485.     Wire.endTransmission();
  486.     lvol = vol;
  487.     Serial.print("volume is: ");                               //says the volume.
  488.     Serial.println(vol);
  489.   }
  490. }
  491.  
  492. void updateEncoder(){                                      
  493.   int MSB = digitalRead(rtncdr1); //MSB = most significant bit
  494.   int LSB = digitalRead(rtncdr2); //LSB = least significant bit
  495.  
  496.   int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  497.   int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  498.  
  499.   if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) ncdrvl ++;
  500.   if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) ncdrvl --;
  501.  
  502.   lastEncoded = encoded; //store this value for next time
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement