Advertisement
SuperBrainAK

master controll 0_4_7 i added charge controll and moved stuf

Mar 8th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.96 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!
  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 = 24;
  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 = 255;                                          //PWM value for the NPN charging transistor. 255 is off and 0 is full on.
  104. int lchrgrte = 0;                                           //last charge rate.
  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.       //if (vltlmt > 0){
  257.       Serial.println("button6: volt limit dowm");
  258.       vltlmt -= .1;
  259.       //}
  260.     }
  261.     if (digitalRead(Button7) == LOW){                         //reads the button4 state.
  262.       Serial.println("button7: volt and current limits reset");
  263.       vltlmt = 0;                                             //shuts off the voltage and current parameters.
  264.       crntlmt = 0;
  265.     }
  266.     if (digitalRead(Button8) == LOW){                         //reads the button4 state.
  267.       Serial.println("button8: current limit up");
  268.       crntlmt += .1;
  269.     }
  270.     if (digitalRead(Button9) == LOW){                         //reads the button4 state.
  271.       Serial.println("button9: current limit down");
  272.       crntlmt -= .1;
  273.     }
  274.     if (pbutton == 1){                                        //toggles my power outputs.
  275.       Serial.println("power");
  276.       power = !power;
  277.       if (power){
  278.         Serial.println("on");
  279.       }
  280.       else {
  281.         Serial.println("off");
  282.       }
  283.       digitalWrite(pbox, power ? LOW : HIGH);
  284.       digitalWrite(green, power ? HIGH : LOW);
  285.       digitalWrite(v5, power ? HIGH : LOW);
  286.       digitalWrite(v9, power ? HIGH : LOW);
  287.       digitalWrite(red, power ? LOW : HIGH);
  288.     }
  289.     if (p5 == 1){                                             //toggles my 5V output.
  290.       Serial.println("5v power");
  291.       p5v = !p5v;
  292.       if (p5v){
  293.         Serial.println("on");
  294.       }
  295.       else {
  296.         Serial.println("off");
  297.       }
  298.       digitalWrite(v5, p5v ? HIGH : LOW);
  299.     }
  300.     if (p9 == 1){                                             //toggles my 9V output.
  301.       Serial.println("9v power");
  302.       p9v = !p9v;
  303.       if (p9v){
  304.         Serial.println("on");
  305.       }
  306.       else {
  307.         Serial.println("off");
  308.       }
  309.       digitalWrite(v9, p9v ? HIGH : LOW);
  310.     }
  311.     if (crnt == 1){                                           //changes the resistance to use for current calculation.
  312.       Serial.println("changed resistance");
  313.       amp = !amp;
  314.     }
  315.     tval = analogRead(temp);                                  //updates the temperature.
  316.     if (tval > 450){                                          //cold temperature turns fan off if not already off.
  317.       if (speed1 != 0){
  318.         speed1 = 0;
  319.       }
  320.     }
  321.     if (450 >= tval && tval >= 300){                          //medium temperature auto equalize the fan.
  322.       if (tval > ltval){                                      //if the temp. falls slow down the fan.
  323.         speed1 --;
  324.       }
  325.       if (tval < ltval){
  326.         speed1 ++;                                            //if the temp. rises speed up the fan.
  327.       }
  328.     }
  329.     if (tval < 300){                                          //rather hot turns fan to max.
  330.       speed1 = 70;
  331.     }
  332.     if (tval < 250){                                          //hot and hotter tells you so.
  333.       Serial.println("heat sink is hot!");
  334.       digitalWrite(red, HIGH);
  335.       digitalWrite(green, HIGH);
  336.     }
  337.     Serial.print("temp value is:");                           //says the temp value.
  338.     ltval = tval;                                             //updates the last temperature.
  339.     Serial.println(tval);
  340.     if (speed1 < 0){                                            //limits range of speeds.
  341.       Serial.println("fan1 low");
  342.       speed1 = 0;
  343.     }
  344.     if (speed1 > 70){
  345.       Serial.println("fan1 high");
  346.       speed1 = 70;
  347.     }
  348.     /*if (speed2 < 0){
  349.      Serial.println("fan2 low");
  350.      speed2 = 0;
  351.      }
  352.      if (speed2 > 100){
  353.      Serial.println("fan2 high");
  354.      speed2 = 100;
  355.      }*/
  356.     if (speed1 != lspeed1){                                     //writes the speed of fan1 if change.
  357.       lspeed1 = speed1;
  358.       if (speed1 <= 14){                                        //puts start1 if fan1 gets to slow to run.
  359.         start1 = 0;
  360.       }
  361.       if (start1 != 2){
  362.         if (start1 == 0){                                       //notices when to startup the fan.
  363.           if (37 > speed1 && speed1 > 14){
  364.             start1 = 1;
  365.           }
  366.         }
  367.       }
  368.       if (start1 == 1){                                         //starts the fan to access low speeds.
  369.         Serial.println("starting fan1");
  370.         analogWrite(fan1, 70);
  371.         start1 = 2;
  372.         delay (100);
  373.       }
  374.     }
  375.     if (speed1 > 14){
  376.       analogWrite(fan1, speed1);
  377.       Serial.print("fan1 speed is:");                         //says the fan1 speed.
  378.       Serial.println(speed1);
  379.     }
  380.     else {
  381.       Serial.print("fan1 speed is slow: ");
  382.       Serial.println(speed1);
  383.       analogWrite(fan1, 0);
  384.     }
  385.     /*if (speed2 != lspeed2){                                   //writes the speed of fan2 if change.
  386.      delay (100);
  387.      lspeed2 = speed2;
  388.      analogWrite(fan2, speed2);
  389.      Serial.print("fan2 speed is:");                            //says the fan2 speed.
  390.      Serial.println(speed2);
  391.      }*/
  392.     Vcc = adc_read_vcc();                                     //updates the Vcc voltage for correct measurements.
  393.     volts = (((analogRead(vlts)/1023.0)*5*42450)/9700);       //reads the voltage.
  394.     Serial.print("voltage is: ");
  395.     Serial.println(volts);                                    //tells you the voltage.
  396.     vampin = (((analogRead(ain)/1023.0)*5*42320)/9720);       //reads the voltage in.
  397.     vampout = (((analogRead(aout)/1023.0)*5*42890)/9890);     //reads the voltage out.
  398.     Serial.print("voltage in is: ");
  399.     Serial.println(vampin);                                   //tells you the voltage in.
  400.     Serial.print("voltage out is: ");
  401.     Serial.println(vampout);                                  //tells you the voltage out.
  402.     Serial.print("voltage drop is: ");
  403.     Serial.println(vampin - vampout);                         //tells you the voltage drop.*i will comment this out as it is just for debugging
  404.     if (amp){                                                 //calculates for high current.
  405.       amps = ((vampin - vampout)/.22);
  406.     }
  407.     else {
  408.       amps = ((vampin - vampout)/.47);                        //calculates for low current.
  409.     }
  410.     Serial.print("amperage is ");
  411.     if (amp){
  412.       Serial.print("high: ");
  413.     }
  414.     else {
  415.       Serial.print("low: ");
  416.     }
  417.     Serial.println(amps);                                     //tells you the amperage.
  418.     if ((crntlmt - amps) > .05){                              //the current is lower than the specified limit move on to voltage check.
  419.       if ((vltlmt - vampout) > .05){                          //if the voltage is lower than the specified value go ahead and decrease the PWM.
  420.         if (chrgrte > 0){
  421.           chrgrte --;
  422.           Serial.print("increasing ");
  423.         }
  424.       }
  425.     }
  426.     else if (((amps - crntlmt) > .05) || ((volts - vampout) > .05)){//if the current or voltage is higher than the specified limit decrease the charge rate.
  427.       if (chrgrte < 255){
  428.         chrgrte ++;
  429.         Serial.print("decreasing ");
  430.       }
  431.     }
  432.     if (chrgrte != lchrgrte){                                //writes the PWM to the transistor.
  433.       analogWrite(batchrg, chrgrte);
  434.       lchrgrte = chrgrte;
  435.     }
  436.     Serial.print("charge rate: ");                           //tells you the charge rate.
  437.     Serial.println(chrgrte);
  438.     Serial.print("voltage limit: ");                         //tells you the voltage limit.
  439.     Serial.println(vltlmt);
  440.     Serial.print("current limit: ");                         //tells you the current limit.
  441.     Serial.println(crntlmt);
  442.     last = millis();
  443.   }                                                           //end of every second things.
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement