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

Thread: LCD programming help needed

  1. #1

    LCD programming help needed

    Hi. This simple sketch is not working properly. Can someone look at it and let me know if there is something wrong? When I first compile and then hit the pushbutton on the Teensy it does print the text, but after powering down and then powering up the LCD it does not display the text. It is backlit with power, but no text shown. Thank you in advance!

    Teensy 3.1
    Serial Enabled 16x2 LCD - White on Black 3.3V - https://www.sparkfun.com/products/9067
    Arduino Teensyduino w/ SerLCD

    Windows 7

    wired power to LCD from 3.3V and GND to GND
    wired the LCD RX to Teensy pin 10 TX2



    // SparkFun Serial LCD example 1
    // Clear the display and say "Hello World!"

    // This sketch is for Arduino versions 1.0 and later
    // If you're using an Arduino version older than 1.0, use
    // the other example code available on the tutorial page.

    // Use the softwareserial library to create a new "soft" serial port
    // for the display. This prevents display corruption when uploading code.
    #include <SoftwareSerial.h>

    // Attach the serial display's RX line to digital pin 10
    SoftwareSerial mySerial(9,10); // pin 10 = TX, pin 9 = RX (unused)

    void setup()
    {
    mySerial.begin(9600); // set up serial port for 9600 baud
    delay(500); // wait for display to boot up
    }

    void loop()
    {
    mySerial.write(254); // move cursor to beginning of first line
    mySerial.write(128);

    mySerial.write(" "); // clear display
    mySerial.write(" ");

    mySerial.write(254); // move cursor to beginning of first line
    mySerial.write(128);

    mySerial.write("VC REV-VAR LOOP ");
    mySerial.write(" RPMS +0000 ");

    while(1); // wait forever
    }

  2. #2
    The only thing i can find on the web is that when powering up the TX line might be sending out garbage that could corrupt the LCD. Does the Teensy 3.1 send garbage out of PIN 10 during power up? Sometimes the LCD will be blank and other times it will have random characters on it. I thought the code creating the new "soft" serial port would alleviate this, but maybe not?

    Any ideas?

    thank you

  3. #3
    Quote Originally Posted by donkeyahoy View Post
    The only thing i can find on the web is that when powering up the TX line might be sending out garbage that could corrupt the LCD. Does the Teensy 3.1 send garbage out of PIN 10 during power up? Sometimes the LCD will be blank and other times it will have random characters on it. I thought the code creating the new "soft" serial port would alleviate this, but maybe not?

    Any ideas?

    thank you
    Shotgun response, but have you tried with the hardware serial (Serial1 for example) vs software serial....I just never use soft serial with Teensy. If that does not work post up and I'll pull out an LCD and give it a try (not sure I have the sparkfun version but probably something similar enough)

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,561
    Does it work if you change the delay to more than 500 ms?

    Or what if you put all the code into loop(), and remove the while(1) wait forever, so it keeps trying to redraw every half second?

  5. #5
    Quote Originally Posted by PaulStoffregen View Post
    Does it work if you change the delay to more than 500 ms?

    Or what if you put all the code into loop(), and remove the while(1) wait forever, so it keeps trying to redraw every half second?
    Yes! It does work if I put the code into the loop() area.


    Still using the soft serial code way shown above. Not sure if I should be worried about the LCD corruption during the boot up. I've turned it on and off maybe 30 times now and seems fine.

    Should I try switching to the hardware serial? Less chance of corruption?

    thanks!!

  6. #6
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,215
    Note, Teensy's SoftwareSerial package will use Serial2 if rx == 9 and tx == 10. What speed are your running your Teensy at?

  7. #7
    Quote Originally Posted by MichaelMeissner View Post
    Note, Teensy's SoftwareSerial package will use Serial2 if rx == 9 and tx == 10. What speed are your running your Teensy at?

    I see 9600 baud called out in the code. Here is my latest code btw:

    You see any issues?

    Code:
    // Use the softwareserial library to create a new "soft" serial port
    // for the display. This prevents display corruption when uploading code.
    #include <SoftwareSerial.h>
    
    // Initialize constants that refer to specific pins on the Teensy
    const int ledPin = 13;
    const int CVin = 14;
    const int PWMout = 22;
    
    // Initialize variables, names are arbitrary
    int CVvalue = 0;
    int dutyCycle = 0;
    char tempstring[10];
    
    // Attach the serial display's RX line to digital pin 10
    SoftwareSerial mySerial(9,10); // pin 10 = TX, pin 9 = RX (unused)
    
    // This setup() function runs once when power is applied to the Teensy or its reprogrammed
    void setup(){
    
      mySerial.begin(9600); // set up serial port for 9600 baud
      delay(1000); // wait for display to boot up
      
    // Initialize the serial COM port for debug
      Serial.begin(38400);                 // Baud rate is not critical
      
    // Initialize pins as either OUTPUT or INPUT
      pinMode(ledPin, OUTPUT);
      pinMode(PWMout, OUTPUT);
      pinMode(CVin, INPUT);
      
    // Initialize PWM module
      analogWriteFrequency(PWMout, 2000);  // PWM frequency is 2kHz
      analogWriteResolution(10);           // 10-bits of resolution = 0-1024
      analogWrite(PWMout, dutyCycle);      // program starts with PWM output at zero
    
      mySerial.write(254); // move cursor to beginning of first line
      mySerial.write(128);
      mySerial.write("                "); // clear display
      mySerial.write("                ");
      mySerial.write(254); // move cursor to beginning of first line
      mySerial.write(128);
      mySerial.write("VC REV-VAR LOOP ");
      mySerial.write("   RPMS +0000   ");
    }
    
    // The loop() runs forever.
    void loop(){
      
      CVvalue = analogRead(CVin);          // sample the analog value
      
      dutyCycle = CVvalue;                 // do math here to scale/limit/adjust the sample value 
      
      analogWrite(PWMout, dutyCycle);      // update the PWM duty-cycle with the new value
      
      Serial.print("CV is:");              // spit out the variables on the serial bus
      Serial.print(CVin);
      Serial.print(" DutyCycle is:");
      Serial.println(dutyCycle);
      
      digitalWrite(ledPin,!digitalRead(ledPin));  // toggle LED on the Teensy (heartbeat)
                                                  //...might look always on because it's toggling so fast
    
      sprintf(tempstring,"%4d",dutyCycle); // create strings from the numbers
                                           // right-justify to 4 spaces
                                           
      mySerial.write(254); // move cursor to beginning of first line
      mySerial.write(128);
      mySerial.write("VC REV-VAR LOOP ");
      mySerial.write("   RPMS  ");
    
      mySerial.write(254); // move cursor to second line 10th space
      mySerial.write(201);
    
      mySerial.write(tempstring);
    										  
    }

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,561
    On Teensy 3.1, if you use SoftwareSerial with pins that are real serial, it will automatically use HardwareSerial. There's a tiny bit of extra overhead, because you're calling the SoftwareSerial function which in turn calls the Serial2 function, but that extra CPU time is tiny.

    My guess is that serial LCD takes quite a while to boot up.

    This is actually a common problem when using stuff designed for regular Arduino Uno. The normal Arduino boards boot up slowly. Teensy 3.1 boots up very fast. Normally fast boot is a good thing, but if you're talking to something else that boots up slowly, you'll have to wait until it's ready.

  9. #9
    Quote Originally Posted by PaulStoffregen View Post
    On Teensy 3.1, if you use SoftwareSerial with pins that are real serial, it will automatically use HardwareSerial. There's a tiny bit of extra overhead, because you're calling the SoftwareSerial function which in turn calls the Serial2 function, but that extra CPU time is tiny.

    My guess is that serial LCD takes quite a while to boot up.

    This is actually a common problem when using stuff designed for regular Arduino Uno. The normal Arduino boards boot up slowly. Teensy 3.1 boots up very fast. Normally fast boot is a good thing, but if you're talking to something else that boots up slowly, you'll have to wait until it's ready.
    ahhh... thank you very much Paul. This explains it. That is why it works so well when the LCD print commands are in the loop. I think I'm all set then. Sweet!

Posting Permissions

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