Advertisement
SuperBrainAK

my full arduino code on my teensy++2.0 v. 0_5_1

Nov 18th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.47 KB | None | 0 0
  1. /*this is controlling my powerbox, 3050, 3090, and fans W/PWM.
  2.  -takes the temp of 3050/3090 to enable auto fan speed.
  3.  -controlls the audio input/mute (no longer connected)
  4.  -fan1 is a small computer fan2 is the xbox fans.(no longer connected)remote cox universal aux 0820.
  5.  -uses display board: 1wire buttons, rotary encoder, led's 1-5, no hope for the FLD(no datasheet)
  6.  By: SuperBranAK
  7.  */
  8. #include <IRremote.h>                                       //includes libraries.
  9. #include <IRremoteInt.h>
  10. //const pin assignments
  11. const byte irin = 27;
  12. const byte rtncdr1 = 18;
  13. const byte rtncdr2 = 19;
  14. const byte temp = 45;
  15. const byte pbox = 14;
  16. //const byte a = 22;
  17. //const byte b = 26;
  18. //const byte mute = 20;
  19. const byte red = 17;
  20. const byte green = 16;
  21. const byte led1 = 1;
  22. const byte led2 = 2;
  23. const byte led3 = 3;
  24. const byte led4 = 4;
  25. const byte led5 = 5;
  26. const byte v5 = 21;
  27. const byte v9 = 23;
  28. const byte fan1 = 26;
  29. //const byte fan2 = 25;
  30. const byte Buttons = 38;                                    //buttons on display board.
  31.  
  32. IRrecv irrecv(irin);                                        //ir recv stuff.
  33. decode_results results;
  34.  
  35. volatile int lastEncoded = 0;                               //rotary encoder stuff.
  36. volatile long ncdrvl = 0;
  37. volatile long lncdrvl = 0;
  38. long lastencoderValue = 0;
  39. int lastMSB = 0;
  40. int lastLSB = 0;
  41.  
  42. unsigned long last = millis();                              //updates the time it has been
  43. byte startup = 0;                                           //runs startup sequence if=0.
  44. byte power = 0;                                             //my main power state.
  45. //byte input = 0;                                            //input selection 0=mute 1=1etc.
  46. byte p5v = 0;                                               //my 5V power state.
  47. byte p9v = 0;                                               //my 9V power state.
  48. byte start1 = 0;                                            //startup for fan1.
  49. int speed1 = 0;                                             //speed of fan1.
  50. //int speed2 = 0;                                             //speed of fan2.
  51. int lspeed1 = 0;                                            //last speed of fan1.
  52. //int lspeed2 = 0;                                            //last speed of fan2.
  53. int bval = 0;                                               //button value.
  54. int lbval = 0;                                              //last button value.
  55. byte Button = 0;                                            //button .
  56. int tval = 0;                                               //temperature value.
  57. int ltval = 0;                                              //last temp. value.
  58.  
  59. void setup () {
  60.   pinMode(pbox, OUTPUT);                                    //pin modes
  61.   //pinMode(a, OUTPUT);
  62.   //pinMode(b, OUTPUT);
  63.   //pinMode(mute, OUTPUT);
  64.   pinMode(red, OUTPUT);
  65.   pinMode(green, OUTPUT);
  66.   pinMode(led1, OUTPUT);
  67.   pinMode(led2, OUTPUT);
  68.   pinMode(led3, OUTPUT);
  69.   pinMode(led4, OUTPUT);
  70.   pinMode(led5, OUTPUT);
  71.   pinMode(v5, OUTPUT);
  72.   pinMode(v9, OUTPUT);
  73.   pinMode(fan1, OUTPUT);
  74.   //pinMode(fan2, OUTPUT);
  75.   pinMode(temp, INPUT);
  76.   pinMode(Buttons, INPUT_PULLUP);
  77.   pinMode(rtncdr1, INPUT);
  78.   pinMode(rtncdr2, INPUT);
  79.   attachInterrupt(7, updateEncoder, CHANGE);                //rotary encoder interupts
  80.   attachInterrupt(6, updateEncoder, CHANGE);
  81.   Serial.begin(9600);
  82.   irrecv.enableIRIn();
  83. }
  84.  
  85. void loop() {
  86.   if (startup == 0){                                        //sets the states of my sensitive outputs.
  87.     digitalWrite(red, HIGH);
  88.     digitalWrite(v5, LOW);
  89.     digitalWrite(v9, LOW);
  90.     //digitalWrite(mute, HIGH);
  91.     startup = 1;
  92.   }
  93.   if (speed1 != lspeed1){                                   //controls the speed of fan1 if change.
  94.     delay (100);
  95.     lspeed1 = speed1;
  96.     if (speed1 <= 14){                                      //resets start1 if fan1 gets to slow to run.
  97.       start1 = 0;
  98.     }
  99.     if (start1 == 0){                                        //notices when to startup the fan.
  100.       if (37 > speed1 && speed1 > 14){
  101.         start1 = 1;
  102.       }
  103.     }
  104.     if (start1 == 1){                                       //starts the fan to access low speeds.
  105.       Serial.println("starting fan1");
  106.       analogWrite(fan1, 70);
  107.       start1 = 2;
  108.       delay (100);
  109.     }
  110.     if (speed1 > 14){
  111.       analogWrite(fan1, speed1);
  112.       Serial.print("fan1 speed is:");                       //says the fan1 speed.
  113.       Serial.println(speed1);
  114.     }
  115.     else {
  116.       Serial.print("fan1 unable to sustain: ");
  117.       Serial.println(speed1);
  118.       analogWrite(fan1, 0);
  119.     }
  120.   }
  121.   /*if (speed2 != lspeed2){                                   //writes the speed of fan2 if change.
  122.    delay (100);
  123.    lspeed2 = speed2;
  124.    analogWrite(fan2, speed2);
  125.    Serial.print("fan2 speed is:");                         //says the fan2 speed.
  126.    Serial.println(speed2);
  127.    }*/
  128.   if (millis() - last > 1000) {                             //only recieves an input every second.
  129.     bval = analogRead(Buttons);                             //updates the button value.
  130.     if (bval >= 300){                                       //no buttons pressed.
  131.       Button = 0;
  132.     }
  133.     if (300 > bval && bval >= 159){
  134.       Button = 6;
  135.     }
  136.     if (159 > bval && bval >= 116){
  137.       Button = 5;
  138.     }
  139.     if (116 > bval && bval >= 81){
  140.       Button = 4;
  141.     }
  142.     if (81 > bval && bval >= 53){
  143.       Button = 1;
  144.     }
  145.     if (53 > bval && bval >= 27){
  146.       Button = 2;
  147.     }
  148.     if (20 > bval){
  149.       Button = 3;
  150.     }
  151.     if (bval != lbval){                                     //if state changes update value.
  152.       Serial.print(Button);
  153.       lbval = bval;
  154.       Serial.print("Button value is:");
  155.       Serial.println(bval);
  156.     }                                                       //end of solid buttons.
  157.     if (irrecv.decode(&results)) {                          //recieves the ir signal.
  158.       if (results.value == 0xd87245ba) {                    //power button.
  159.         Serial.println("power button");
  160.         Button = 1;
  161.       }
  162.       else if (results.value == 0xffc03f){
  163.         Serial.println("display/setup");
  164.       }
  165.       else if (results.value == 0xff807f){
  166.         Serial.println("zoom");
  167.       }
  168.       else if (results.value == 0xff609f){
  169.         Serial.println("SUB");
  170.       }
  171.       else if (results.value == 0xff906f){
  172.         Serial.println("back");
  173.       }
  174.       else if (results.value == 0xfff807){
  175.         Serial.println("skip");
  176.       }
  177.       else if (results.value == 0xffb04f){
  178.         Serial.println("A-B");
  179.       }
  180.       else if (results.value == 0xffa857){
  181.         Serial.println("1/all");
  182.       }
  183.       else if (results.value == 0xd872748b){
  184.         Serial.println("up");
  185.         speed1 += 5;                                        //fan1 speed +5.
  186.       }
  187.       else if (results.value == 0xd872b44b){
  188.         Serial.println("down");
  189.         speed1 -= 5;                                        //fan1 speed -5.
  190.       }
  191.       else if (results.value == 0xd872f807){
  192.         Serial.println("left");
  193.         //speed2 -= 5;                                        //fan2 speed -5.
  194.       }
  195.       else if (results.value == 0xd87204fb){
  196.         Serial.println("right");
  197.         //speed2 += 5;                                        //fan2 speed +5.
  198.       }
  199.       else if (results.value == 0xd8720cf3){
  200.         Serial.println("select");
  201.       }
  202.       else if (results.value == 0xffe817){
  203.         Serial.println("play/pause");
  204.       }
  205.       else if (results.value == 0xff6897){
  206.         Serial.println("stop");
  207.       }
  208.       else if (results.value == 0xffb24d){
  209.         Serial.println("menu");
  210.       }
  211.       else if (results.value == 0xd872649b){                //cycle inputs.
  212.         Serial.println("input");
  213.         //input += 1;
  214.       }
  215.       else if (results.value == 0xff58a7){
  216.         Serial.println("angle");
  217.       }
  218.       else if (results.value == 0xff40bf){
  219.         Serial.println("lcd mode");
  220.       }
  221.       else if (results.value == 0xffa05f){
  222.         Serial.println("title");
  223.       }
  224.       else if (results.value == 0xd872d02f){                //input1 selection.
  225.         Serial.println("1");
  226.         //input = 1;
  227.       }
  228.       else if (results.value == 0xd872906f){                //input2 selection.
  229.         Serial.println("2");
  230.         //input = 2;
  231.       }
  232.       else if (results.value == 0xd872f00f){                //input3 selection.
  233.         Serial.println("3");
  234.         //input = 3;
  235.       }
  236.       else if (results.value == 0xd872b04f){                //input4 selection.
  237.         Serial.println("4");
  238.         //input = 4;
  239.       }
  240.       else if (results.value == 0xd87252ad){                //5v power button.
  241.         Serial.println("5");
  242.         Button = 3;
  243.       }
  244.       else if (results.value == 0xd872d02f){
  245.         Serial.println("6");
  246.       }
  247.       else if (results.value == 0xd872708f){
  248.         Serial.println("7");
  249.       }
  250.       else if (results.value == 0xd872609f){
  251.         Serial.println("8");
  252.       }
  253.       else if (results.value == 0xd872a05f){                //9Vpower button.
  254.         Serial.println("9");
  255.         Button = 2;
  256.       }
  257.       else if (results.value == 0xd87240bf){
  258.         Serial.println("0");
  259.       }
  260.       else {                                                //tells you any unknown signal.
  261.         if (results.decode_type == NEC) {
  262.           Serial.print("Decoded NEC: ");
  263.         }
  264.         else if (results.decode_type == SONY) {
  265.           Serial.print("Decoded SONY: ");
  266.         }
  267.         else if (results.decode_type == RC5) {
  268.           Serial.print("Decoded RC5: ");
  269.         }
  270.         else if (results.decode_type == RC6) {
  271.           Serial.print("Decoded RC6: ");
  272.         }
  273.         Serial.println(results.value, HEX);
  274.       }
  275.     }
  276.     irrecv.resume();
  277.     tval = analogRead(temp);                                //updates the temperature.
  278.     if (tval > 450){                                        //cold temperature turns fan off if not already off.
  279.       if (speed1 != 0){
  280.         speed1 = 0;
  281.       }
  282.     }
  283.     if (450 >= tval && tval >= 300){                        //medium temperature auto equalize the fan.
  284.       if (tval > ltval){                                    //if the temp. falls slow down the fan.
  285.         speed1 --;
  286.       }
  287.       if (tval < ltval){
  288.         speed1 ++;                                          //if the temp. rises speed up the fan.
  289.       }
  290.     }
  291.     if (tval < 300){                                        //rather hot turns fan to max.
  292.       speed1 = 70;
  293.     }
  294.     if (tval < 250){                                        //hot and hotter tells you so.
  295.       Serial.println("heat sink is hot!");
  296.       digitalWrite(red, HIGH);
  297.       digitalWrite(green, HIGH);
  298.     }
  299.     if (tval != ltval){                                     //if temperature changes update value.
  300.       Serial.print(ltval);
  301.       ltval = tval;
  302.       Serial.print("temp value is:");
  303.       Serial.println(tval);
  304.     }
  305.     if (Button == 1){                                      //toggles my power outputs.
  306.       power = !power;
  307.       Serial.print("power: ");
  308.       if (power){
  309.         Serial.println("on");
  310.       }
  311.       else {
  312.         Serial.println("off");
  313.       }
  314.       digitalWrite(pbox, power ? HIGH : LOW);
  315.       digitalWrite(green, power ? HIGH : LOW);
  316.       digitalWrite(red, power ? LOW : HIGH);
  317.       digitalWrite(v5, power ? HIGH : LOW);
  318.       digitalWrite(v9, power ? HIGH : LOW);
  319.       p5v = power;
  320.       p9v = power;
  321.     }
  322.     if (Button == 2){                                           //toggles my 9V output.
  323.       p9v = !p9v;
  324.       Serial.print("9v power: ");
  325.       if (p9v){
  326.         Serial.println("on");
  327.       }
  328.       else {
  329.         Serial.println("off");
  330.       }
  331.       digitalWrite(v9, p9v ? HIGH : LOW);
  332.     }
  333.     if (Button == 3){                                           //toggles my 5V output.
  334.       p5v = !p5v;
  335.       Serial.print("5v power: ");
  336.       if (p5v){
  337.         Serial.println("on");
  338.       }
  339.       else {
  340.         Serial.println("off");
  341.       }
  342.       digitalWrite(v5, p5v ? HIGH : LOW);
  343.     }
  344.     if (Button == 4){
  345.       speed1 += 5;
  346.       delay (100);
  347.     }
  348.     if (Button == 5){                         //change speeds of fans.
  349.       speed1 -= 5;
  350.       delay (100);
  351.     }
  352.     /*if (input > 4){                                         //controlls my input selector.
  353.      input = 0;
  354.      }
  355.      if (input == 0){                                          //says mute every second
  356.      digitalWrite(mute, LOW);
  357.      Serial.println("mute");
  358.      }
  359.      else {
  360.      digitalWrite(mute, HIGH);
  361.      Serial.println("mute off");
  362.      }
  363.      if (input == 1){
  364.      digitalWrite(a, HIGH);
  365.      digitalWrite(b, HIGH);
  366.      Serial.println("input 1");
  367.      }
  368.      if (input == 2){
  369.      digitalWrite(a, LOW);
  370.      digitalWrite(b, HIGH);
  371.      Serial.println("input 2");
  372.      }
  373.      if (input == 3){
  374.      digitalWrite(a, HIGH);
  375.      digitalWrite(b, LOW);
  376.      Serial.println("input 3");
  377.      }
  378.      if (input == 4){
  379.      digitalWrite(a, LOW);
  380.      digitalWrite(b, LOW);
  381.      Serial.println("input 4");
  382.      }*/
  383.     last = millis();
  384.   }                                                         //end of every second things.
  385.   /*if (digitalRead(Button6) == LOW){
  386.    speed2 -= 1;
  387.    delay (100);
  388.    }
  389.    if (digitalRead(Button7) == LOW){
  390.    speed2 += 1;
  391.    delay (100);
  392.    }*/
  393.   if (speed1 < 0){                                          //limits range of speeds.
  394.     Serial.println("fan1 low");
  395.     speed1 = 0;
  396.   }
  397.   if (speed1 > 70){
  398.     Serial.println("fan1 high");
  399.     speed1 = 70;
  400.   }
  401.   /*if (speed2 < 0){
  402.    Serial.println("fan2 low");
  403.    speed2 = 0;
  404.    }
  405.    if (speed2 > 100){
  406.    Serial.println("fan2 high");
  407.    speed2 = 100;
  408.    }*/
  409.  if (lncdrvl != ncdrvl){
  410.    switch (ncdrvl % 6){
  411.      case 0:
  412.      digitalWrite(led1, LOW);
  413.      digitalWrite(led2, LOW);
  414.      digitalWrite(led3, LOW);
  415.      digitalWrite(led4, LOW);
  416.      digitalWrite(led5, LOW);
  417.      break;
  418.      case 1:
  419.      digitalWrite(led1, HIGH);
  420.      digitalWrite(led2, LOW);
  421.      digitalWrite(led3, LOW);
  422.      digitalWrite(led4, LOW);
  423.      digitalWrite(led5, LOW);
  424.      break;
  425.      case 2:
  426.      digitalWrite(led1, LOW);
  427.      digitalWrite(led2, HIGH);
  428.      digitalWrite(led3, LOW);
  429.      digitalWrite(led4, LOW);
  430.      digitalWrite(led5, LOW);
  431.      break;
  432.      case 3:
  433.      digitalWrite(led1, LOW);
  434.      digitalWrite(led2, LOW);
  435.      digitalWrite(led3, HIGH);
  436.      digitalWrite(led4, LOW);
  437.      digitalWrite(led5, LOW);
  438.      break;
  439.      case 4:
  440.      digitalWrite(led1, LOW);
  441.      digitalWrite(led2, LOW);
  442.      digitalWrite(led3, LOW);
  443.      digitalWrite(led4, HIGH);
  444.      digitalWrite(led5, LOW);
  445.      break;
  446.      case 5:
  447.      digitalWrite(led1, LOW);
  448.      digitalWrite(led2, LOW);
  449.      digitalWrite(led3, LOW);
  450.      digitalWrite(led4, LOW);
  451.      digitalWrite(led5, HIGH);
  452.      break;
  453.    }
  454.    Serial.print("encoder value:");
  455.    Serial.println(ncdrvl);
  456.    lncdrvl = ncdrvl;
  457.  }
  458. }
  459.  
  460. void updateEncoder(){                                      
  461.   int MSB = digitalRead(rtncdr1); //MSB = most significant bit
  462.   int LSB = digitalRead(rtncdr2); //LSB = least significant bit
  463.  
  464.   int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  465.   int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  466.  
  467.   if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) ncdrvl ++;
  468.   if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) ncdrvl --;
  469.  
  470.   lastEncoded = encoded; //store this value for next time
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement