Advertisement
SuperBrainAK

master control 0_5

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