Advertisement
SuperBrainAK

master controll 0_4_9 accurate resistance, better overheat

May 8th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.32 KB | None | 0 0
  1. /*this is controlling my powerbox, 3050, 3090 and fans W/PWM.takes the temp of 3050/3090 to enable auto fan speed.
  2.  fan1 is a small computer fan, fan2 is the xbox fans.(no longer connected)remote cox universal aux 0820.
  3.  monitors the voltage and current has a selector for high current and low current (changes the resistance to calculate with) can constant current charge a battery with different voltages and different amperages will slow down/turn off once voltage is reached.
  4.  ABSOLUTE MAX OF 21.5v ON ALL INPUTS! now more accurate.
  5.  button panel is a direct tv satellite box
  6.  put together by SuperBrainAK
  7.  */
  8. #include <IRremote.h>                                       //includes libraries.
  9. //const pin assignments
  10. const byte irin = 27;
  11. const byte temp = 45;
  12. const byte vlts = 38;
  13. const byte ain = 39;
  14. const byte aout = 40;
  15. const byte pbox = 0;
  16. const byte red = 17;
  17. const byte green = 13;
  18. const byte v5 = 21;
  19. const byte v9 = 22;
  20. const byte fan1 = 26;
  21. //const byte fan2 = 25;
  22. const byte batchrg = 25;
  23. const byte Button1 = 1;                                     //power (power)
  24. const byte Button2 = 2;                                     //5v power (guide)
  25. const byte Button3 = 3;                                     //9v power (menu)
  26. const byte Button4 = 4;                                     //change amperage resistance (active)
  27. const byte Button5 = 5;                                     //raise the output voltage (up)
  28. const byte Button6 = 7;                                     //lower the output voltage (down)
  29. const byte Button7 = 8;                                     //reset the current and voltage (select)
  30. const byte Button8 = 9;                                     //raises the output current (right)
  31. const byte Button9 = 10;                                    //lowers the output current (left)
  32.  
  33.  
  34. IRrecv irrecv(irin);                                        //ir recv stuff.
  35. decode_results results;
  36.  
  37. int16_t adc_read_vcc(){
  38.   //"reset" the ADMUX
  39.   ADMUX = 0b01000000;
  40.  
  41.   ADMUX |=(1<<REFS0) | (1<<MUX4) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1);
  42.   //REFS0 - VCC as reference
  43.   //mux - measurment on the 1.11V internal
  44.  
  45.   ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);//ADEN - enable ADC. ADPS - 1,1,1 = F_CPU/128 prescalar (should work for 8MHz - 16MHz)
  46.  
  47.   delay(5);//ehh...
  48.  
  49.   ADCSRA|=(1<<ADSC);//we start conversion
  50.  
  51.   while(!(ADCSRA & (1<<ADIF)));//Wait for conversion to complete
  52.  
  53.   ADCSRA|=(1<<ADIF);//just making sure it's 0
  54.  
  55.   return (1125300L/ADC);//to get it in mV (to not to loose to many decimals) - (1100mV*1023)
  56. }
  57.  
  58. void setup () {
  59.   pinMode(pbox, OUTPUT);                                    //pin modes
  60.   pinMode(red, OUTPUT);
  61.   pinMode(green, OUTPUT);
  62.   pinMode(v5, OUTPUT);
  63.   pinMode(v9, OUTPUT);
  64.   pinMode(fan1, OUTPUT);
  65.   //pinMode(fan2, OUTPUT);
  66.   pinMode(batchrg, OUTPUT);
  67.   pinMode(temp, INPUT);
  68.   pinMode(vlts, INPUT);
  69.   pinMode(ain, INPUT);
  70.   pinMode(aout, INPUT);
  71.   pinMode(Button1, INPUT);
  72.   pinMode(Button2, INPUT);
  73.   pinMode(Button3, INPUT);
  74.   pinMode(Button4, INPUT);
  75.   pinMode(Button5, INPUT);
  76.   pinMode(Button6, INPUT);
  77.   pinMode(Button7, INPUT);
  78.   pinMode(Button8, INPUT);
  79.   pinMode(Button9, INPUT);
  80.   Serial.begin(9600);
  81.   irrecv.enableIRIn();
  82. }
  83. unsigned long last = millis();                              //declares millis var?
  84. byte startup = 0;                                           //runs startup sequence if=0.
  85. byte power = 0;                                             //main power state.
  86. byte p5v = 0;                                               //5V power state.
  87. byte p9v = 0;                                               //9V power state.
  88. byte start1 = 0;                                            //startup for fan1.
  89. int speed1 = 0;                                             //speed of fan1.
  90. //int speed2 = 0;                                             //speed of fan2.
  91. int lspeed1 = 0;                                            //last speed of fan1.
  92. //int lspeed2 = 0;                                            //last speed of fan2.
  93. int tval = 0;                                               //temperature value.
  94. int ltval = 0;                                              //last temperature value.
  95. float Vcc = 0;                                              //stores the Vcc voltage. in mV
  96. float volts = 0;                                            //voltage metering.
  97. float vampin = 0;                                           //voltage before the current resistor.
  98. float vampout = 0;                                          //voltage after the current resistor.
  99. byte amp = 0;                                               //0=low current, 1=high current.
  100. float amps = 0;                                             //calculated amps.
  101. float crntlmt = 0;                                          //max current the transistor will source.
  102. float vltlmt = 0;                                           //max voltage the transistor will get to.
  103. int chrgrte = 0;                                            //PWM value for the PNP to a NPN charging transistor.
  104. int lchrgrte = 1;
  105.  
  106. void loop() {
  107.   if (startup == 0){                                        //sets the states of my sensitive outputs.
  108.     digitalWrite(red, HIGH);
  109.     digitalWrite(pbox, HIGH);
  110.     digitalWrite(v5, LOW);
  111.     digitalWrite(v9, LOW);
  112.     startup = 1;
  113.   }
  114.   byte pbutton = 0;                                         //temporary power variable state.
  115.   byte p5 = 0;                                              //temporary 5V variable state.
  116.   byte p9 = 0;                                              //temporary 9V variable state.
  117.   byte crnt = 0;                                            //temporary current variable state.
  118.   if (millis() - last > 1000) {                             //only recieves an input every second.
  119.     if (irrecv.decode(&results)) {                          //recieves the ir signal.
  120.       if (results.value == 0xd87245ba) {                    //power button.
  121.         Serial.println("power button");
  122.         pbutton = !pbutton;
  123.       }
  124.       else if (results.value == 0xffc03f){
  125.         Serial.println("display/setup");
  126.       }
  127.       else if (results.value == 0xff807f){
  128.         Serial.println("zoom");
  129.       }
  130.       else if (results.value == 0xff609f){
  131.         Serial.println("SUB");
  132.       }
  133.       else if (results.value == 0xff906f){
  134.         Serial.println("back");
  135.       }
  136.       else if (results.value == 0xfff807){
  137.         Serial.println("skip");
  138.       }
  139.       else if (results.value == 0xffb04f){
  140.         Serial.println("A-B");
  141.       }
  142.       else if (results.value == 0xffa857){                  //changes the current ranges.
  143.         Serial.println("1/all");
  144.         crnt = !crnt;
  145.       }
  146.       else if (results.value == 0xd872748b){
  147.         Serial.println("up");
  148.         speed1 += 5;                                        //fan1 speed +5.
  149.       }
  150.       else if (results.value == 0xd872b44b){
  151.         Serial.println("down");
  152.         speed1 -= 5;                                        //fan1 speed -5.
  153.       }
  154.       else if (results.value == 0xd872f807){
  155.         Serial.println("left");
  156.         //speed2 -= 5;                                        //fan2 speed -5.
  157.       }
  158.       else if (results.value == 0xd87204fb){
  159.         Serial.println("right");
  160.         //speed2 += 5;                                        //fan2 speed +5.
  161.       }
  162.       else if (results.value == 0xd8720cf3){
  163.         Serial.println("select");
  164.       }
  165.       else if (results.value == 0xffe817){
  166.         Serial.println("play/pause");
  167.       }
  168.       else if (results.value == 0xff6897){
  169.         Serial.println("stop");
  170.       }
  171.       else if (results.value == 0xffb24d){
  172.         Serial.println("menu");
  173.       }
  174.       else if (results.value == 0xd872649b){
  175.         Serial.println("input");
  176.       }
  177.       else if (results.value == 0xff58a7){
  178.         Serial.println("angle");
  179.       }
  180.       else if (results.value == 0xff40bf){
  181.         Serial.println("lcd mode");
  182.       }
  183.       else if (results.value == 0xffa05f){
  184.         Serial.println("title");
  185.       }
  186.       else if (results.value == 0xd872d02f){
  187.         Serial.println("1");
  188.       }
  189.       else if (results.value == 0xd872906f){
  190.         Serial.println("2");
  191.       }
  192.       else if (results.value == 0xd872f00f){
  193.         Serial.println("3");
  194.       }
  195.       else if (results.value == 0xd872b04f){
  196.         Serial.println("4");
  197.       }
  198.       else if (results.value == 0xd87252ad){                  //5v power button.
  199.         Serial.println("5");
  200.         p5 = !p5;
  201.       }
  202.       else if (results.value == 0xd872d02f){
  203.         Serial.println("6");
  204.       }
  205.       else if (results.value == 0xd872708f){
  206.         Serial.println("7");
  207.       }
  208.       else if (results.value == 0xd872609f){
  209.         Serial.println("8");
  210.       }
  211.       else if (results.value == 0xd872a05f){                  //9v power button.
  212.         Serial.println("9");
  213.         p9 = !p9;
  214.       }
  215.       else if (results.value == 0xd87240bf){
  216.         Serial.println("0");
  217.       }
  218.       else {                                                  //tells you any unknown signal.
  219.         if (results.decode_type == NEC) {
  220.           Serial.print("Decoded NEC: ");
  221.         }
  222.         else if (results.decode_type == SONY) {
  223.           Serial.print("Decoded SONY: ");
  224.         }
  225.         else if (results.decode_type == RC5) {
  226.           Serial.print("Decoded RC5: ");
  227.         }
  228.         else if (results.decode_type == RC6) {
  229.           Serial.print("Decoded RC6: ");
  230.         }
  231.         Serial.println(results.value, HEX);
  232.       }
  233.     }
  234.     irrecv.resume();
  235.     if (digitalRead(Button1) == LOW){                         //reads the button1 state.
  236.       Serial.println("button1");
  237.       pbutton = !pbutton;
  238.     }
  239.     if (digitalRead(Button2) == LOW){                         //reads the button2 state.
  240.       Serial.println("button2");
  241.       p5 = !p5;
  242.     }
  243.     if (digitalRead(Button3) == LOW){                         //reads the button3 state.
  244.       Serial.println("button3");
  245.       p9 = !p9;
  246.     }
  247.     if (digitalRead(Button4) == LOW){                         //reads the button4 state.
  248.       Serial.println("button4");
  249.       crnt = !crnt;
  250.     }
  251.     if (digitalRead(Button5) == LOW){                         //reads the button4 state.
  252.       Serial.println("button5: volt limit up");
  253.       vltlmt += .1;
  254.     }
  255.     if (digitalRead(Button6) == LOW){                         //reads the button4 state.
  256.         Serial.println("button6: volt limit dowm");
  257.         vltlmt -= .1;
  258.     }
  259.     if (digitalRead(Button7) == LOW){                         //reads the button4 state.
  260.       Serial.println("button7: volt and current limits reset");
  261.       vltlmt = 0;                                             //shuts off the voltage and current parameters.
  262.       crntlmt = 0;
  263.     }
  264.     if (digitalRead(Button8) == LOW){                         //reads the button4 state.
  265.       Serial.println("button8: current limit up");
  266.       crntlmt += .1;
  267.     }
  268.     if (digitalRead(Button9) == LOW){                         //reads the button4 state.
  269.       Serial.println("button9: current limit down");
  270.       crntlmt -= .1;
  271.     }
  272.     tval = analogRead(temp);                                  //updates the temperature.
  273.     if (tval > 450){                                          //cold temperature turns fan off if not already off.
  274.       if (speed1 != 0){
  275.         speed1 = 0;
  276.       }
  277.     }
  278.     if (450 >= tval && tval >= 300){                          //medium temperature auto equalize the fan.
  279.       if (tval > ltval){                                      //if the temp. falls slow down the fan.
  280.         speed1 --;
  281.       }
  282.       if (tval < ltval){
  283.         speed1 ++;                                            //if the temp. rises speed up the fan.
  284.       }
  285.     }
  286.     if (tval < 300){                                          //rather hot turns fan to max.
  287.       speed1 = 70;
  288.     }
  289.     if (tval < 250){                                          //hot and hotter tells you so.
  290.       Serial.println("heat sink is hot!");
  291.       digitalWrite(red, HIGH);
  292.       digitalWrite(green, HIGH);
  293.       p5v = 1 , p5 = 1 , p9v = 1 , p9 = 1;                    //turns off the regulators.
  294.       chrgrte = 0;                                            //stops any charging.
  295.       crntlmt = -0.1                                          //resets the current limit.
  296.       speed1 = 70;                                            //makes sure the fan is running.
  297.     }
  298.     Serial.print("temp value is:");                           //says the temp value.
  299.     ltval = tval;                                             //updates the last temperature.
  300.     Serial.println(tval);
  301.     if (pbutton == 1){                                        //toggles my power outputs.
  302.       Serial.println("power");
  303.       power = !power;
  304.       if (power){
  305.         Serial.println("on");
  306.       }
  307.       else {
  308.         Serial.println("off");
  309.       }
  310.       digitalWrite(pbox, power ? LOW : HIGH);
  311.       digitalWrite(green, power ? HIGH : LOW);
  312.       digitalWrite(v5, power ? HIGH : LOW);
  313.       digitalWrite(v9, power ? HIGH : LOW);
  314.       digitalWrite(red, power ? LOW : HIGH);
  315.     }
  316.     if (p5 == 1){                                             //toggles my 5V output.
  317.       Serial.println("5v power");
  318.       p5v = !p5v;
  319.       if (p5v){
  320.         Serial.println("on");
  321.       }
  322.       else {
  323.         Serial.println("off");
  324.       }
  325.       digitalWrite(v5, p5v ? HIGH : LOW);
  326.     }
  327.     if (p9 == 1){                                             //toggles my 9V output.
  328.       Serial.println("9v power");
  329.       p9v = !p9v;
  330.       if (p9v){
  331.         Serial.println("on");
  332.       }
  333.       else {
  334.         Serial.println("off");
  335.       }
  336.       digitalWrite(v9, p9v ? HIGH : LOW);
  337.     }
  338.     if (crnt == 1){                                           //changes the resistance to use for current calculation.
  339.       Serial.println("changed resistance");
  340.       amp = !amp;
  341.     }
  342.     if (speed1 < 0){                                          //limits range of speeds.
  343.       Serial.println("fan1 low");
  344.       speed1 = 0;
  345.     }
  346.     if (speed1 > 70){
  347.       Serial.println("fan1 high");
  348.       speed1 = 70;
  349.     }
  350.     /*if (speed2 < 0){
  351.      Serial.println("fan2 low");
  352.      speed2 = 0;
  353.      }
  354.      if (speed2 > 100){
  355.      Serial.println("fan2 high");
  356.      speed2 = 100;
  357.      }*/
  358.     if (speed1 != lspeed1){                                     //writes the speed of fan1 if change.
  359.       lspeed1 = speed1;
  360.       if (speed1 <= 14){                                        //puts start1 if fan1 gets to slow to run.
  361.         start1 = 0;
  362.       }
  363.       if (start1 != 2){
  364.         if (start1 == 0){                                       //notices when to startup the fan.
  365.           if (37 > speed1 && speed1 > 14){
  366.             start1 = 1;
  367.           }
  368.         }
  369.       }
  370.       if (start1 == 1){                                         //starts the fan to access low speeds.
  371.         Serial.println("starting fan1");
  372.         analogWrite(fan1, 70);
  373.         start1 = 2;
  374.         delay (100);
  375.       }
  376.     }
  377.     if (speed1 > 14){
  378.       analogWrite(fan1, speed1);
  379.       Serial.print("fan1 speed is:");                           //says the fan1 speed.
  380.       Serial.println(speed1);
  381.     }
  382.     else {
  383.       Serial.print("fan1 speed is slow: ");
  384.       Serial.println(speed1);
  385.       analogWrite(fan1, 0);
  386.     }
  387.     /*if (speed2 != lspeed2){                                   //writes the speed of fan2 if change.
  388.      delay (100);
  389.      lspeed2 = speed2;
  390.      analogWrite(fan2, speed2);
  391.      Serial.print("fan2 speed is:");                            //says the fan2 speed.
  392.      Serial.println(speed2);
  393.      }*/
  394.     Vcc = adc_read_vcc();                                     //updates the Vcc voltage for correct measurements.
  395.     volts = (((analogRead(vlts)/1023.0)*5*42620)/9800);       //reads the voltage.
  396.     Serial.print("voltage is: ");
  397.     Serial.println(volts);                                    //tells you the voltage.
  398.     vampin = (((analogRead(ain)/1023.0)*5*42390)/9770);       //reads the voltage in.
  399.     vampout = (((analogRead(aout)/1023.0)*5*43060)/9940);     //reads the voltage out.
  400.     Serial.print("voltage in is: ");
  401.     Serial.println(vampin);                                   //tells you the voltage in.
  402.     Serial.print("voltage out is: ");
  403.     Serial.println(vampout);                                  //tells you the voltage out.
  404.     Serial.print("voltage drop is: ");
  405.     Serial.println(vampin - vampout);                         //tells you the voltage drop.*i will comment this out as it is just for debugging
  406.     if (amp){                                                 //calculates for high current.
  407.       amps = ((vampin - vampout)/.22);
  408.     }
  409.     else {
  410.       amps = ((vampin - vampout)/.47);                        //calculates for low current.
  411.     }
  412.     Serial.print("amperage is ");
  413.     if (amp){
  414.       Serial.print("high: ");
  415.     }
  416.     else {
  417.       Serial.print("low: ");
  418.     }
  419.     Serial.println(amps);                                     //tells you the amperage.
  420.     if ((crntlmt - amps) > .05){                              //the current is lower than the specified limit move on to voltage check.
  421.       if ((vltlmt - vampout) > .05){                          //if the voltage is lower than the specified value go ahead and decrease the PWM.
  422.         if (chrgrte < 255){
  423.           chrgrte ++;
  424.           Serial.print("increased ");
  425.         }
  426.       }
  427.     }
  428.     if ((vampout - vltlmt) > .05){//if the current or voltage is higher than the specified limit decrease the charge rate.
  429.       if (chrgrte > 0){
  430.         chrgrte --;
  431.         Serial.print("high voltage decreasing ");
  432.       }
  433.     }
  434.     if ((amps - crntlmt) > .05){
  435.       if (chrgrte > 0){
  436.         chrgrte --;
  437.         Serial.print("high current decreasing ");
  438.       }
  439.     }
  440.     if (chrgrte != lchrgrte){
  441.       analogWrite(batchrg, chrgrte);
  442.       lchrgrte = chrgrte;
  443.       Serial.print("changed PWM ");
  444.     }
  445.     Serial.print("charge rate: ");                           //tells you the charge rate.
  446.     Serial.println(chrgrte);
  447.     Serial.print("voltage limit: ");                         //tells you the voltage limit.
  448.     Serial.println(vltlmt);
  449.     Serial.print("current limit: ");                         //tells you the current limit.
  450.     Serial.println(crntlmt);
  451.     last = millis();
  452.   }                                                           //end of every second things.
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement