Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*this teensy++2.0 is controlling my powerbox, 3050, 3090, fans W/PWM, surround processor/EVC ic.
- -takes the temp of 3050/3090 to enable auto fan speed.
- -controlls a LV1116N surround processor&EVC. input and volume work with some gain issues.
- -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: SuperBrainAK
- */
- #include <IRremote.h> //includes libraries.
- #include <IRremoteInt.h>
- #include <Wire.h>
- //const pin assignments
- const byte irin = 27;
- const byte rtncdr1 = 18;
- const byte rtncdr2 = 19;
- const byte temp = 45;
- const byte pbox = 12;
- const byte red = 17;
- const byte green = 13;
- const byte led1 = 25;
- const byte led2 = 24;
- const byte led3 = 14;
- const byte led4 = 15;
- const byte led5 = 16;
- 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;
- unsigned long last = millis(); //updates the time it has been
- volatile int lastEncoded = 0; //rotary encoder stuff.
- volatile long ncdrvl = 0;
- volatile long lncdrvl = 0;
- long lastencoderValue = 0;
- int lastMSB = 0;
- int lastLSB = 0;
- float Map = 10; //map variable = varmax/5.
- float ledvl = 0; //decimal for the leds var/Map.
- int led = 0; //the pwm state of the pin.
- float ledp = 0; //the percent of pwm to the led.
- boolean volctrl = 0; //will keep controlling the volume.
- byte vol = 48; //variable for volume.
- byte lvol = 0; //last volume.
- boolean startup = 0; //runs startup sequence if=0.
- boolean power = 0; //my main power state.
- byte input = 3; //input selection 0=mute 1 = A etc.
- byte linput = 0; //last input #.
- boolean p5v = 0; //my 5V power state.
- boolean p9v = 0; //my 9V power state.
- boolean spdcntrl = 0; //will keep controlling the fan
- boolean start1 = 0; //startup for fan1.
- byte speed1 = 0; //speed of fan1.
- //byte speed2 = 0; //speed of fan2.
- byte lspeed1 = 0; //last speed of fan1.
- //byte lspeed2 = 0; //last speed of fan2.
- int bval = 0; //button value.
- int lbval = 0; //last button value.
- byte Button = 0; //wich button.
- int tval = 0; //temperature value.
- int ltval = 0; //last temp. value.
- void setup () {
- pinMode(pbox, OUTPUT); //pin modes
- 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);
- Wire.begin();
- irrecv.enableIRIn();
- }
- void loop() {
- if (!startup){ //sets the states of my sensitive outputs.
- digitalWrite(red, HIGH);
- digitalWrite(v5, LOW);
- digitalWrite(v9, LOW);
- Wire.beginTransmission(119); //begins transmitting to the chip.
- Wire.send(1); //the sub address for input.
- Wire.send(0); //puts it to mute.
- Wire.endTransmission(); //ends the transmission.
- 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){ //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){
- Serial.println("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: ");
- (power) ? Serial.println("on") : 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;
- input = power;
- }
- if (Button == 2){ //toggles my 9V output.
- p9v = !p9v;
- Serial.print("9v power: ");
- (p9v) ? Serial.println("on") : Serial.println("off");
- digitalWrite(v9, p9v ? HIGH : LOW);
- }
- if (Button == 3){ //toggles my 5V output.
- p5v = !p5v;
- Serial.print("5v power: ");
- (p5v) ? Serial.println("on") : Serial.println("off");
- digitalWrite(v5, p5v ? HIGH : LOW);
- }
- if (Button == 4){ //toggles controll for fan1.
- spdcntrl = !spdcntrl;
- Serial.print("control fan speed: ");
- (spdcntrl) ? Serial.println("yes") : Serial.println("no");
- if (spdcntrl){
- Map = 70/5; //creats the map for speed.
- }
- else {
- Map = 10; //resets the map #.
- }
- }
- if (Button == 5){ //cycles my inputs.
- input ++;
- }
- if (Button == 6){ //toggles controll for v1.
- volctrl = !volctrl;
- Serial.print("control volume: ");
- (volctrl) ? Serial.println("yes") : Serial.println("no");
- if (volctrl){
- Map = 48/5; //creats the map for volume.
- }
- else {
- Map = 10; //resets the map #.
- }
- }
- last = millis();
- } //end of every second things.
- /*if (digitalRead(Button6) == LOW){
- speed2 -= 1;
- delay (100);
- }
- if (digitalRead(Button7) == LOW){
- speed2 += 1;
- delay (100);
- }*/
- if (input > 3){ //loops the inputs.
- input = 0;
- }
- if (lncdrvl != ncdrvl){ //handles the variables and led graph.
- if (volctrl == 1){
- if (ncdrvl > lncdrvl){
- vol ++;
- }
- if (ncdrvl < lncdrvl){
- vol --;
- }
- if (vol < 0){ //limits range of volume.
- Serial.println("volume low");
- vol = 0;
- }
- if (vol > 48){
- Serial.println("volume high");
- vol = 48;
- }
- ledvl = vol/Map;
- Serial.print("volume is: ");
- Serial.println(vol);
- }
- if (spdcntrl == 1){
- if (ncdrvl > lncdrvl){
- speed1 ++;
- }
- if (ncdrvl < lncdrvl){
- speed1 --;
- }
- if (speed1 < 0){ //limits range of fan1.
- Serial.println("fan1 low");
- speed1 = 0;
- }
- if (speed1 > 70){
- Serial.println("fan1 high");
- speed1 = 70;
- }
- ledvl = speed1/Map;
- Serial.print("speed is: ");
- Serial.println(speed1);
- }
- if (ledvl == 0){
- digitalWrite(led1, LOW);
- digitalWrite(led2, LOW);
- digitalWrite(led3, LOW);
- digitalWrite(led4, LOW);
- digitalWrite(led5, LOW);
- }
- if (1 > ledvl && ledvl > 0){
- led = ledvl * 255;
- analogWrite(led1, led);
- }
- if (ledvl >= 1){
- digitalWrite(led1, HIGH);
- digitalWrite(led2, LOW);
- }
- if (2 > ledvl && ledvl > 1){
- ledp = ledvl - 1;
- led = ledp * 255;
- analogWrite(led2, led);
- }
- if (ledvl >= 2){
- digitalWrite(led2, HIGH);
- digitalWrite(led3, LOW);
- }
- if (3 > ledvl && ledvl > 2){
- ledp = ledvl - 2;
- led = ledp * 255;
- analogWrite(led3, led);
- }
- if (ledvl >= 3){
- digitalWrite(led3, HIGH);
- digitalWrite(led4, LOW);
- }
- if (4 > ledvl && ledvl > 3){
- ledp = ledvl - 3;
- led = ledp * 255;
- analogWrite(led4, led);
- }
- if (ledvl >= 4){
- digitalWrite(led4, HIGH);
- digitalWrite(led5, LOW);
- }
- if (5 > ledvl && ledvl > 4){
- ledp = ledvl - 4;
- led = ledp * 255;
- analogWrite(led5, led);
- }
- if (ledvl == 5){
- digitalWrite(led5, HIGH);
- }
- Serial.print("ledvalue is: ");
- Serial.println(ledvl);
- lncdrvl = ncdrvl;
- }
- if (input != linput){ //sees if it needs to change the input.
- Wire.beginTransmission(119);
- Wire.send(1); //the sub address for input.
- Wire.send(input); //sets the input.
- Wire.endTransmission();
- linput = input;
- Serial.print("input is: "); //says the input value.
- Serial.println(input);
- }
- if (vol != lvol){ //sees if it needs to change the volume.
- Wire.beginTransmission(119);
- Wire.send(2); //the sub address for volume.
- Wire.send(vol + 192); //sets the volume(the 192 comes from controlling both channels)
- Wire.endTransmission();
- lvol = vol;
- Serial.print("volume is: "); //says the volume.
- Serial.println(vol);
- }
- }
- 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