Advertisement
SuperBrainAK

master controll dvd board (before i2c)

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