[SOLVED] Why does lcd.print(a); work in one sketch but not the other?

I have two similar sketches , one to test the IR receiver and display and one to add more instructions to the display.

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 * altered to work with any ir chip.
 * left to right output, 0 volts, vcc.
 * except ir with four pins on the top, left to right  output, vcc, 0 volts.
 * when using wharfdale dvd remote
 *    1. puts "warf dvdbutton 1" on screen
 *    2. puts "        button 2" on screen
 *    3. puts a count on the last two elements that keep counting even when not showing using variable 'a'
 *    4. puts a count down on element 1 and then puts pow on the left side and repeats.
 *    5. counts up from the left on each element in hexidecimal.
 *    6. clock on lcd display.
 * turns off 8 digits.
 */
#include <IRremote.h>     // Ver 3.3.0 <-------<<<<<<<
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip
#include "LedControl.h"//turns 8 digits off see lc.control below & next line
LedControl lc=LedControl(7,8,6,1);//data,clock,latch,number of ics, see previous line

const byte IR_RECEIVE_PIN = 5; //  receive pin

char buffer [9] = ""; //set up so button 6 gives a timer
int a=0;//number to show on lcd
int f=0;//compare a to f to clearscreen each second
int b=0;//show time on lcd
int i;
int seconds;
int minutes;
int hours;
int hour=0;int min=0;int sec=0;
long unsigned int d;//save millis count
int e;//millis compare
unsigned long t=0;//for void clock2
void setup()
{
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);//use pin 13
  Serial.begin(9600);
  lcd.begin (16,2);  // initialize the lcd
// Switch on the backlight
//  lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
  lcd.clear();
  delay(100);
  lcd.home();  
//  lcd.displayon();
  lc.shutdown(0,true);//turns 8digits off, see include ledcontrol above
 // Print on the LCD
 // lcd.backlight();  
 
  lcd.setCursor(0,0); 
  lcd.print("this program will");
  lcd.setCursor(1,1); 
  lcd.print("test remotes");
}

void loop() 
{
  if (IrReceiver.decode()) // have we received an IR signal?
  {
    lcd.clear();
    Serial.print("Code: ");
    Serial.println(IrReceiver.decodedIRData.command);
    //for (int z=0; z<1; z++) // ignore 2nd and 3rd signal repeat
    {
    translateIR();
    Serial.print ("\n");// same as using Serial.println
    IrReceiver.resume(); // Enable receiving of the next value
    }
  }
  if (b==3)//button 3  count on elements 15 and 16
  {
  //  Serial.print ("  here");
    twonumbers();
  }
  if (b==4)//button 4  count down to "pow" on element 1
  {
    pow();
  }
  if (b==5)
  {
    numbers_to_right();
  }
  if (b==6)
  {
  clock2();
  lcd.setCursor(0,0);
  sprintf(buffer, "%02d:%02d:%02d", hour, min, sec);
  lcd.print(buffer); //< You will get "13:02:00"
  } 
}
void translateIR() // takes action based on IR code received
                   // describing Sony IR codes on LCD module
{
//      Serial.print("Code: ");
 //     Serial.println(IrReceiver.decodedIRData.command);
  switch(IrReceiver.decodedIRData.command)
  {
    case 2460: Serial.println(" pause          "); break;
    case 1436: Serial.println(" Play button    "); break;
    case 3484: Serial.println(" rewind         "); break;
    case 924:  Serial.println(" fast forward   "); break;
    case 412:  Serial.println(" stop           "); break;
    case 3164: Serial.println(" slow           "); break;
    case 1468: Serial.println(" display        "); break;
    case 19613:Serial.println(" start/stop     "); break;
    case 27803:Serial.println(" w              "); break;
    case 11419:Serial.println(" t              "); break;
    case 9:   Serial.println("warfdale dvd button 1");b=0;//b=0 stops time showing on lcd
    lcd.setCursor(0,0);lcd.print("warfdale dvd");
    lcd.setCursor(0,1);lcd.print("button 1");break;
    case 29:  Serial.println("warfdale dvd button 2");b=0;
    lcd.setCursor(0,0);lcd.print("warfdale dvd");
    lcd.setCursor(0,1);lcd.print("button 2");break;//print on monitor and lcd screen 
    case 31:  Serial.println("warfdale dvd button 3");b=3;break;
    //b=3 lets time show on lcd
    case 83603535:  Serial.println("warf' dvd button 4");b=4;break;//83603535  b=2 starts countdown
   //case 3932342631:  Serial.println("warf' dvd button 4");b=4;break;//83603535  b=2 starts countdown
    case 83597415:  Serial.println("warf' dvd button 5");b=5;break;//83597415
   //case 2431053599:  Serial.println("warf' dvd button 5");b=5;break;//83597415
    case 83613735:  Serial.println("warf' dvd button 6");b=6;break;//83613735
   //case 2152700347:  Serial.println("warf' dvd button 6");b=6;break;//83613735
    case 83593335:  Serial.println("warf' dvd button 7");b=7;break;//83593335
   //case 2559788867:  Serial.println("warf' dvd button 7");b=7;break;//83593335
    case 83601495:  Serial.println("warf' dvd button 8"); break;//83601495
    case 83617815:  Serial.println("warf' dvd button 9"); break;//83617815
    case 83597925:  Serial.println("warf' dvd button 0"); break;//83597925
    case 83612205:  Serial.println("warf' dvd 10+     "); break;//83612205
    case 83618325:  Serial.println("warf' dvd v.mode  "); break;//83618325
    case 83591805:  Serial.println("warf' dvd osd     "); break;//83591805
    case 83583645:  Serial.println("warf' dvd subtitle"); break;//83583645
    case 83577525:  Serial.println("warf' dvd title   "); break;//83577525
    case 83577015:  Serial.println("warf' dvd search  "); break;//83577015
    case 83607615:  Serial.println("warf' dvd repeat  "); break;//83607615
    case 83574975:  Serial.println("warf' dvd a-b rpt "); break;//83574975
    case 83609655:  Serial.println("warf' dvd clear   "); break;//83609655
    case 83593845:  Serial.println("warf' dvd pal/ntsc"); break;//83593845
    case 83561205:  Serial.println("warf' dvd setup   "); break;//83561205
    case 83564775:  Serial.println("warf' dvd menu    "); break;//83564775
    case 83599965:  Serial.println("warf' dvd zoom    "); break;//83599965
    case 83622405:  Serial.println("warf' dvd prog    "); break;//83622405
    case 0x90:   Serial.println(" channel up     "); break;//0x90
    case 0x890:  Serial.println(" channel down   "); break;//0x890
    default: Serial.println(" other button   ");
  }
  delay(500);
}
void twonumbers()//button 3  print a on next to last digit, or last digit if less then 10
{  
  a=(millis()/1000);  //Serial.print ("   ");  //Serial.print (a);
  if (a>99)
  {
    a=(millis()/1000);
  }
  //if (f!=a)//only clearscreen when number changes
  //{
  //  f=a;
  //}
  if (a<10)
  {
    lcd.setCursor(6,1);
    lcd.print(" "); 
   // lcd.print(a);
  }
  else
  {
    lcd.setCursor(6,1); 
    //lcd.print(a);
  }
}
void pow()

  {
    for (int c=9;c>-1;c--)
    if (c>0)
    {
      lcd.setCursor(0,0); 
      lcd.print(c);
      delay(500);
    }
    else
    {
      lcd.setCursor(0,0); 
      lcd.print("pow");
      delay(1000);
    }
  }
  
void numbers_to_right()//count up from the left in hexidecimal
{
  for (i=0; i<16; i++)//for (int z=0; z<1; z++)
  {
    lcd.setCursor(i,0); 
    lcd.print(i,HEX);
    delay(500);
    lcd.setCursor(i,0);
    lcd.print(" ");
  }
   for (i=0; i<16; i++)//for (int z=0; z<1; z++)
  { 
    lcd.setCursor(i,1); 
    lcd.print(i,HEX);//print on the second half and print in hex i.e. 8,9,A,B etc
    delay(500);
    lcd.setCursor(i,1);
    lcd.print(" ");
  }
}
/*
void clock()
{
  if (seconds>58)
  {
    clearscreen();
    minutes=(minutes+1);
    seconds=0;
  }
  lcd.setCursor(2,0);
//  lcd.print(":  :");
  if (minutes<10)
  {
    lcd.setCursor(3,0);
//    lcd.print("0");
  }
  else
  lcd.setCursor(3,0);
//  lcd.print(minutes);
  d=(millis()/1000);
  if (e<(d-1000))
  {
    seconds=(seconds+1);
    e=d;
  }
  if (seconds<10)
  {
    lcd.setCursor(6,0);
 //   lcd.print("0");
  }
    else
    lcd.setCursor(6,0);
 // lcd.print(seconds);

  delay (500);
}
*/
void clock2()
{
  if (t <(millis()-1000))
  {
    sec = (sec+1);
    t=millis();
  }
  if (sec>59)
  {
    min = (min+1);
    sec = 0;
  }
  if (min>59)
  {
    hour = (hour+1);
    min = 0;
  }
}

and

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 * altered to work with any ir chip.
 * left to right output, 0 volts, vcc.
 * except ir with four pins on the top, left to right  output, vcc, 0 volts.
 * when using wharfdale dvd remote
 *    1. puts "warf dvdbutton 1" on screen
 *    2. puts "        button 2" on screen
 *    3. puts a count on the last two elements that keep counting even when not showing using variable 'a'
 *    4. puts a count down on element 1 and then puts pow on the left side and repeats.
 *    5. counts up from the left on each element in hexidecimal.
 *    6. clock on lcd display.
 */
#include <IRremote.h>//remove the robot libraries and include kens from here  https://github.com/shirriff/Arduino-IRremote
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip
#include "LedControl.h"//turns 8 digits off see lc.control below & next line
int RECV_PIN = 5;
const byte IR_RECEIVE_PIN = 5; //  receive pin
char buffer [9] = ""; //set up so button 6 gives a timer
int a;//number to show on lcd
int b=0;//show time on lcd
int c;
long unsigned int d;//save millis count
int e;//millis compare
int f=0;//compare a to f to clearscreen each second
int i;
int hour=0;int min=0;int sec=0;
unsigned long t=0;//for void clock
void setup()
{
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);//use pin 13
  lcd.begin (16,2);  // initialize the lcd
// Switch off the backlight
//  lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
//  lcd.setBacklight(LED_OFF);
  lcd.clear();
  delay(100);
  lcd.home();  
//  lc.shutdown(0,true);//turns 8digits off, see include ledcontrol above
//  lcmatrix.shutdown(0,true);
//  lcmatrix.shutdown(1,true);
 // Print on the LCD
  lcd.setCursor(0,0); 
  lcd.print("Hello, this is");
  lcd.setCursor(1,1); 
  lcd.print("my world!");
}

void loop() 
{
  if (IrReceiver.decode()) // have we received an IR signal?
  {
    lcd.clear();//clears the screen every time the remote is pressed
    //for (int z=0; z<1; z++) // ignore 2nd and 3rd signal repeat
    
    translateIR();
    Serial.print ("here");
    Serial.print ("\n");// same as using Serial.println
    IrReceiver.resume(); // receive the next value
    
    Serial.print ("here too");
  }
  if (b==1) warf1();                 //button 1  put warf dvd button1 on lcd
  if (b==2) warf2();                 //button 2  put warf dvd button2 on led and scroll
  if (b==3) twonumbers();            //button 3  count on elements 15 and 16
  if (b==4) pow();                   //button 4  count down to "pow" on element 1
  if (b==5) numbers_to_right();      //button 5  count up along both rows
  if (b==6) {setclock();clock();}    //button 6  put a clock on the first line
  if (b==7) keywords();
/*
  lcd.backlight();
  delay (500);
  lcd.noBacklight();
  delay(500);
  */
}
void translateIR() // takes action based on IR code received
                   // describing Sony IR codes on LCD module
{
 // Serial.println(results.value, DEC);
  switch(IrReceiver.decodedIRData.command)
  {
    case 9:Serial.println("warf' dvd button 1");b=1;break;
    case 29:Serial.println("warf' dvd button 2");b=2;break;
    case 31:Serial.println("warf' dvd button 3");b=3;break;
    case 83603535:Serial.println("warf' dvd button 4");b=4;break;
    case 83597415:Serial.println("warf' dvd button 5");b=5;break;
    case 83613735:Serial.println("warf' dvd button 6");b=6;break;
    case 83593335:Serial.println("warf' dvd button 7");b=7;break;
    case 83601495:Serial.println("warf' dvd button 8");b=8;break;
    case 83617815:Serial.println("warf' dvd button 9");b=9;break;
    case 83597925:Serial.println("warf' dvd button 0");b=0;break;
  }
  delay(500);
}
void warf1()//button 1
{ 
  lcd.setCursor(0,0);lcd.print("warf dvd");b=1;lcd.setCursor(0,1); 
  lcd.print("button 1");
}
void warf2()//button 2
{
  lcd.setCursor(0,0);lcd.print("warf dvdbutton2");
  /*
  for (int positionCounter = 0; positionCounter < 32; positionCounter++) 
  {
    lcd.scrollDisplayLeft(); // scroll one position left:
    delay(500);  // wait a bit:
  }
  lcd.setCursor(0,1);lcd.print("warf dvdbutton2");
  for (int positionCounter = 16; positionCounter >0; positionCounter--) 
  {
    lcd.scrollDisplayRight(); // scroll one position right:
    delay(500);  // wait a bit:
  }*/
}
void twonumbers()//button 3, print a on next to last digit, or last digit if less then 10
{  
  a=(millis()/1000);  //Serial.print ("   ");  //Serial.print (a);
  if (a>99)
  {
    a=(millis()/1000);
  }
  //if (f!=a)//only clearscreen when number changes
  //{
  //  f=a;
  //}
  if (a<10)
  {
    lcd.setCursor(6,1);
    lcd.print(" "); 
    lcd.print(a);
  }
  else
  {
    lcd.setCursor(6,1); 
    lcd.print(a);
  }
}
void pow()//button 4
{
  for (c=9;c>-1;c--)
  if (c>0)
  {
    lcd.setCursor(0,0); 
    lcd.print(c);
    delay(500);
  }
  else
  {
    lcd.setCursor(0,0); 
    lcd.print("pow");
    delay(1000);
  }
}  
void numbers_to_right()//button 5, count up from the left in hexidecimal
{
  for (i=0; i<16; i++)
  {
    lcd.setCursor(i,0); 
    lcd.print(i,HEX);//print on the first line
    delay(500);
    lcd.setCursor(i,0);
    lcd.print(" ");
  }
  for (i=0; i<16; i++)
  { 
    lcd.setCursor(i,1); 
    lcd.print(i,HEX);//print on the second line
    delay(500);
    lcd.setCursor(i,1);
    lcd.print(" ");
  }
}
void setclock()//button 6
{
  if (t <(millis()-1000)){sec = (sec+1);t=millis();}
  if (sec>59){min = (min+1);sec = 0;}
  if (min>59){hour = (hour+1);min = 0;}
}
void clock()//button 6
{
  lcd.setCursor(0,0);
  sprintf(buffer, "%02d:%02d:%02d", hour, min, sec);
  lcd.print(buffer); // You will get "13:02:00" format
}
void keywords()
{
  /*
//........blink.........  
  lcd.setCursor(0,0);
  lcd.noDisplay();
  lcd.print("this is a test");//blinking due to display being turned on and off
  delay(500);
  lcd.display();
  delay(500);
//....................
  */
  /*
  //..... blink both lines...........
  lcd.setCursor(0,0);
  lcd.noBlink();
  lcd.noCursor();
  lcd.print("this is a test");//on line 1
  lcd.setCursor(0,1);
  lcd.cursor();//blinking on line 2, at end of printed words
  lcd.blink();//only makes cursor blink, use above to make line blink
  lcd.print("this is another");//on line 2
  delay(500);
  //............................
  */


//..........scrollright..............
lcd.home();
lcd.setCursor(0,0);
lcd.print("the answer");
for (int l=0; l<5; l++)
  {
  lcd.scrollDisplayRight();
  delay(500);
  }
  //.....................
  
}


They are both a bit long but both have a void called twonumbers()

void twonumbers()//button 3, print a on next to last digit, or last digit if less then 10
{  
  a=(millis()/1000);  //Serial.print ("   ");  //Serial.print (a);
  if (a>99)
  {
    a=(millis()/1000);
  }
  if (a<10)
  {
    lcd.setCursor(6,1);
    lcd.print(" "); 
    lcd.print(a);
  }
  else
  {
    lcd.setCursor(6,1); 
    lcd.print(a);
  }
}

If I run the second one it runs fine but the first one fails to see any input from the IR transmitter after I press button 3 and the 'two numbers' void runs. All subsequent inputs are seen as '0'. If I comment out the two 'lcd.print(a);' lines I get nothing on the display but the IR receiver continues to see all subsequent inputs? Can anyone see a difference with them?

It's a function (not a "void").

1 Like

First sketch has the above. Second does not.

Make 'a' unsigned long (millis() will overflow an int) and set it to zero.

They both have int a; or at least the first has int a=0; and the second only has int a;
Anyway I replaced them both with unsigned long a=0; and the problem is still there on the second and not the first.
The third code is just a small section from either of them to show where the problem is and doesn't contain any assigning.

Yes, I know.

Try Serial.print(a) and Serial.write(a) in both to see what the value looks like as a character and as a value.

1 Like

If I change both of them to the code below the good one seems to just put random digits on the monitor at first and then square boxes. The bad one seems to show the number generated by a until it gets to 10 and then square boxes?
It doesn't seem to be uniform at all?

 {
    lcd.setCursor(6,1);
    lcd.print(" "); 
    lcd.print(a);
    Serial.print(a);
   }
  else
  {
    lcd.setCursor(6,1); 
    lcd.print(a);
    Serial.write(a); 
  }

Would you copy/paste the output?

  • Numbers are the ASCII values in decimal.
  • Empty boxes are unprintable/control characters.
  • random might mean baud mismatch from Serial.begin(???) and the output monitor baud.

I don know where you are re initializating the variable 'b'. if you are not doing that, in the void loop, the comparations with that variable must goes inside the previous comparation (if (IrReceiver.decode()))

Xakko

Soirry I am in so much pain with tooth ache I am going to have to delay this for a day.

1 Like

:grimacing:

That’s tough
:tooth:

right, lets try again.
I use linux and I don't have a snippet tool as far as I know so I will give you a photo.


If I press button 3 before variable 'a' has reached 10 then it prints a row of numbers the same as variable 'a', once 'a' has reached 10 I get either the row of boxes (none printable characters) or just a row of random characters, (each row being the same character)
If I press button two it takes me back to 'warf' dvd button 2 (it also prints here too or here depending on where it is. This is the good one.
The bad one prints warf' dvd button 3 and then the row of random characters.
At this point pressing button 2 has no effect it stays on 3. I can see that it fails to return variable b to anything other than '0'

In a lot of desktop environments you can just hit the Print Screen key and a screenshotter app will come up.

The snippet tool in linux is called snapshot and can be placed on the panel.
I am using switch and case in my program, for some reason if I get close to 32 lines of case it gets unstable. I want to use a tv remote with the ir input and the remote has 36 pins so assigning most of them makes the program do all sorts of odd things? Comment some of them out (any) and it clears up?

Can you post your latest code that gets unstable with 32+ cases? Some people in this forum have done hundreds of cases with no problem so it could be an issue with other code (if there's undefined behavior, for example, then anything goes).

Hi Christop,
Firstly Linux has a built in snippet tool. It is called screenshot.
I have added it to my panel and can now do screenshots in the same way as I can do snippets in windows.
I have found the problem with the restriction with 32 cases with the help of people on this site..
See this one
The problem is that each of the cases prints something on the LCD and each print takes up memory space, when it gets close to the limit of memory space it does unpredictable things.
Short of not using so many print statements if I put an F before each print statement as in

case 1436: Serial.println(F(" Play button    ")); break;

as opposed to

case 1436: Serial.println(" Play button    ")  break;

The amount of used memory is drastically reduced giving me more space and a stable program.
That is why I have put [SOLVED] in the heading.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.