0

Serial Backpack LCD code examples?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial Backpack LCD code examples?

by devinf on Tue Aug 20, 2019 12:55 am

I was wondering if anyone could post some examples of Serial backpack LCD code for Arduino.

I was also having trouble setting my cursor location. I can't figure out how to format the cursor position code.

This is what I have for setting the cursor location, I must be missing something.

lcd.write(0xFE);
lcd.write(0x47);
lcd.write(1, 10);
delay(10);

The Arduino ide is telling me it won't compile formatted like that. "no matching function for call to 'write(int, int)"

I'm trying to print an int variable to the lcd . I'm not exactly sure I can even do this with the serial backpack. I just wanted to play around with it a little and see if I could get it to work.

Go easy on me I'm pretty new to this,
Devin



Code: Select all | TOGGLE FULL SIZE
#include <EEPROM.h>
#include<SoftwareSerial.h>


int calLED = 13;         //Calibrate LED illuminates while in the calibrate function
int saveLED = 12;        //Save LED blinks after min max values from calibrate routine have been saved into EEProm
int savePin = 9;         //Button pressed to write min max values into EEPROM
int calPin = 8;         // Set calibrate pin to digit pin 8
int leftActuator = A0;      // Set left actuator position input to analog pin 0
int leftActuatorRead = 0;     // variable to store the value read from the actutor potentiometer
int leftActuatorPos = 0;      // remapped position of laft actuator
int leftSensorMin = 1023;     // minimum sensor value
int leftSensorMax = 0;        // maximum sensor value
const int numReadings = 10;   // number readings in smoothing array
int readings[numReadings];    // the readings from the analog input
int readIndex = 0;            // position in array
int total = 0;                // sum of readings in array
int leftPosAverage = 0;       // Average of last 10 sensor readings in array
int calLeft = 0;

//right sensor variables


int rightActuator = A1;
int rightActuatorRead = 0;
int rightActuatorPos = 0;
int rightSensorMin = 1023;    // minimum sensor value
int rightSensorMax = 0;       // maximum sensor value
const int  rightNumReadings = 10;
int rightReadings[rightNumReadings];
int rightReadIndex = 0;
int rightTotal = 0;
int rightPosAverage = 0;
int calRight = 0;


//button timer setup
//long press cal button enters clearEeprom function
//short press cal button enters calibration function

//track how long the momentary pushbutton is held in order to execute different commands
//This value will be recorded in seconds
float pressLength_milliSeconds = 0;

// Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 5000;

SoftwareSerial lcd = SoftwareSerial(0,2);


void setup()
{
   (EEPROM.get(0,leftSensorMax));       //read maximum sensor value from eeprom
   (EEPROM.get(10,leftSensorMin));      //read minimum sensor value from eeprom

   (EEPROM.get(20,rightSensorMax));     //read maximum sensor value from eeprom
   (EEPROM.get(30,rightSensorMin));     //read minimum sensor value from eeprom

  pinMode (savePin, INPUT_PULLUP);
  pinMode (calPin, INPUT_PULLUP);
  pinMode (13, OUTPUT);
  pinMode (12, OUTPUT);

  lcd.begin(9600); //open software serial pin 2
  Serial.begin(9600); //open serial port
   
   for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
   }

   for (int thisRightReading = 0; thisRightReading < rightNumReadings; thisRightReading++)   { 
    rightReadings[thisRightReading] = 0;
   }

      // set the contrast, 200 is a good place to start, adjust as desired
  lcd.write(0xFE);
  lcd.write(0x50);
  lcd.write(200);
  delay(10);       
 
  // set the brightness - we'll max it (255 is max brightness)
  lcd.write(0xFE);
  lcd.write(0x99);
  lcd.write(255);
  delay(10);       
 
  // turn off cursors
  lcd.write(0xFE);
  lcd.write(0x4B);
  lcd.write(0xFE);
  lcd.write(0x54);

    // clear screen
  lcd.write(0xFE);
  lcd.write(0x58);
  delay(10);   // we suggest putting delays after each command
 
  // go 'home'
  lcd.write(0xFE);
  lcd.write(0x48);
  delay(10);   // we suggest putting delays after each command

  lcd.println("L Sensor:");
  delay(10);
  lcd.print("R Sensor:");
  delay(10);
 
  delay(1000);

 
}
void loop()
{

  while (digitalRead(calPin) == LOW) {
    delay(100);
    pressLength_milliSeconds += 100;
    Serial.print("ms = ");
    Serial.println(pressLength_milliSeconds);

   
    }

    if (pressLength_milliSeconds >= optionTwo_milliSeconds){

      clearEeprom();

     
    }
    else if(pressLength_milliSeconds >= optionOne_milliSeconds){
      digitalWrite(calLED, HIGH);
      calibrate();
     
    }

    pressLength_milliSeconds = 0;
   

 

 
  //Left Sensor Read Smooth map and constrain
 
  leftActuatorRead = analogRead(leftActuator);
  leftActuatorPos = map(leftActuatorRead, leftSensorMin, leftSensorMax, 0, 50);
  leftActuatorPos = constrain(leftActuatorPos, 0, 50);
 
 
   // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = leftActuatorPos;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  leftPosAverage = total / numReadings;
  // send it to the computer as ASCII digits

 
 
  //Right Sensor read smooth map and constrain

  rightActuatorRead = analogRead(rightActuator);
  rightActuatorPos = map(rightActuatorRead, rightSensorMin, rightSensorMax, 0, 50);
  rightActuatorPos = constrain(rightActuatorPos, 0, 50);
 
 
   // subtract the last reading:
  rightTotal = rightTotal - rightReadings[rightReadIndex];
  // read from the right sensor:
  rightReadings[rightReadIndex] = rightActuatorPos;
  // add the reading to the total:
  rightTotal = rightTotal + rightReadings[rightReadIndex];
  // advance to the next position in the array:
  rightReadIndex = rightReadIndex + 1;

  // if we're at the end of the array...
  if (rightReadIndex >= rightNumReadings) {
    // ...wrap around to the beginning:
    rightReadIndex = 0;
  }

  // calculate the average:
  rightPosAverage = rightTotal / rightNumReadings;
  // send it to the computer as ASCII digits


  Serial.print(leftPosAverage);
  Serial.print("\t");
  Serial.print(rightPosAverage);
  Serial.print("\t");
  Serial.print(leftSensorMax);
  Serial.print("\t");
  Serial.print(leftSensorMin);
  Serial.print("\t");
  Serial.print(rightSensorMax);
  Serial.print("\t");
  Serial.print(rightSensorMin);
  Serial.println();
  delay(100);

 
  lcd.write(0xFE);
  lcd.write(0x47);
  lcd.write(1, 10);
  delay(10);
 
 


 



 
}



   
 }

devinf
 
Posts: 4
Joined: Mon Aug 19, 2019 11:56 pm

Re: Serial Backpack LCD code examples?

by franklin97355 on Tue Aug 20, 2019 1:50 am

Which backpack and LCD are you using? Have you checked the examples included with the libraries?

franklin97355
 
Posts: 21087
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Serial Backpack LCD code examples?

by kcl1s on Tue Aug 20, 2019 6:33 am

Never used this backpack before but the serial write function (in this case your lcd.write function) only takes 1 argument. So try putting the row and column values in separate lcd.write statements.

Fellow hobbyist
Keith

kcl1s
 
Posts: 1251
Joined: Tue Aug 30, 2016 12:06 pm

Re: Serial Backpack LCD code examples?

by devinf on Tue Aug 20, 2019 9:23 am

The kit product id: 782
The only example I could find was on a setup guide but that example didn't really cover printing variables or setting cursor location.

I'll give it try separating them and spend some more time messing with it.

Thanks,
Devin

devinf
 
Posts: 4
Joined: Mon Aug 19, 2019 11:56 pm

Re: Serial Backpack LCD code examples?

by kcl1s on Tue Aug 20, 2019 9:51 am

It should work the same as printing to the serial monitor, for literal strings lcd.print("Temperature: "); and variables lcd.print(temp);

I agree there should be better docs on this.

Keith

kcl1s
 
Posts: 1251
Joined: Tue Aug 30, 2016 12:06 pm

Re: Serial Backpack LCD code examples?

by franklin97355 on Tue Aug 20, 2019 1:27 pm

There is a tutorial that covers most of this here. Like Keith said you need to pass the cursor position in two commands.

franklin97355
 
Posts: 21087
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Serial Backpack LCD code examples?

by devinf on Tue Aug 20, 2019 11:55 pm

I got it working thanks for the help guys. I'll post the snippet I used to get it working.

I did run into a small problem after I got it to print in the correct location. The variable I was printing was an analog reading off a 10k pot mapped to 0 - 20. Whenever it would go over 9 it would automatically move the cursor over and print the second digit in the next space just like it should. However, when going back below 10 it would leave a zero in the second space so 9 looked like 90 and so forth. I came up with a little if statement to clear out the second space. Is there an easier cleaner way to do that?

Code: Select all | TOGGLE FULL SIZE
  lcd.write(0xFE);    //Command code
  lcd.write(0x47);    //set cursor position command
  lcd.write(10);      //move to row 10
  lcd.write(1);       //move to column 1
  delay(10);          //delay after command sent

  lcd.print(leftPosAverage);    //print the variable

    if (leftPosAverage <= 10){    //if the variable is less than or equal to 10
    lcd.write(0xFE);              //command code
    lcd.write(0x47);              //set cursor position command
    lcd.write(11);                //move to row 11
    lcd.write(1);                 //move to column 1
    delay(10);                    //delay after command sent
    lcd.print(" ");               //print a space to clear the second digit position
    delay(10);                    //delay after command sent
   
  }

devinf
 
Posts: 4
Joined: Mon Aug 19, 2019 11:56 pm

Re: Serial Backpack LCD code examples?

by kcl1s on Wed Aug 21, 2019 7:52 am

Is there an easier cleaner way to do that?

I like to use the decimal to string function dtostrf to format my display outputs. This function works for both ints and floats. For floats it will even round your numbers to a desired number of places. For your use it will right justify the output and add leading blank spaces for smaller numbers. This is the parameters it expects
dtostrf(int or float variable, total # chars in output, #decimal places for floats, output char array variable);

First you need to declare a char array at least one element larger than your out put string. It looks like you are only expecting 2 digits for your output so you could declare this global variable array at the top of your code char outstr[3];. This variable is only used for temporary storage so it can be re-used for other dtostrf conversions as well. Just make sure it is at least one char larger than the expected largest output including places for the decimal point and a negative sign (-) if applicable.

With the outstr array created your code snippet would look like this...
Code: Select all | TOGGLE FULL SIZE
  lcd.write(0xFE);    //Command code
  lcd.write(0x47);    //set cursor position command
  lcd.write(10);      //move to row 10
  lcd.write(1);       //move to column 1
  delay(10);          //delay after command sent

  dtostrf(leftPosAverage, 2, 0, outstr); //convert decimal to char string
  lcd.print(outstr);    //print the char string


Give it a try and see if it is easier
Keith

kcl1s
 
Posts: 1251
Joined: Tue Aug 30, 2016 12:06 pm

Re: Serial Backpack LCD code examples?

by devinf on Wed Aug 21, 2019 10:43 pm

Thanks, I figured there was a better way to do that. I'll give it a shot.

That will be easier if I can use the same char array variable for both of my sensors readings.

devinf
 
Posts: 4
Joined: Mon Aug 19, 2019 11:56 pm

Please be positive and constructive with your questions and comments.