Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*this is controlling my powerbox, 3050, 3090 and fans W/PWM.takes the temp of 3050/3090 to enable auto fan speed.
- fan1 is a small computer fan, fan2 is the xbox fans.(no longer connected)remote cox universal aux 0820.
- 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.
- ABSOLUTE MAX OF 21.5v ON ALL INPUTS!
- button panel is a direct tv satellite box
- put together by SuperBrainAK
- */
- #include <IRremote.h> //includes libraries.
- //const pin assignments
- const byte irin = 27;
- const byte temp = 45;
- const byte vlts = 38;
- const byte ain = 39;
- const byte aout = 40;
- const byte pbox = 0;
- const byte red = 17;
- const byte green = 13;
- const byte v5 = 21;
- const byte v9 = 22;
- const byte fan1 = 26;
- //const byte fan2 = 25;
- const byte batchrg = 24;
- const byte Button1 = 1; //power (power)
- const byte Button2 = 2; //5v power (guide)
- const byte Button3 = 3; //9v power (menu)
- const byte Button4 = 4; //change amperage resistance (active)
- const byte Button5 = 5; //raise the output voltage (up)
- const byte Button6 = 7; //lower the output voltage (down)
- const byte Button7 = 8; //reset the current and voltage (select)
- const byte Button8 = 9; //raises the output current (right)
- const byte Button9 = 10; //lowers the output current (left)
- IRrecv irrecv(irin); //ir recv stuff.
- decode_results results;
- int16_t adc_read_vcc(){
- //"reset" the ADMUX
- ADMUX = 0b01000000;
- ADMUX |=(1<<REFS0) | (1<<MUX4) | (1<<MUX3) | (1<<MUX2) | (1<<MUX1);
- //REFS0 - VCC as reference
- //mux - measurment on the 1.11V internal
- 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)
- delay(5);//ehh...
- ADCSRA|=(1<<ADSC);//we start conversion
- while(!(ADCSRA & (1<<ADIF)));//Wait for conversion to complete
- ADCSRA|=(1<<ADIF);//just making sure it's 0
- return (1125300L/ADC);//to get it in mV (to not to loose to many decimals) - (1100mV*1023)
- }
- void setup () {
- pinMode(pbox, OUTPUT); //pin modes
- pinMode(red, OUTPUT);
- pinMode(green, OUTPUT);
- pinMode(v5, OUTPUT);
- pinMode(v9, OUTPUT);
- pinMode(fan1, OUTPUT);
- //pinMode(fan2, OUTPUT);
- pinMode(batchrg, OUTPUT);
- pinMode(temp, INPUT);
- pinMode(vlts, INPUT);
- pinMode(ain, INPUT);
- pinMode(aout, INPUT);
- pinMode(Button1, INPUT);
- pinMode(Button2, INPUT);
- pinMode(Button3, INPUT);
- pinMode(Button4, INPUT);
- pinMode(Button5, INPUT);
- pinMode(Button6, INPUT);
- pinMode(Button7, INPUT);
- pinMode(Button8, INPUT);
- pinMode(Button9, INPUT);
- Serial.begin(9600);
- irrecv.enableIRIn();
- }
- unsigned long last = millis(); //declares millis var?
- byte startup = 0; //runs startup sequence if=0.
- byte power = 0; //main power state.
- byte p5v = 0; //5V power state.
- byte p9v = 0; //9V power state.
- byte start1 = 0; //startup for fan1.
- int speed1 = 0; //speed of fan1.
- //int speed2 = 0; //speed of fan2.
- int lspeed1 = 0; //last speed of fan1.
- //int lspeed2 = 0; //last speed of fan2.
- int tval = 0; //temperature value.
- int ltval = 0; //last temperature value.
- float Vcc = 0; //stores the Vcc voltage. in mV
- float volts = 0; //voltage metering.
- float vampin = 0; //voltage before the current resistor.
- float vampout = 0; //voltage after the current resistor.
- byte amp = 0; //0=low current, 1=high current.
- float amps = 0; //calculated amps.
- float crntlmt = 0; //max current the transistor will source.
- float vltlmt = 0; //max voltage the transistor will get to.
- int chrgrte = 255; //PWM value for the NPN charging transistor. 255 is off and 0 is full on.
- int lchrgrte = 0; //last charge rate.
- void loop() {
- if (startup == 0){ //sets the states of my sensitive outputs.
- digitalWrite(red, HIGH);
- digitalWrite(pbox, HIGH);
- digitalWrite(v5, LOW);
- digitalWrite(v9, LOW);
- startup = 1;
- }
- byte pbutton = 0; //temporary power variable state.
- byte p5 = 0; //temporary 5V variable state.
- byte p9 = 0; //temporary 9V variable state.
- byte crnt = 0; //temporary current variable state.
- if (millis() - last > 1000) { //only recieves an input every second.
- if (irrecv.decode(&results)) { //recieves the ir signal.
- if (results.value == 0xd87245ba) { //power button.
- Serial.println("power button");
- pbutton = !pbutton;
- }
- else if (results.value == 0xffc03f){
- Serial.println("display/setup");
- }
- else if (results.value == 0xff807f){
- Serial.println("zoom");
- }
- else if (results.value == 0xff609f){
- Serial.println("SUB");
- }
- else if (results.value == 0xff906f){
- Serial.println("back");
- }
- else if (results.value == 0xfff807){
- Serial.println("skip");
- }
- else if (results.value == 0xffb04f){
- Serial.println("A-B");
- }
- else if (results.value == 0xffa857){ //changes the current ranges.
- Serial.println("1/all");
- crnt = !crnt;
- }
- else if (results.value == 0xd872748b){
- Serial.println("up");
- speed1 += 5; //fan1 speed +5.
- }
- else if (results.value == 0xd872b44b){
- Serial.println("down");
- speed1 -= 5; //fan1 speed -5.
- }
- else if (results.value == 0xd872f807){
- Serial.println("left");
- //speed2 -= 5; //fan2 speed -5.
- }
- else if (results.value == 0xd87204fb){
- Serial.println("right");
- //speed2 += 5; //fan2 speed +5.
- }
- else if (results.value == 0xd8720cf3){
- Serial.println("select");
- }
- else if (results.value == 0xffe817){
- Serial.println("play/pause");
- }
- else if (results.value == 0xff6897){
- Serial.println("stop");
- }
- else if (results.value == 0xffb24d){
- Serial.println("menu");
- }
- else if (results.value == 0xd872649b){
- Serial.println("input");
- }
- else if (results.value == 0xff58a7){
- Serial.println("angle");
- }
- else if (results.value == 0xff40bf){
- Serial.println("lcd mode");
- }
- else if (results.value == 0xffa05f){
- Serial.println("title");
- }
- else if (results.value == 0xd872d02f){
- Serial.println("1");
- }
- else if (results.value == 0xd872906f){
- Serial.println("2");
- }
- else if (results.value == 0xd872f00f){
- Serial.println("3");
- }
- else if (results.value == 0xd872b04f){
- Serial.println("4");
- }
- else if (results.value == 0xd87252ad){ //5v power button.
- Serial.println("5");
- p5 = !p5;
- }
- else if (results.value == 0xd872d02f){
- Serial.println("6");
- }
- else if (results.value == 0xd872708f){
- Serial.println("7");
- }
- else if (results.value == 0xd872609f){
- Serial.println("8");
- }
- else if (results.value == 0xd872a05f){ //9v power button.
- Serial.println("9");
- p9 = !p9;
- }
- else if (results.value == 0xd87240bf){
- Serial.println("0");
- }
- else { //tells you any unknown signal.
- if (results.decode_type == NEC) {
- Serial.print("Decoded NEC: ");
- }
- else if (results.decode_type == SONY) {
- Serial.print("Decoded SONY: ");
- }
- else if (results.decode_type == RC5) {
- Serial.print("Decoded RC5: ");
- }
- else if (results.decode_type == RC6) {
- Serial.print("Decoded RC6: ");
- }
- Serial.println(results.value, HEX);
- }
- }
- irrecv.resume();
- if (digitalRead(Button1) == LOW){ //reads the button1 state.
- Serial.println("button1");
- pbutton = !pbutton;
- }
- if (digitalRead(Button2) == LOW){ //reads the button2 state.
- Serial.println("button2");
- p5 = !p5;
- }
- if (digitalRead(Button3) == LOW){ //reads the button3 state.
- Serial.println("button3");
- p9 = !p9;
- }
- if (digitalRead(Button4) == LOW){ //reads the button4 state.
- Serial.println("button4");
- crnt = !crnt;
- }
- if (digitalRead(Button5) == LOW){ //reads the button4 state.
- Serial.println("button5: volt limit up");
- vltlmt += .1;
- }
- if (digitalRead(Button6) == LOW){ //reads the button4 state.
- //if (vltlmt > 0){
- Serial.println("button6: volt limit dowm");
- vltlmt -= .1;
- //}
- }
- if (digitalRead(Button7) == LOW){ //reads the button4 state.
- Serial.println("button7: volt and current limits reset");
- vltlmt = 0; //shuts off the voltage and current parameters.
- crntlmt = 0;
- }
- if (digitalRead(Button8) == LOW){ //reads the button4 state.
- Serial.println("button8: current limit up");
- crntlmt += .1;
- }
- if (digitalRead(Button9) == LOW){ //reads the button4 state.
- Serial.println("button9: current limit down");
- crntlmt -= .1;
- }
- if (pbutton == 1){ //toggles my power outputs.
- Serial.println("power");
- power = !power;
- if (power){
- Serial.println("on");
- }
- else {
- Serial.println("off");
- }
- digitalWrite(pbox, power ? LOW : HIGH);
- digitalWrite(green, power ? HIGH : LOW);
- digitalWrite(v5, power ? HIGH : LOW);
- digitalWrite(v9, power ? HIGH : LOW);
- digitalWrite(red, power ? LOW : HIGH);
- }
- if (p5 == 1){ //toggles my 5V output.
- Serial.println("5v power");
- p5v = !p5v;
- if (p5v){
- Serial.println("on");
- }
- else {
- Serial.println("off");
- }
- digitalWrite(v5, p5v ? HIGH : LOW);
- }
- if (p9 == 1){ //toggles my 9V output.
- Serial.println("9v power");
- p9v = !p9v;
- if (p9v){
- Serial.println("on");
- }
- else {
- Serial.println("off");
- }
- digitalWrite(v9, p9v ? HIGH : LOW);
- }
- if (crnt == 1){ //changes the resistance to use for current calculation.
- Serial.println("changed resistance");
- amp = !amp;
- }
- tval = analogRead(temp); //updates the temperature.
- if (tval > 450){ //cold temperature turns fan off if not already off.
- if (speed1 != 0){
- speed1 = 0;
- }
- }
- if (450 >= tval && tval >= 300){ //medium temperature auto equalize the fan.
- if (tval > ltval){ //if the temp. falls slow down the fan.
- speed1 --;
- }
- if (tval < ltval){
- speed1 ++; //if the temp. rises speed up the fan.
- }
- }
- if (tval < 300){ //rather hot turns fan to max.
- speed1 = 70;
- }
- if (tval < 250){ //hot and hotter tells you so.
- Serial.println("heat sink is hot!");
- digitalWrite(red, HIGH);
- digitalWrite(green, HIGH);
- }
- Serial.print("temp value is:"); //says the temp value.
- ltval = tval; //updates the last temperature.
- Serial.println(tval);
- if (speed1 < 0){ //limits range of speeds.
- Serial.println("fan1 low");
- speed1 = 0;
- }
- if (speed1 > 70){
- Serial.println("fan1 high");
- speed1 = 70;
- }
- /*if (speed2 < 0){
- Serial.println("fan2 low");
- speed2 = 0;
- }
- if (speed2 > 100){
- Serial.println("fan2 high");
- speed2 = 100;
- }*/
- if (speed1 != lspeed1){ //writes the speed of fan1 if change.
- lspeed1 = speed1;
- if (speed1 <= 14){ //puts start1 if fan1 gets to slow to run.
- start1 = 0;
- }
- if (start1 != 2){
- if (start1 == 0){ //notices when to startup the fan.
- if (37 > speed1 && speed1 > 14){
- start1 = 1;
- }
- }
- }
- if (start1 == 1){ //starts the fan to access low speeds.
- Serial.println("starting fan1");
- analogWrite(fan1, 70);
- start1 = 2;
- delay (100);
- }
- }
- if (speed1 > 14){
- analogWrite(fan1, speed1);
- Serial.print("fan1 speed is:"); //says the fan1 speed.
- Serial.println(speed1);
- }
- else {
- Serial.print("fan1 speed is slow: ");
- Serial.println(speed1);
- analogWrite(fan1, 0);
- }
- /*if (speed2 != lspeed2){ //writes the speed of fan2 if change.
- delay (100);
- lspeed2 = speed2;
- analogWrite(fan2, speed2);
- Serial.print("fan2 speed is:"); //says the fan2 speed.
- Serial.println(speed2);
- }*/
- Vcc = adc_read_vcc(); //updates the Vcc voltage for correct measurements.
- volts = (((analogRead(vlts)/1023.0)*5*42450)/9700); //reads the voltage.
- Serial.print("voltage is: ");
- Serial.println(volts); //tells you the voltage.
- vampin = (((analogRead(ain)/1023.0)*5*42320)/9720); //reads the voltage in.
- vampout = (((analogRead(aout)/1023.0)*5*42890)/9890); //reads the voltage out.
- Serial.print("voltage in is: ");
- Serial.println(vampin); //tells you the voltage in.
- Serial.print("voltage out is: ");
- Serial.println(vampout); //tells you the voltage out.
- Serial.print("voltage drop is: ");
- Serial.println(vampin - vampout); //tells you the voltage drop.*i will comment this out as it is just for debugging
- if (amp){ //calculates for high current.
- amps = ((vampin - vampout)/.22);
- }
- else {
- amps = ((vampin - vampout)/.47); //calculates for low current.
- }
- Serial.print("amperage is ");
- if (amp){
- Serial.print("high: ");
- }
- else {
- Serial.print("low: ");
- }
- Serial.println(amps); //tells you the amperage.
- if ((crntlmt - amps) > .05){ //the current is lower than the specified limit move on to voltage check.
- if ((vltlmt - vampout) > .05){ //if the voltage is lower than the specified value go ahead and decrease the PWM.
- if (chrgrte > 0){
- chrgrte --;
- Serial.print("increasing ");
- }
- }
- }
- else if (((amps - crntlmt) > .05) || ((volts - vampout) > .05)){//if the current or voltage is higher than the specified limit decrease the charge rate.
- if (chrgrte < 255){
- chrgrte ++;
- Serial.print("decreasing ");
- }
- }
- if (chrgrte != lchrgrte){ //writes the PWM to the transistor.
- analogWrite(batchrg, chrgrte);
- lchrgrte = chrgrte;
- }
- Serial.print("charge rate: "); //tells you the charge rate.
- Serial.println(chrgrte);
- Serial.print("voltage limit: "); //tells you the voltage limit.
- Serial.println(vltlmt);
- Serial.print("current limit: "); //tells you the current limit.
- Serial.println(crntlmt);
- last = millis();
- } //end of every second things.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement