Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Media center build, ir remote only works with serial connection

  1. #1
    Junior Member
    Join Date
    Apr 2015
    Posts
    4

    Media center build, ir remote only works with serial connection

    Hello everyone,

    For my project I tried to use an HD44780 as Matrix Orbital with temp/fan controller, remote control and power switch on my Openelec media center.
    My problem is when i put all the code together my ir remote wont work unless is connected to serial and because of that i cant power on, i've used many hours trying different thing but i cant figure out what im doing wrong.

    Im using a Teensy2.0++

    Im a noob with out any experience and im trying to learn, so please be kind.

    Attached my code.


    Code:
     
    #include <math.h>
    #include <EEPROM.h>
    #include <IRremote.h>
    #include <LiquidCrystal.h>
    
    LiquidCrystal lcd(13, 12, 11, 7, 8, 9, 10);
    
    int pinButton = 2; //the pin where we connect the button
    int fan = 16;       // the pin where HDD fan is
    int fan2 = 0;       // the pin where CPU fan is
    int relayPin = 5; // pin out for relay pin
    int powerLed = 27; // pin out for Power led
    int blueLed = 31; // pin out for Blue led
    int greenLed = 30; // pin out for Green led
    int RECV_PIN = 4; // pin out for reciever pin
    
    int tempMin = 30;   // the temperature to start the fan
    int tempMax = 60;   // the maximum temperature when fan is at 100%
    int fanSpeed;
    int fanSpeed2;
    int fanLCD;
    int fanLCD2;
    
    const int skipRepeat = 3;
    int repeatCount = 0;
    int lastBtn = 0;
    IRrecv irrecv(RECV_PIN);
    decode_results results;
    
    // Bit Rate - using 19200 but LCD Smartie defaults to 9600
    const int baud = 19200;
    
    // EEPROM address values
    const byte BRIGHTNESS = 0;  // Brightness
    const byte CONTRAST = 1;  // Contrast
    const byte SERIAL_LO = 2; // Serial number low byte
    const byte SERIAL_HI = 3; // Serial number high byte
    
    // IO Pins
    const byte GPIO = 6;   // D13 - Built in LED on Nano V3.0
    const byte backLight = 15;  // D10 - Use PWM to change brightness
    const byte contrast = 14;    // D3  - Use PWM to change contrast 
    
    // Variables used in code - can probably optimise some of these out....
    byte rxbyte;
    byte temp;
    byte addr;
    byte level;
    int val;
    int i;
    byte data[8];  // buffer for user character data
    
    // Temp Sensor
    #define Thermistor1PIN 6                 // Analog Pin 6 HDD
    #define Thermistor2PIN 7                 // Analog Pin 7 CPU
    float vcc = 5.06;                       // only used for display purposes, if used set to the measured Vcc.
    float pad7 = 9950;                      // balance/pad resistor value, set this to
    float pad6 = 9930;                      // the measured resistance of your pad resistor
    float thermr = 10000;                   // thermistor nominal resistance
    
    float Thermistor1(int RawADC) {
      long Resistance;  
      float Temp1;  // Dual-Purpose variable to save space.
      Resistance=pad6*((1024.0 / RawADC) - 1); 
      Temp1 = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
      Temp1 = 1 / (0.001129148 + (0.000234125 * Temp1) + (0.0000000876741 * Temp1 * Temp1 * Temp1));
      Temp1 = Temp1 - 273.15;  // Convert Kelvin to Celsius                      
      return Temp1;                                      // Return the Temperature
    }
    
    float Thermistor2(int RawADC) {
      long Resistance;  
      float Temp2;  // Dual-Purpose variable to save space.
      Resistance=pad7*((1024.0 / RawADC) - 1); 
      Temp2 = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
      Temp2 = 1 / (0.001129148 + (0.000234125 * Temp2) + (0.0000000876741 * Temp2 * Temp2 * Temp2));
      Temp2 = Temp2 - 273.15;  // Convert Kelvin to Celsius                      
      return Temp2;                                      // Return the Temperature
    }
    
    // Definer 8 karakter-mnstre
    byte newChar0[8] = { B10000,B01000,B00100,B00010,B00001,B00010,B00100,B00000}; // >
    byte Lae[8] ={ B00000,B00000,B11010,B00101,B01111,B10100,B11111,B00000}; // 
    byte Loe[8] ={ B00000,B00001,B01110,B10101,B10101,B01110,B10000,B00000}; // 
    byte Laa[8] ={ B00100,B00000,B01110,B00001,B01111,B10001,B01111,B00000}; // 
    byte Sae[8] ={ B01111,B10100,B10100,B11110,B10100,B10100,B10111,B00000}; // 
    byte Soe[8] ={ B00001,B01110,B10011,B10101,B11001,B01110,B10000,B00000}; // 
    byte Saa[8] ={ B00100,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // 
    byte VT[8] = { B11111,B10101,B10101,B01110,B01110,B00100,B00100,B00000}; // VT
    
    
    void sendKey(int key) //Send a single key
    {
      Keyboard.set_key1(key);
      Keyboard.send_now();    
      Keyboard.set_key1(0);
      Keyboard.send_now();  
    }
    
    void sendForButton(int btn) //Map the button and send the command on the Keyboard
    {
      if (btn == 0xFFFFFFFF){   
        repeatCount+=1;
        if (repeatCount > skipRepeat){
            btn = lastBtn;
        } else {
          return;
        }
      } else {
        repeatCount = 0;
        lastBtn = btn;
      }
      
      switch (btn) {
        case 0x41E: sendKey(KEY_UP); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //UP
        case 0x41F: sendKey(KEY_DOWN); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //DOWN
        case 0x420: sendKey(KEY_LEFT); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //LEFT
        case 0x421: sendKey(KEY_RIGHT); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RIGHT
        case 0x422: sendKey(KEY_ENTER); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //OK
        case 0x423: sendKey(KEY_BACKSPACE); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //BACK
        case 0x415: Keyboard.print("r"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //SPOL TILBAGE
        case 0x40C: digitalWrite(relayPin, HIGH); delay(500); digitalWrite(relayPin, LOW); digitalWrite(blueLed, LOW); delay(10000); digitalWrite(blueLed, HIGH); break; // POWER ON PC
        case 0x414: Keyboard.print("f"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //SPOL FREM
        case 0x419: Keyboard.print("x"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;  //STOP
        case 0x416: Keyboard.print(" "); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //PLAY
        case 0x412: sendKey(KEY_PAGE_UP); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //CHANNEL UP
        case 0x413: sendKey(KEY_PAGE_DOWN); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //CHANNEL DOWN
        case 0x40D: Keyboard.print("c"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //WINDOWS
        case 0x40F: Keyboard.print("i"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //INFO
        case 0x424: Keyboard.print("m"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //DVD MENU
        case 0x448: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RECORDED TV
        case 0x426: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //GUIDE
        case 0x425: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //LIVE TV
        case 0x41B: Keyboard.print(","); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //HOP TILBAGE
        case 0x41A: Keyboard.print("."); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //HOP FREM
        case 0x40E: sendKey(KEY_F8); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //MUTE
        case 0x411: sendKey(KEY_F9); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //VOLUME DOWN
        case 0x410: sendKey(KEY_F10); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //VOLUME UP
        case 0x400: Keyboard.print("0"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //0
        case 0x401: Keyboard.print("1"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //1
        case 0x402: Keyboard.print("2"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x403: Keyboard.print("3"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x404: Keyboard.print("4"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x405: Keyboard.print("5"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x406: Keyboard.print("6"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x407: Keyboard.print("7"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x408: Keyboard.print("8"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x409: Keyboard.print("9"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //9
        case 0x41D: Keyboard.print("*"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //*
        case 0x41C: Keyboard.print("#"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //#
        case 0x417: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RECORD
        case 0x40A: Keyboard.print("KEY_ESC"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //CLEAR
        case 0x40B: sendKey(KEY_ENTER); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //ENTER
        case 0x45A: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //TELETEXT
        case 0x45B: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RD
        case 0x45C: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //GRN
        case 0x45D: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //GUL
        case 0x45E: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //BL
        case 0x44A: Keyboard.print("t"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //TEST
        default: digitalWrite (blueLed, HIGH);
       }
    }
    
    void setup() {
      pinMode(fan, OUTPUT);  
      pinMode(fan2, OUTPUT);  
      irrecv.enableIRIn();
      pinMode(blueLed, OUTPUT); digitalWrite(blueLed, HIGH); // turn off the blue. 
      pinMode(greenLed, OUTPUT); digitalWrite(greenLed, HIGH); // turn off the green.
      pinMode(powerLed, OUTPUT); analogWrite(powerLed, LOW); // turn off the pled. 
      
      pinMode(relayPin, OUTPUT); digitalWrite(relayPin, LOW); // turn off the relay. 
    
      pinMode(pinButton, INPUT); //set the button pin as INPUT
        
      // Set the use ouf our output pins
      pinMode(GPIO, OUTPUT);
      pinMode(backLight, OUTPUT);
      pinMode(contrast, OUTPUT);
      
    // Gather default values from EEPROM (could add code here to handle default if EEPROM has never been written to)
      digitalWrite(GPIO, LOW);
      analogWrite(backLight, EEPROM.read(BRIGHTNESS));    //  Read brightness level out of EEPROM
      analogWrite(contrast, EEPROM.read(CONTRAST));       // Read contrast level out of EEPROM
    
      // set up the LCD's number of columns and rows: 
      lcd.begin(20, 4); lcd.clear();
      lcd.print(" Display - PS3, DK ");
    
      // Custom Characters for the HTPS Logo
      byte htps1[8] = { B10001, B10001, B10001, B11111, B11111, B10001, B10001, B10001 };
      byte htps2[8] = { B11111, B11111, B00100, B00100, B00100, B00100, B00100, B00100 };
      byte htps3[8] = { B01110, B11011, B10001, B11011, B11110, B10000, B10000, B10000 };
      byte htps4[8] = { B01110, B11011, B10001, B11000, B01110, B00001, B11001, B01110 };
      lcd.createChar(4, htps1);
      lcd.createChar(5, htps2);
      lcd.createChar(6, htps3);
      lcd.createChar(7, htps4);
      lcd.setCursor(1, 2);
      lcd.write(4);
      lcd.setCursor(2, 2);
      lcd.write(5);
      lcd.setCursor(3, 2);
      lcd.write(6);
      lcd.setCursor(4, 2);
      lcd.write(7);
      lcd.setCursor(8, 2);
      // End of custome character generation and display
    
      lcd.print("Input Ready");
      lcd.setCursor(8, 3);
      lcd.print("19200,8,N,1");
    
    
      Serial.begin(baud);
      // Wait until we receive some data - note we want to hold what data it is as we use it the first iteration
      rxbyte = serial_getch();
    
      // Clear display, wait a little and load in the custom icons 
      lcd.clear(); delay(200);
    
      // Generate 3 different bar graph values for 33% 66% and 100%
      byte bara[8] = { B10000, B10000, B10000, B10000, B10000, B10000, B11111, B10000 };
      byte barb[8] = { B11100, B11100, B11100, B11100, B11100, B11100, B11111, B11100 };
      byte barc[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };
    
      // Characters for CPU, Nivida Graphics Card, C: drive and D: drive and degrees symbol (DegC)
      byte cpu[8] = { B10111, B10101, B10111, B00000, B11101, B10101, B11101, B00000 };
      byte nv[8] = { B11101, B10110, B00000, B11111, B11101, B11111, B01100, B00000 };
      byte cdrive[8] = { B01100, B10001, B10000, B01101, B00000, B11111, B11101, B11111 };
      byte ddrive[8] = { B11000, B10101, B10100, B11001, B00000, B11111, B11101, B11111 };
      byte degs[8] = { B11000, B11000, B00000, B00110, B01000, B01000, B00110, B00000 };
    
      // Write characters into LCD RAM
     lcd.createChar(0, newChar0); // upload 8 selvdef. karakterer
     lcd.createChar(1, barc); lcd.createChar(2, bara); lcd.createChar(3, barb); lcd.createChar(4, cpu); 
     lcd.createChar(5, nv); lcd.createChar(6, cdrive); lcd.createChar(7, ddrive); lcd.createChar(8, degs); 
     lcd.createChar(9, VT); // VT ( ;-) 
     lcd.createChar(10, Lae); // 
     lcd.createChar(11, Loe); // 
     lcd.createChar(12, Laa); // 
     lcd.createChar(13, Sae); // 
     lcd.createChar(14, Soe); // 
     lcd.createChar(15, Saa); // 
     delay(20); lcd.clear();
    }
    
    // Helper function to read serial input as clean 8 bit byte
    byte serial_getch(){ int ch; while (Serial.available() == 0){} ch = Serial.read(); return (byte)(ch & 0xff); } // read the incoming byte:
    
    
    void loop() {
      float temp1; temp1=Thermistor1(analogRead(Thermistor1PIN));       // read ADC and  convert it to Celsius
      float temp2; temp2=Thermistor2(analogRead(Thermistor2PIN));       // read ADC and  convert it to Celsius
       if(temp1 < tempMin) {   // if temp is lower than minimum temp
           fanSpeed = 0;      // fan is not spinning
           analogWrite(fan, 0);    
           } 
       if((temp1 >= tempMin) && (temp1 <= tempMax)) {  // if temperature is higher than minimum temp
           fanSpeed = map(temp1, tempMin, tempMax, 32, 255); // the actual speed of fan
           fanLCD = map(temp1, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
           analogWrite(fan, fanSpeed);  // spin the fan at the fanSpeed speed
       } 
       
       if(temp1 > tempMax) {        // if temp is higher than tempMax
         digitalWrite(greenLed, LOW);  // turn on led 
       } else {                    // else turn of led
         digitalWrite(greenLed, HIGH); 
       }
       
       if(temp2 < tempMin) {   // if temp is lower than minimum temp
       fanSpeed2 = 0;      // fan is not spinning
       analogWrite(fan2, 0);       
       } 
       if((temp2 >= tempMin) && (temp2 <= tempMax)) {  // if temperature is higher than minimum temp
           fanSpeed2 = map(temp2, tempMin, tempMax, 32, 255); // the actual speed of fan
           fanLCD2 = map(temp2, tempMin, tempMax, 0, 100);  // speed of fan to display on LCD
           analogWrite(fan2, fanSpeed);  // spin the fan at the fanSpeed speed
       } 
       
       if(temp2 > tempMax) {        // if temp is higher than tempMax
         digitalWrite(greenLed, LOW);  // turn on led 
       } else {                    // else turn of led
         digitalWrite(greenLed, HIGH); 
       }
        
      if (irrecv.decode(&results)) { results.value &=0x7ff; sendForButton(results.value); delay(100); irrecv.resume(); } // Receive the next IR REMOTE value
    
       if (rxbyte == 254) { //Matrix Orbital uses 254 prefix for commands
        switch (serial_getch()) {
        case 52: EEPROM.write(SERIAL_HI, serial_getch()); EEPROM.write(SERIAL_LO, serial_getch()); break; //set serial number /*USING EEPROM*/
        case 53: Serial.write(EEPROM.read(SERIAL_HI)); Serial.write(EEPROM.read(SERIAL_LO)); break; //read serial number /*USING EEPROM*/
        case 54: Serial.write(0x11); break; //read version number v1.1
        case 55: Serial.write(0x05); break; //read module lcd_type = 'LCD2041'
        case 59: break; //exit flow-control mode
        case 64: addr = serial_getch(); val = serial_getch(); EEPROM.write(addr, val); break; // EEPROM Write (address, value) 
        case 65: addr = serial_getch(); val = EEPROM.read(addr); Serial.print(val); break; // EEPROM Read  (address)
        case 66: temp = serial_getch(); break; //backlight on (at previously set brightness) NOT implemented      
        case 67: //auto line-wrap on
        case 68: break; //auto line-wrap off
        case 70: analogWrite(backLight, 0); analogWrite(powerLed, 0); break; //backlight and powerled off
        case 71: temp = (serial_getch() - 1); lcd.setCursor(temp, serial_getch() - 1); break; //set cursor position
        case 72: lcd.setCursor(0, 0); break; //cursor home (reset display position)
        case 74: lcd.command(0b00001110); break; //show underline cursor
        case 75:  //underline cursor off
        case 84: lcd.command(0b00001100); break; //block cursor off
        case 76: lcd.command(16); break;  //move cursor left    
        case 77: lcd.command(20); break;  //move cursor right
        case 78: temp = serial_getch(); for (i = 0; i < 8; i++){ data[i] = serial_getch(); } lcd.createChar(temp, data); break; //define custom char
        case 80:  // Set contrast (but we save anyway)
        case 145: level = 255 - serial_getch(); analogWrite(contrast, level); EEPROM.write(CONTRAST, level); break; // Set Contrast and save
        case 81: //auto scroll on
        case 82: break; //auto scroll off
        case 83: lcd.command(0b00001111); break; //show blinking block cursor
        case 86: temp = serial_getch(); digitalWrite(GPIO, LOW); break; //GPIO OFF
        case 87: temp = serial_getch(); digitalWrite(GPIO, HIGH); break; //GPIO ON
        case 88: lcd.clear(); break; //clear display, cursor home
        case 96: break; //auto-repeat mode off (keypad)
        case 98: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //Draw bitmap
        case 99: temp = serial_getch(); break; // set drawing color
        case 101: temp = serial_getch(); temp = serial_getch(); break; //Draw line continue
        case 104: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //init horiz bar graph
        case 108: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break;
        case 109: break; //init med size digits
        case 112:  temp = serial_getch(); temp = serial_getch(); break; // Draw Pixel
        case 114:  temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //draw rect
        case 115: //init narrow vert bar graph
        case 118: break; //init wide vert bar graph
        case 120: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //draw rect solid
        // case 145:  //defined above see case 80:
        case 152: level = serial_getch(); analogWrite(backLight, level); EEPROM.write(BRIGHTNESS, level); break; //set brightness and save 
        case 153: level = serial_getch(); analogWrite(backLight, level); analogWrite(powerLed, level); break;//set backlight and powerled brightness
        default: temp = serial_getch(); break; } //all other commands ignored and parameter byte discarded
      } //END OF COMMAND HANDLER
      else {
        // Not a command character but still might be a special character
        //change accented char to plain, detect and change descenders
        switch (rxbyte) {
          //chars that have direct equivalent in LCD charmap
          case 0xE4: rxbyte = 0xE1; break; //ASCII "a" umlaut
          case 0xF1: rxbyte = 0xEE; break; //ASCII "n" tilde
          case 0xF6: rxbyte = 0xEF; break; //ASCII "o" umlaut
          case 0xFC: rxbyte = 0xF5; break; //ASCII "u" umlaut //accented -> plain equivalent and misc symbol translation
          case 0xA3: rxbyte = 0xED; break; //sterling (pounds)
          case 0xAC: rxbyte = 0xB0; break; //sterling (pounds)
          case 0xB0: rxbyte = 0xDF; break; //degrees symbol
          case 0xB5: rxbyte = 0xE4; break; //mu
          case 0xC0: //"A" variants
          case 0xC1:
          case 0xC2:
          case 0xC3:
          case 0xC4:
          case 0xC5: rxbyte = 0x41; break;
          case 0xC8: //"E" variants
          case 0xC9:
          case 0xCA:
          case 0xCB: rxbyte = 0x45; break;
          case 0xCC: //"I" variants
          case 0xCD:
          case 0xCE:
          case 0xCF: rxbyte = 0x49; break;
          case 0xD1: rxbyte = 0x43; break; //"N" tilde -> plain "N"
          case 0xD2: //"O" variants
          case 0xD3:
          case 0xD4:
          case 0xD5:
          case 0xD6:
          case 0xD8: rxbyte = 0x4F; break;
          case 0xD9: //"U" variants
          case 0xDA:
          case 0xDB:
          case 0xDC: rxbyte = 0x55; break;
          case 0xDD: rxbyte = 0x59; break; //"Y" acute -> "Y"
          case 0xE0: //"a" variants except umlaut
          case 0xE1:
          case 0xE2:
          case 0xE3:
          case 0xE5: rxbyte = 0x61; break;
          case 0xE7: rxbyte = 0x63; break; //"c" cedilla -> "c"
          case 0xE8: //"e" variants
          case 0xE9:
          case 0xEA:
          case 0xEB: rxbyte = 0x65; break;
          case 0xEC: //"i" variants
          case 0xED:
          case 0xEE:
          case 0xEF: rxbyte = 0x69; break;
          case 0xDF: //beta  // LCDSmartie degree symbol??
          case 0xF2: //"o" variants except umlaut
          case 0xF3:
          case 0xF4:
          case 0xF5:
          case 0xF8: rxbyte = 0x6F; break;
          case 0xF7: rxbyte = 0xFD; break; //division symbol
          case 0xF9: //"u" variants except umlaut
          case 0xFA:
          case 0xFB: rxbyte = 0x75; break;
          default: break;
        }
        // By now either special character is converted or it was printable already
        lcd.print((char)rxbyte);  //print it to lcd
      }
      rxbyte = serial_getch();    // Wait for the next byte and use value for next iteration
    }
    I think it must have something to do with the rxbyte = serial_getch(); command, but im not sure. Is there a way that i can bypass it so my remote always work or just the power on command?

    I hope a kind soul can point me in the right direction so i better can understand this problem and fix it.

    Sorry if my english is bad, i try my best.
    Last edited by petanque; 04-04-2016 at 12:53 AM. Reason: error in code, can compile now

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,810
    I see the Teensy3 serial_getch() returns -1 when there are no characters ready, it does not wait for a character to be ready?

    I don't see that Arduino or Teensy2 defines a serial_getch(), but instead has a read() - that acts the same and returns -1 when there are no characters ready.

    You are not using Teensy3: I'm not sure what I'm missing that it works for you to even compile - but in either case - you should expect and test for a -1 return and not expect every call to wait for and get a character.

    Perhaps you need to buffer the incoming characters, or put a wrapper around your function that will not return until a character is received. Of course doing that means nothing will happen in your program until you get the character or time out. If you allow for a time out, then there will be times again when you cannot assume a valid character was always returned.

  3. #3
    Junior Member
    Join Date
    Apr 2015
    Posts
    4
    I messed up in my first code upload, its fixed and can compile on my end..

    most of my code i "stole" from https://github.com/albal/Matrix/blob/master/matrix.ino
    its is sketch that makes a hd44780 into a matrix orbital like display, it for the arduino nano but it works on my teensy2++

    thank you for your advice, will return when I have tested some more.

  4. #4
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    Have you tested your IR code to see if it recognizes the IR signals from your remote ?

  5. #5
    Junior Member
    Join Date
    Apr 2015
    Posts
    4
    The remote works fine when im connected with lcdsmartie or lcdproc, and stops when i disconnected.

    Its like the loop stops when im not using serial, maybe i overlooked something. Cant figure it out right now.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,810
    Given the definition of serial_getch() - any call to it will wait forever when there is no serial data coming in. This happens in setup() and again at the bottom of your loop based on the updated code in OP.

    // Helper function to read serial input as clean 8 bit byte
    byte serial_getch(){ int ch; while (Serial.available() == 0){} ch = Serial.read(); return (byte)(ch & 0xff); } // read the incoming byte:


    // Wait until we receive some data - note we want to hold what data it is as we use it the first iteration
    rxbyte = serial_getch();

    rxbyte = serial_getch(); // Wait for the next byte and use value for next iteration
    Last edited by defragster; 04-05-2016 at 05:39 AM.

  7. #7
    Junior Member
    Join Date
    Apr 2015
    Posts
    4
    Thanks for your guidance defragster, without it I would probably still be stuck.

    If i changed while or Serial.available() == 0){} i would get garbage on screen. Im still learning so my fix is probably not the best way to do this.

    I created a second loop inside Serial.available() == 0){ loop2() } I then moved my remote and temp/fan controller from loop to loop2 and now it works

    My new code:
    Code:
     
    
    #include <math.h>
    #include <EEPROM.h>
    #include <IRremote.h>
    #include <LiquidCrystal.h>
    
    LiquidCrystal lcd(13, 12, 11, 7, 8, 9, 10);
    
    int pinButton = 2; //the pin where we connect the button
    int fan = 16;       // the pin where HDD fan is
    int fan2 = 0;       // the pin where CPU fan is
    int relayPin = 5; // pin out for relay pin
    int powerLed = 27; // pin out for Power led
    int blueLed = 31; // pin out for Blue led
    int greenLed = 30; // pin out for Green led
    int RECV_PIN = 4; // pin out for reciever pin
    
    int tempMin = 20;   // the temperature to start the fan
    int tempMax = 60;   // the maximum temperature when fan is at 100%
    int tempGreen = 35; // the temperature when green light turns on
    int fanSpeed;
    int fanSpeed2;
    int fanLCD;
    int fanLCD2;
    
    const int skipRepeat = 3;
    int repeatCount = 0;
    int lastBtn = 0;
    IRrecv irrecv(RECV_PIN);
    decode_results results;
    
    // Bit Rate - using 19200 but LCD Smartie defaults to 9600
    const int baud = 19200;
    
    // EEPROM address values
    const byte BRIGHTNESS = 0;  // Brightness
    const byte CONTRAST = 1;  // Contrast
    const byte SERIAL_LO = 2; // Serial number low byte
    const byte SERIAL_HI = 3; // Serial number high byte
    
    // IO Pins
    const byte GPIO = 6;   // D13 - Built in LED on Nano V3.0
    const byte backLight = 15;  // D10 - Use PWM to change brightness
    const byte contrast = 14;    // D3  - Use PWM to change contrast 
    
    // Variables used in code - can probably optimise some of these out....
    byte rxbyte;
    byte temp;
    byte addr;
    byte level;
    int val;
    int i;
    byte data[8];  // buffer for user character data
    
    // Temp Sensor
    #define Thermistor1PIN 6                 // Analog Pin 6 HDD
    #define Thermistor2PIN 7                 // Analog Pin 7 CPU
    float vcc = 5.06;                       // only used for display purposes, if used set to the measured Vcc.
    float pad7 = 9950;                      // balance/pad resistor value, set this to
    float pad6 = 9930;                      // the measured resistance of your pad resistor
    float thermr = 10000;                   // thermistor nominal resistance
    
    float Thermistor1(int RawADC) {
      long Resistance;  
      float Temp1;  // Dual-Purpose variable to save space.
      Resistance=pad6*((1024.0 / RawADC) - 1); 
      Temp1 = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
      Temp1 = 1 / (0.001129148 + (0.000234125 * Temp1) + (0.0000000876741 * Temp1 * Temp1 * Temp1));
      Temp1 = Temp1 - 273.15;  // Convert Kelvin to Celsius                      
      return Temp1;                                      // Return the Temperature
    }
    
    float Thermistor2(int RawADC) {
      long Resistance;  
      float Temp2;  // Dual-Purpose variable to save space.
      Resistance=pad7*((1024.0 / RawADC) - 1); 
      Temp2 = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
      Temp2 = 1 / (0.001129148 + (0.000234125 * Temp2) + (0.0000000876741 * Temp2 * Temp2 * Temp2));
      Temp2 = Temp2 - 273.15;  // Convert Kelvin to Celsius                      
      return Temp2;                                      // Return the Temperature
    }
    
    // Definer 8 karakter-mnstre
    byte newChar0[8] = { B10000,B01000,B00100,B00010,B00001,B00010,B00100,B00000}; // >
    byte Lae[8] ={ B00000,B00000,B11010,B00101,B01111,B10100,B11111,B00000}; // 
    byte Loe[8] ={ B00000,B00001,B01110,B10101,B10101,B01110,B10000,B00000}; // 
    byte Laa[8] ={ B00100,B00000,B01110,B00001,B01111,B10001,B01111,B00000}; // 
    byte Sae[8] ={ B01111,B10100,B10100,B11110,B10100,B10100,B10111,B00000}; // 
    byte Soe[8] ={ B00001,B01110,B10011,B10101,B11001,B01110,B10000,B00000}; // 
    byte Saa[8] ={ B00100,B00000,B01110,B10001,B11111,B10001,B10001,B00000}; // 
    byte VT[8] = { B11111,B10101,B10101,B01110,B01110,B00100,B00100,B00000}; // VT
    
    
    void sendKey(int key) //Send a single key
    {
      Keyboard.set_key1(key);
      Keyboard.send_now();    
      Keyboard.set_key1(0);
      Keyboard.send_now();  
    }
    
    void sendForButton(int btn) //Map the button and send the command on the Keyboard
    {
      if (btn == 0xFFFFFFFF){   
        repeatCount+=1;
        if (repeatCount > skipRepeat){
            btn = lastBtn;
        } else {
          return;
        }
      } else {
        repeatCount = 0;
        lastBtn = btn;
      }
      
      switch (btn) {
        case 0x41E: sendKey(KEY_UP); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //UP
        case 0x41F: sendKey(KEY_DOWN); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //DOWN
        case 0x420: sendKey(KEY_LEFT); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //LEFT
        case 0x421: sendKey(KEY_RIGHT); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RIGHT
        case 0x422: sendKey(KEY_ENTER); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //OK
        case 0x423: sendKey(KEY_BACKSPACE); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //BACK
        case 0x415: Keyboard.print("r"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //SPOL TILBAGE
        case 0x40C: digitalWrite(relayPin, HIGH); delay(500); digitalWrite(relayPin, LOW); digitalWrite(blueLed, LOW); delay(10000); digitalWrite(blueLed, HIGH); break; // POWER ON PC
        case 0x414: Keyboard.print("f"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //SPOL FREM
        case 0x419: Keyboard.print("x"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;  //STOP
        case 0x416: Keyboard.print(" "); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //PLAY
        case 0x412: sendKey(KEY_PAGE_UP); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //CHANNEL UP
        case 0x413: sendKey(KEY_PAGE_DOWN); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //CHANNEL DOWN
        case 0x40D: Keyboard.print("c"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //WINDOWS
        case 0x40F: Keyboard.print("i"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //INFO
        case 0x424: Keyboard.print("m"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //DVD MENU
        case 0x448: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RECORDED TV
        case 0x426: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //GUIDE
        case 0x425: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //LIVE TV
        case 0x41B: Keyboard.print(","); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //HOP TILBAGE
        case 0x41A: Keyboard.print("."); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //HOP FREM
        case 0x40E: sendKey(KEY_F8); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //MUTE
        case 0x411: sendKey(KEY_F9); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //VOLUME DOWN
        case 0x410: sendKey(KEY_F10); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //VOLUME UP
        case 0x400: Keyboard.print("0"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //0
        case 0x401: Keyboard.print("1"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //1
        case 0x402: Keyboard.print("2"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x403: Keyboard.print("3"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x404: Keyboard.print("4"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x405: Keyboard.print("5"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x406: Keyboard.print("6"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x407: Keyboard.print("7"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x408: Keyboard.print("8"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break;
        case 0x409: Keyboard.print("9"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //9
        case 0x41D: Keyboard.print("*"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //*
        case 0x41C: Keyboard.print("#"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //#
        case 0x417: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RECORD
        case 0x40A: Keyboard.print("KEY_ESC"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //CLEAR
        case 0x40B: sendKey(KEY_ENTER); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //ENTER
        case 0x45A: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //TELETEXT
        case 0x45B: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //RD
        case 0x45C: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //GRN
        case 0x45D: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //GUL
        case 0x45E: digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //BL
        case 0x44A: Keyboard.print("t"); digitalWrite(blueLed, LOW); delay(200); digitalWrite(blueLed, HIGH); break; //TEST
        default: digitalWrite (blueLed, HIGH);
       }
    }
    
    void setup() {
      pinMode(fan, OUTPUT);  
      pinMode(fan2, OUTPUT);  
      irrecv.enableIRIn();
      pinMode(blueLed, OUTPUT); digitalWrite(blueLed, HIGH); // turn off the blue. 
      pinMode(greenLed, OUTPUT); digitalWrite(greenLed, HIGH); // turn off the green.
      pinMode(powerLed, OUTPUT); analogWrite(powerLed, LOW); // turn off the pled. 
      
      pinMode(relayPin, OUTPUT); digitalWrite(relayPin, LOW); // turn off the relay. 
    
      pinMode(pinButton, INPUT); //set the button pin as INPUT
        
      // Set the use ouf our output pins
      pinMode(GPIO, OUTPUT);
      pinMode(backLight, OUTPUT);
      pinMode(contrast, OUTPUT);
      
    // Gather default values from EEPROM (could add code here to handle default if EEPROM has never been written to)
      digitalWrite(GPIO, LOW);
      analogWrite(backLight, EEPROM.read(BRIGHTNESS));    //  Read brightness level out of EEPROM
      analogWrite(contrast, EEPROM.read(CONTRAST));       // Read contrast level out of EEPROM
    
      // set up the LCD's number of columns and rows: 
      lcd.begin(20, 4); lcd.clear();
      lcd.print(" Display - PS3, DK ");
    
      // Custom Characters for the HTPS Logo
      byte htps1[8] = { B10001, B10001, B10001, B11111, B11111, B10001, B10001, B10001 };
      byte htps2[8] = { B11111, B11111, B00100, B00100, B00100, B00100, B00100, B00100 };
      byte htps3[8] = { B01110, B11011, B10001, B11011, B11110, B10000, B10000, B10000 };
      byte htps4[8] = { B01110, B11011, B10001, B11000, B01110, B00001, B11001, B01110 };
      lcd.createChar(4, htps1);
      lcd.createChar(5, htps2);
      lcd.createChar(6, htps3);
      lcd.createChar(7, htps4);
      lcd.setCursor(1, 2);
      lcd.write(4);
      lcd.setCursor(2, 2);
      lcd.write(5);
      lcd.setCursor(3, 2);
      lcd.write(6);
      lcd.setCursor(4, 2);
      lcd.write(7);
      lcd.setCursor(8, 2);
      // End of custome character generation and display
    
      lcd.print("Input Ready");
      lcd.setCursor(8, 3);
      lcd.print("19200,8,N,1");
    
    
      Serial.begin(baud);
      // Wait until we receive some data - note we want to hold what data it is as we use it the first iteration
      rxbyte = serial_getch();
    
      // Clear display, wait a little and load in the custom icons 
      lcd.clear(); delay(200);
    
      // Generate 3 different bar graph values for 33% 66% and 100%
      byte bara[8] = { B10000, B10000, B10000, B10000, B10000, B10000, B11111, B10000 };
      byte barb[8] = { B11100, B11100, B11100, B11100, B11100, B11100, B11111, B11100 };
      byte barc[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };
    
      // Characters for CPU, Nivida Graphics Card, C: drive and D: drive and degrees symbol (DegC)
      byte cpu[8] = { B10111, B10101, B10111, B00000, B11101, B10101, B11101, B00000 };
      byte nv[8] = { B11101, B10110, B00000, B11111, B11101, B11111, B01100, B00000 };
      byte cdrive[8] = { B01100, B10001, B10000, B01101, B00000, B11111, B11101, B11111 };
      byte ddrive[8] = { B11000, B10101, B10100, B11001, B00000, B11111, B11101, B11111 };
      byte degs[8] = { B11000, B11000, B00000, B00110, B01000, B01000, B00110, B00000 };
    
      // Write characters into LCD RAM
     lcd.createChar(0, newChar0); // upload 8 selvdef. karakterer
     lcd.createChar(1, barc); lcd.createChar(2, bara); lcd.createChar(3, barb); lcd.createChar(4, cpu); 
     lcd.createChar(5, nv); lcd.createChar(6, cdrive); lcd.createChar(7, ddrive); lcd.createChar(8, degs); 
     lcd.createChar(9, VT); // VT ( ;-) 
     lcd.createChar(10, Lae); // 
     lcd.createChar(11, Loe); // 
     lcd.createChar(12, Laa); // 
     lcd.createChar(13, Sae); // 
     lcd.createChar(14, Soe); // 
     lcd.createChar(15, Saa); // 
     delay(20); lcd.clear();
    }
    
    // Helper function to read serial input as clean 8 bit byte
    byte serial_getch(){ int ch; while (Serial.available() == 0){ loop2(); }ch = Serial.read(); return (byte)(ch & 0xff); } // read the incoming byte:
    
    
    void loop() {  //First loop
       if (rxbyte == 254) { //Matrix Orbital uses 254 prefix for commands
        switch (serial_getch()) {
        case 52: EEPROM.write(SERIAL_HI, serial_getch()); EEPROM.write(SERIAL_LO, serial_getch()); break; //set serial number /*USING EEPROM*/
        case 53: Serial.write(EEPROM.read(SERIAL_HI)); Serial.write(EEPROM.read(SERIAL_LO)); break; //read serial number /*USING EEPROM*/
        case 54: Serial.write(0x11); break; //read version number v1.1
        case 55: Serial.write(0x05); break; //read module lcd_type = 'LCD2041'
        case 59: break; //exit flow-control mode
        case 64: addr = serial_getch(); val = serial_getch(); EEPROM.write(addr, val); break; // EEPROM Write (address, value) 
        case 65: addr = serial_getch(); val = EEPROM.read(addr); Serial.print(val); break; // EEPROM Read  (address)
        case 66: temp = serial_getch(); break; //backlight on (at previously set brightness) NOT implemented      
        case 67: //auto line-wrap on
        case 68: break; //auto line-wrap off
        case 70: analogWrite(backLight, 0); analogWrite(powerLed, 0); break; //backlight and powerled off
        case 71: temp = (serial_getch() - 1); lcd.setCursor(temp, serial_getch() - 1); break; //set cursor position
        case 72: lcd.setCursor(0, 0); break; //cursor home (reset display position)
        case 74: lcd.command(0b00001110); break; //show underline cursor
        case 75:  //underline cursor off
        case 84: lcd.command(0b00001100); break; //block cursor off
        case 76: lcd.command(16); break;  //move cursor left    
        case 77: lcd.command(20); break;  //move cursor right
        case 78: temp = serial_getch(); for (i = 0; i < 8; i++){ data[i] = serial_getch(); } lcd.createChar(temp, data); break; //define custom char
        case 80:  // Set contrast (but we save anyway)
        case 145: level = 255 - serial_getch(); analogWrite(contrast, level); EEPROM.write(CONTRAST, level); break; // Set Contrast and save
        case 81: //auto scroll on
        case 82: break; //auto scroll off
        case 83: lcd.command(0b00001111); break; //show blinking block cursor
        case 86: temp = serial_getch(); digitalWrite(GPIO, LOW); break; //GPIO OFF
        case 87: temp = serial_getch(); digitalWrite(GPIO, HIGH); break; //GPIO ON
        case 88: lcd.clear(); break; //clear display, cursor home
        case 96: break; //auto-repeat mode off (keypad)
        case 98: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //Draw bitmap
        case 99: temp = serial_getch(); break; // set drawing color
        case 101: temp = serial_getch(); temp = serial_getch(); break; //Draw line continue
        case 104: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //init horiz bar graph
        case 108: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break;
        case 109: break; //init med size digits
        case 112:  temp = serial_getch(); temp = serial_getch(); break; // Draw Pixel
        case 114:  temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //draw rect
        case 115: //init narrow vert bar graph
        case 118: break; //init wide vert bar graph
        case 120: temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); temp = serial_getch(); break; //draw rect solid
        // case 145:  //defined above see case 80:
        case 152: level = serial_getch(); analogWrite(backLight, level); EEPROM.write(BRIGHTNESS, level); break; //set brightness and save 
        case 153: level = serial_getch(); analogWrite(backLight, level); analogWrite(powerLed, level); break;//set backlight and powerled brightness
        default: temp = serial_getch(); break; } //all other commands ignored and parameter byte discarded
      } //END OF COMMAND HANDLER
      else {
        // Not a command character but still might be a special character
        //change accented char to plain, detect and change descenders
        switch (rxbyte) {
          //chars that have direct equivalent in LCD charmap
          case 0xE4: rxbyte = 0xE1; break; //ASCII "a" umlaut
          case 0xF1: rxbyte = 0xEE; break; //ASCII "n" tilde
          case 0xF6: rxbyte = 0xEF; break; //ASCII "o" umlaut
          case 0xFC: rxbyte = 0xF5; break; //ASCII "u" umlaut //accented -> plain equivalent and misc symbol translation
          case 0xA3: rxbyte = 0xED; break; //sterling (pounds)
          case 0xAC: rxbyte = 0xB0; break; //sterling (pounds)
          case 0xB0: rxbyte = 0xDF; break; //degrees symbol
          case 0xB5: rxbyte = 0xE4; break; //mu
          case 0xC0: //"A" variants
          case 0xC1:
          case 0xC2:
          case 0xC3:
          case 0xC4:
          case 0xC5: rxbyte = 0x41; break;
          case 0xC8: //"E" variants
          case 0xC9:
          case 0xCA:
          case 0xCB: rxbyte = 0x45; break;
          case 0xCC: //"I" variants
          case 0xCD:
          case 0xCE:
          case 0xCF: rxbyte = 0x49; break;
          case 0xD1: rxbyte = 0x43; break; //"N" tilde -> plain "N"
          case 0xD2: //"O" variants
          case 0xD3:
          case 0xD4:
          case 0xD5:
          case 0xD6:
          case 0xD8: rxbyte = 0x4F; break;
          case 0xD9: //"U" variants
          case 0xDA:
          case 0xDB:
          case 0xDC: rxbyte = 0x55; break;
          case 0xDD: rxbyte = 0x59; break; //"Y" acute -> "Y"
          case 0xE0: //"a" variants except umlaut
          case 0xE1:
          case 0xE2:
          case 0xE3:
          case 0xE5: rxbyte = 0x61; break;
          case 0xE7: rxbyte = 0x63; break; //"c" cedilla -> "c"
          case 0xE8: //"e" variants
          case 0xE9:
          case 0xEA:
          case 0xEB: rxbyte = 0x65; break;
          case 0xEC: //"i" variants
          case 0xED:
          case 0xEE:
          case 0xEF: rxbyte = 0x69; break;
          case 0xDF: //beta  // LCDSmartie degree symbol??
          case 0xF2: //"o" variants except umlaut
          case 0xF3:
          case 0xF4:
          case 0xF5:
          case 0xF8: rxbyte = 0x6F; break;
          case 0xF7: rxbyte = 0xFD; break; //division symbol
          case 0xF9: //"u" variants except umlaut
          case 0xFA:
          case 0xFB: rxbyte = 0x75; break;
          default: break;
        }
        // By now either special character is converted or it was printable already
        lcd.print((char)rxbyte);  //print it to lcd
      }
      rxbyte = serial_getch();    // Wait for the next byte and use value for next iteration
    }
    
    void loop2(){ //Second loop
      if (irrecv.decode(&results)) { results.value &=0x7ff; sendForButton(results.value); irrecv.resume(); } 
      float temp1; temp1=Thermistor1(analogRead(Thermistor1PIN)); 
      float temp2; temp2=Thermistor2(analogRead(Thermistor2PIN)); 
       if(temp1 < tempMin) { fanSpeed = 0; analogWrite(fan, 0); } 
       if((temp1 >= tempMin) && (temp1 <= tempMax)) { fanSpeed = map(temp1, tempMin, tempMax, 32, 255); fanLCD = map(temp1, tempMin, tempMax, 0, 100);  analogWrite(fan, fanSpeed); } 
       if(temp1 > tempGreen) { digitalWrite(greenLed, LOW);} else { digitalWrite(greenLed, HIGH); }   
       if(temp2 < tempMin) { fanSpeed2 = 0; analogWrite(fan2, 0); } 
       if((temp2 >= tempMin) && (temp2 <= tempMax)) { fanSpeed2 = map(temp2, tempMin, tempMax, 32, 255); fanLCD2 = map(temp2, tempMin, tempMax, 0, 100); analogWrite(fan2, fanSpeed); } 
       if(temp2 > tempGreen) { digitalWrite(greenLed, LOW);} else { digitalWrite(greenLed, HIGH); }
       }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •