I am trying to print a variable to a spark fun serial enabled LCD screen. The following code is what I have tried so far to display a countdown timer and the temperature from an LM35DZ temperature sensor. The LCD screen displays characters when programmed to display the variables for TimerValue and temperature, but they are not readable. The variables are displayed correctly on the serial monitor when instructed to.
The characters on the screen for the timer component alternate between showing 3 or 2 vertical lines.
Could anyone give me advice on how to rectify this issue?
Is there anything visibly wrong with the code listed below?
Am I using the correct command for printing a variable?
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
// Attach the serial display’s RX line to digital pin 2
SoftwareSerial mySerial(5,4); // pin 4 = TX, pin 3 = RX (unused)
//Button pin setup:
const int buttonPin1 = 13;
const int buttonPin2 = 1;
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonPushCounter1 = 0;
//Temperature sensor setup:
const int sensorPin = A0;
int led = 10;
const float baselineTemp = 23.0;
float voltage = 0;
float temperature = 0;
//Rotary encoder setup:
int encoderPin1 = 2;
int encoderPin2 = 3;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
//Timer setup:
int Timer1 = 20;
int Timer2 = 30;
int Timer3 = 40;
int TimerValue = 0;
void setup() {
Serial.begin(9600);
pinMode(13, INPUT);
pinMode(10, OUTPUT);
digitalWrite(buttonPin1, LOW);
mySerial.begin(9600); // set up serial port for 9600 baud
delay(500); // wait for display to boot up
//digitalWrite (10,LOW);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
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(“Welcome to CRYO”);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(194);
mySerial.write(“press enter”);
while (digitalRead(13) == LOW) {
delay(500);
Serial.print (“waiting”);
}
delay(500);
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(“Fill ice chamber”);
delay (500);
while (analogRead (sensorPin) > 53.248) {
delay(500);
int sensorVal = analogRead (sensorPin);
float voltage = (sensorVal/1024.0)5.0;
float temperature = voltage100;
Serial.println(temperature);
Serial.print("loop degrees C: ");
}
Serial.print(“break”);
delay(500);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(" “); // clear display
mySerial.write(” ");
while (digitalRead(13) == LOW) {
Serial.println(encoderValue);
Serial.print("Encoder reading: ");
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(“Apply strap then”);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(194);
mySerial.write(“press enter”);
}
Serial.print (“Timer time”);
delay(500);
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(129);
delay(500);
mySerial.write(“Cryo begins in:”);
delay(1000);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(199);
mySerial.write(“3”);
delay(1000);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(199);
mySerial.write("");
mySerial.write(“2”);
delay(1000);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(199);
mySerial.write("");
mySerial.write(“1”);
delay(500);
if (encoderValue < 100){
TimerValue=Timer1;}
else if (encoderValue < 200){
TimerValue=Timer2;
}
else {TimerValue=Timer3;}
while (TimerValue>0){
delay(1000);
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(“Time:”);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(138);
mySerial.write(TimerValue);
int sensorVal = analogRead (sensorPin);
float voltage = (sensorVal/1024.0)5.0;
float temperature = voltage100;
Serial.println(temperature);
Serial.print("degrees C: ");
if (analogRead (sensorPin) > 53.248) {
digitalWrite (10, HIGH);
}
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(192);
mySerial.write(“Temp:”);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(202);
mySerial.write(temperature);
//mySerial.write((sensorVal/1024.0)5.0100);
TimerValue = TimerValue -1;
}
digitalWrite (10, LOW);
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(130);
mySerial.write(“Cryo complete”);
delay(10000);
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //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) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //store this value for next time
}