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.
- -controlls the audio input/mute (no longer connected)
- -fan1 is a small computer fan2 is the xbox fans.(no longer connected)remote cox universal aux 0820.
- -uses display board: 1wire buttons, rotary encoder, led's 1-5, no hope for the FLD(no datasheet)
- By: SuperBranAK
- */
- #include <IRremote.h> //includes libraries.
- #include <IRremoteInt.h>
- //const pin assignments
- const byte irin = 27;
- const byte rtncdr1 = 18;
- const byte rtncdr2 = 19;
- const byte temp = 45;
- const byte pbox = 14;
- //const byte a = 22;
- //const byte b = 26;
- //const byte mute = 20;
- const byte red = 17;
- const byte green = 16;
- const byte led1 = 1;
- const byte led2 = 2;
- const byte led3 = 3;
- const byte led4 = 4;
- const byte led5 = 5;
- const byte v5 = 21;
- const byte v9 = 23;
- const byte fan1 = 26;
- //const byte fan2 = 25;
- const byte Buttons = 38; //buttons on display board.
- IRrecv irrecv(irin); //ir recv stuff.
- decode_results results;
- volatile int lastEncoded = 0; //rotary encoder stuff.
- volatile long ncdrvl = 0;
- volatile long lncdrvl = 0;
- long lastencoderValue = 0;
- int lastMSB = 0;
- int lastLSB = 0;
- unsigned long last = millis(); //updates the time it has been
- byte startup = 0; //runs startup sequence if=0.
- byte power = 0; //my main power state.
- //byte input = 0; //input selection 0=mute 1=1etc.
- byte p5v = 0; //my 5V power state.
- byte p9v = 0; //my 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 bval = 0; //button value.
- int lbval = 0; //last button value.
- byte Button = 0; //button .
- int tval = 0; //temperature value.
- int ltval = 0; //last temp. value.
- void setup () {
- pinMode(pbox, OUTPUT); //pin modes
- //pinMode(a, OUTPUT);
- //pinMode(b, OUTPUT);
- //pinMode(mute, OUTPUT);
- pinMode(red, OUTPUT);
- pinMode(green, OUTPUT);
- pinMode(led1, OUTPUT);
- pinMode(led2, OUTPUT);
- pinMode(led3, OUTPUT);
- pinMode(led4, OUTPUT);
- pinMode(led5, OUTPUT);
- pinMode(v5, OUTPUT);
- pinMode(v9, OUTPUT);
- pinMode(fan1, OUTPUT);
- //pinMode(fan2, OUTPUT);
- pinMode(temp, INPUT);
- pinMode(Buttons, INPUT_PULLUP);
- pinMode(rtncdr1, INPUT);
- pinMode(rtncdr2, INPUT);
- attachInterrupt(7, updateEncoder, CHANGE); //rotary encoder interupts
- attachInterrupt(6, updateEncoder, CHANGE);
- Serial.begin(9600);
- irrecv.enableIRIn();
- }
- void loop() {
- if (startup == 0){ //sets the states of my sensitive outputs.
- digitalWrite(red, HIGH);
- digitalWrite(v5, LOW);
- digitalWrite(v9, LOW);
- //digitalWrite(mute, HIGH);
- startup = 1;
- }
- if (speed1 != lspeed1){ //controls the speed of fan1 if change.
- delay (100);
- lspeed1 = speed1;
- if (speed1 <= 14){ //resets start1 if fan1 gets to slow to run.
- start1 = 0;
- }
- 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 unable to sustain: ");
- 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);
- }*/
- if (millis() - last > 1000) { //only recieves an input every second.
- bval = analogRead(Buttons); //updates the button value.
- if (bval >= 300){ //no buttons pressed.
- Button = 0;
- }
- if (300 > bval && bval >= 159){
- Button = 6;
- }
- if (159 > bval && bval >= 116){
- Button = 5;
- }
- if (116 > bval && bval >= 81){
- Button = 4;
- }
- if (81 > bval && bval >= 53){
- Button = 1;
- }
- if (53 > bval && bval >= 27){
- Button = 2;
- }
- if (20 > bval){
- Button = 3;
- }
- if (bval != lbval){ //if state changes update value.
- Serial.print(Button);
- lbval = bval;
- Serial.print("Button value is:");
- Serial.println(bval);
- } //end of solid buttons.
- if (irrecv.decode(&results)) { //recieves the ir signal.
- if (results.value == 0xd87245ba) { //power button.
- Serial.println("power button");
- Button = 1;
- }
- 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){
- Serial.println("1/all");
- }
- 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){ //cycle inputs.
- Serial.println("input");
- //input += 1;
- }
- 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){ //input1 selection.
- Serial.println("1");
- //input = 1;
- }
- else if (results.value == 0xd872906f){ //input2 selection.
- Serial.println("2");
- //input = 2;
- }
- else if (results.value == 0xd872f00f){ //input3 selection.
- Serial.println("3");
- //input = 3;
- }
- else if (results.value == 0xd872b04f){ //input4 selection.
- Serial.println("4");
- //input = 4;
- }
- else if (results.value == 0xd87252ad){ //5v power button.
- Serial.println("5");
- Button = 3;
- }
- 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){ //9Vpower button.
- Serial.println("9");
- Button = 2;
- }
- 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();
- 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);
- }
- if (tval != ltval){ //if temperature changes update value.
- Serial.print(ltval);
- ltval = tval;
- Serial.print("temp value is:");
- Serial.println(tval);
- }
- if (Button == 1){ //toggles my power outputs.
- power = !power;
- Serial.print("power: ");
- if (power){
- Serial.println("on");
- }
- else {
- Serial.println("off");
- }
- digitalWrite(pbox, power ? HIGH : LOW);
- digitalWrite(green, power ? HIGH : LOW);
- digitalWrite(red, power ? LOW : HIGH);
- digitalWrite(v5, power ? HIGH : LOW);
- digitalWrite(v9, power ? HIGH : LOW);
- p5v = power;
- p9v = power;
- }
- if (Button == 2){ //toggles my 9V output.
- p9v = !p9v;
- Serial.print("9v power: ");
- if (p9v){
- Serial.println("on");
- }
- else {
- Serial.println("off");
- }
- digitalWrite(v9, p9v ? HIGH : LOW);
- }
- if (Button == 3){ //toggles my 5V output.
- p5v = !p5v;
- Serial.print("5v power: ");
- if (p5v){
- Serial.println("on");
- }
- else {
- Serial.println("off");
- }
- digitalWrite(v5, p5v ? HIGH : LOW);
- }
- if (Button == 4){
- speed1 += 5;
- delay (100);
- }
- if (Button == 5){ //change speeds of fans.
- speed1 -= 5;
- delay (100);
- }
- /*if (input > 4){ //controlls my input selector.
- input = 0;
- }
- if (input == 0){ //says mute every second
- digitalWrite(mute, LOW);
- Serial.println("mute");
- }
- else {
- digitalWrite(mute, HIGH);
- Serial.println("mute off");
- }
- if (input == 1){
- digitalWrite(a, HIGH);
- digitalWrite(b, HIGH);
- Serial.println("input 1");
- }
- if (input == 2){
- digitalWrite(a, LOW);
- digitalWrite(b, HIGH);
- Serial.println("input 2");
- }
- if (input == 3){
- digitalWrite(a, HIGH);
- digitalWrite(b, LOW);
- Serial.println("input 3");
- }
- if (input == 4){
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- Serial.println("input 4");
- }*/
- last = millis();
- } //end of every second things.
- /*if (digitalRead(Button6) == LOW){
- speed2 -= 1;
- delay (100);
- }
- if (digitalRead(Button7) == LOW){
- speed2 += 1;
- delay (100);
- }*/
- 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 (lncdrvl != ncdrvl){
- switch (ncdrvl % 6){
- case 0:
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(led3, LOW);
- digitalWrite(led4, LOW);
- digitalWrite(led5, LOW);
- break;
- case 1:
- digitalWrite(led1, HIGH);
- digitalWrite(led2, LOW);
- digitalWrite(led3, LOW);
- digitalWrite(led4, LOW);
- digitalWrite(led5, LOW);
- break;
- case 2:
- digitalWrite(led1, LOW);
- digitalWrite(led2, HIGH);
- digitalWrite(led3, LOW);
- digitalWrite(led4, LOW);
- digitalWrite(led5, LOW);
- break;
- case 3:
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(led3, HIGH);
- digitalWrite(led4, LOW);
- digitalWrite(led5, LOW);
- break;
- case 4:
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(led3, LOW);
- digitalWrite(led4, HIGH);
- digitalWrite(led5, LOW);
- break;
- case 5:
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(led3, LOW);
- digitalWrite(led4, LOW);
- digitalWrite(led5, HIGH);
- break;
- }
- Serial.print("encoder value:");
- Serial.println(ncdrvl);
- lncdrvl = ncdrvl;
- }
- }
- void updateEncoder(){
- int MSB = digitalRead(rtncdr1); //MSB = most significant bit
- int LSB = digitalRead(rtncdr2); //LSB = least significant bit
- int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
- int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
- if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) ncdrvl ++;
- if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) ncdrvl --;
- lastEncoded = encoded; //store this value for next time
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement