Arduino Forum

Using Arduino => Displays => Topic started by: LukeOliver2001 on Dec 03, 2019, 01:12 pm

Title: Missing first character on screen
Post by: LukeOliver2001 on Dec 03, 2019, 01:12 pm
Hi all, I now have a problem trying to get my text to show the first character. For example, if i set the force as 100 it will only read 00. everything's working fine normally i would just put a space in front of my text however, i can't do it this time becsause i'm displaying a variable.
thanks
Code: [Select]
  #include <Wire.h>

  #define LCD05  0x63                   // LCD05 address

  char stringBuf[20];
  char stringBuf1[4];
  byte buffer[4];
  // constants won't change. They're used here to
  // set pin numbers:
  const int buttonPin1 = 11;     // the number of the pushbutton pin
  const int buttonPin2 = 10;
  const int buttonPin3 = 9;
  const int buttonPin4 = 8;
 
  // variables will change:
  int buttonState1 = 0;         // variable for reading the pushbutton status
  int buttonState2 = 0;
  int buttonState3 = 0;
  int buttonState4 = 0;


  int force = 100;


 


  void setup() {
  delay(100);                          // Delay to wait for everything to power up
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);


 
  Wire.begin();
 
  buffer[0] = 0;                       // Clear the screen
  buffer[1] = 12;
  buffer[2] = 19;
  buffer[3] = 31;
  buffer[4] = 10;
 
 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
  }

  void loop() {



  while (buttonState1 == LOW){
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

  if (buttonState3 == HIGH){
    force = force +1;

    }

 

  if (buttonState4 == HIGH){
    force = force -1;
 
    }


//    if (force <= 0){
  //    force = 0;
    //}
 

/*
  Wire.begin();
  buffer[1] = 19;
  buffer[2] = 3;
  buffer[3] = 3;
  buffer[4] = 1; 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
 
   
  String message1 = "   ";      // The message to be put on the screen
  int len1 = message1.length() + 1;       // Length of the message
  message1.toCharArray(stringBuf, len1);  // Convert the message to a car array
  stringBuf[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf, len1);
  Wire.endTransmission();
  */

  Wire.begin();
  buffer[0] = 0;
  buffer[1] = 19;
  buffer[2] = 3;
  buffer[3] = 3;
  buffer[4] = 15; 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
 
      String displayforce = String(force);    // The message to be put on the screen
 int len2 = displayforce.length() + 1;       // Length of the message

  displayforce.toCharArray(stringBuf1, len2);  // Convert the message to a car array
  //itoa(force,stringBuf1,5);
 
//  stringBuf1[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf1, len2);
  Wire.endTransmission();
 

   
  delay(150);
   
 


  }







 
  }

 
Title: Re: Missing first character on screen
Post by: PerryBebbington on Dec 03, 2019, 01:34 pm
Code: [Select]
byte buffer[4];

Creates an array of 4 bytes.
Code: [Select]
 buffer[0] = 0;                       // Clear the screen
  buffer[1] = 12;
  buffer[2] = 19;
  buffer[3] = 31;
  buffer[4] = 10;

That's 5 bytes, the last one

Code: [Select]
buffer[4]
will be in memory used for something else and will corrupt it.

If you create an array with 4 bytes then the are:

Code: [Select]
[0], [1], [2], [3]. There is no [4]

Futher on you use

Code: [Select]
[1] to [4]
instead of

Code: [Select]
[0] to [3]







Title: Re: Missing first character on screen
Post by: LukeOliver2001 on Dec 03, 2019, 03:09 pm
I've tried this but still having no luck :(.
Code: [Select]
#include <Wire.h>

  #define LCD05  0x63                   // LCD05 address

  char stringBuf[20];
  char stringBuf1[4];
  byte buffer[4];
  // constants won't change. They're used here to
  // set pin numbers:
  const int buttonPin1 = 11;     // the number of the pushbutton pin
  const int buttonPin2 = 10;
  const int buttonPin3 = 9;
  const int buttonPin4 = 8;
 
  // variables will change:
  int buttonState1 = 0;         // variable for reading the pushbutton status
  int buttonState2 = 0;
  int buttonState3 = 0;
  int buttonState4 = 0;


  int force = 100;



  void setup() {
  delay(100);                          // Delay to wait for everything to power up
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);


 
  Wire.begin();
 
  buffer[0] = 12;                       // Clear the screen
  buffer[1] = 19;
  buffer[2] = 31;
  buffer[3] = 10;

 
 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,4);
  Wire.endTransmission();
  }

  void loop() {



  while (buttonState1 == LOW){
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

  if (buttonState3 == HIGH){
    force = force +1;

    }

 

  if (buttonState4 == HIGH){
    force = force -1;
 
    }


//    if (force <= 0){
  //    force = 0;
    //}
 

/*
  Wire.begin();
  buffer[1] = 19;
  buffer[2] = 3;
  buffer[3] = 3;
  buffer[4] = 1; 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
 
   
  String message1 = "   ";      // The message to be put on the screen
  int len1 = message1.length() + 1;       // Length of the message
  message1.toCharArray(stringBuf, len1);  // Convert the message to a car array
  stringBuf[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf, len1);
  Wire.endTransmission();
  */

  Wire.begin();
  buffer[0] = 19;
  buffer[2] = 3;
  buffer[3] = 3;
  buffer[4] = 15; 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
 
      String displayforce = String(force);    // The message to be put on the screen
 int len2 = displayforce.length() + 1;       // Length of the message

  displayforce.toCharArray(stringBuf1, len2);  // Convert the message to a car array
  //itoa(force,stringBuf1,5);
 
//  stringBuf1[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf1, len2);
  Wire.endTransmission();
 

   
  delay(150);

  }
while (buttonState1 == LOW){

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);


Wire.begin();

  buffer[0] = 19;
  buffer[1] = 3;
  buffer[2] = 3;
  buffer[3] = 1; 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,4);
  Wire.endTransmission();
 
   
  String message1 = "  hi ";      // The message to be put on the screen
  int len1 = message1.length() + 1;       // Length of the message
  message1.toCharArray(stringBuf, len1);  // Convert the message to a car array
  stringBuf[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf, len1);
  Wire.endTransmission();


}

 
  }

 
 
Title: Re: Missing first character on screen
Post by: PerryBebbington on Dec 03, 2019, 04:12 pm

Quote
I've tried this but still having no luck
You've not done what I said.
Title: Re: Missing first character on screen
Post by: LukeOliver2001 on Dec 03, 2019, 04:18 pm
If I change byte buffer it either doesn't change at all or it stays the same same goes for all the other buffers if I change them starting from 0 instead of 1.
Title: Re: Missing first character on screen
Post by: PerryBebbington on Dec 03, 2019, 04:44 pm
You still have at least one instance of:
Code: [Select]
  buffer[4] = 1; 

In your code. I don't know if that's your only problem but while there is an obvious thing that's wrong it doesn't make any sense to look elsewhere for problem.

Title: Re: Missing first character on screen
Post by: gilshultz on Dec 03, 2019, 09:52 pm
A hint, print the space before you print the variable. If you are worried about buffer size add 1 to it but it appears you know about zero. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil
Title: How do i add a space within but before a variable?
Post by: LukeOliver2001 on Dec 09, 2019, 10:26 am
Hi all, I'm having trouble displaying my variable. Normally if I wanted to display a message all I would have to do is add a space in front then the message and then it reads fine. So what I'm trying to do is add a space before a variable so that when I want the variable "Force" to display 100 it doesn't just read 00 it will have the first character of 100.

How would I go about doing this without adding numbers in front because ill need to use the overall number later on to do some equations.

Thanks
Title: Re: How do i add a space within but before a variable?
Post by: 6v6gt on Dec 09, 2019, 10:46 am
Normally, numeric variables don't have embedded spaces in them. Only when you come to print them in some way, do you start formatting them to display nicely.
Functions like snprintf() can be used for extensive formatting of numbers.
Maybe post the code you are having trouble with and say how the output should appear.
Title: Re: Missing first character on screen
Post by: UKHeliBob on Dec 09, 2019, 12:01 pm
Topics merged
Title: Re: Missing first character on screen
Post by: LukeOliver2001 on Dec 09, 2019, 12:34 pm
Code: [Select]
#include <Wire.h>

  #define LCD05  0x63                   // LCD05 address

  char stringBuf[24];
  char stringBuf1[24];
  byte buffer[4];
  // constants won't change. They're used here to
  // set pin numbers:
  const int buttonPin1 = 11;     // the number of the pushbutton pin
  const int buttonPin2 = 10;
  const int buttonPin3 = 9;
  const int buttonPin4 = 8;
 
  // variables will change:
  int buttonState1 = 0;         // variable for reading the pushbutton status
  int buttonState2 = 0;
  int buttonState3 = 0;
  int buttonState4 = 0;
 

  int force = 100;



  void setup() {
  delay(100);                          // Delay to wait for everything to power up
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);


 
  Wire.begin();
 
  buffer[0] = 24;
  buffer[1] = 4;                      // Clear the screen
  buffer[2] = 19;
  buffer[3] = 31;
  buffer[4] = 150;
 
 
 
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
  }

  void loop() {



  while (buttonState1 == LOW){
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

  if (buttonState3 == HIGH){
    force = force +1;

    }

 

  if (buttonState4 == HIGH){
    force = force -1;
 
    }

/*
   if (force <= 0){
     force = 0;
    }
 

/*
  Wire.begin();
  buffer[1] = 19;
  buffer[2] = 3;
  buffer[3] = 3;
  buffer[4] = 1;
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,5);
  Wire.endTransmission();
 
 
  String message1 = "  100 ";      // The message to be put on the screen
  int len1 = message1.length() + 1;       // Length of the message
  message1.toCharArray(stringBuf, len1);  // Convert the message to a car array
  stringBuf[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf, len1);
  Wire.endTransmission();
 */

  Wire.begin();

                       // Clear the screen
  buffer[0] = 19;
  buffer[1] = 3;
  buffer[2] = 3;
  buffer[3] = 5;
  buffer[4] = 31;
  buffer[5] = 150;
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,6);
  Wire.endTransmission();
 
  String displayforce = String(force);   
  int len2 = displayforce.length() + 1;       

  displayforce.toCharArray(stringBuf1, len2);
  //itoa(force,stringBuf1,5);
 
  //stringBuf1[0] = 0;                   
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf1, len2);
  Wire.endTransmission();
 

   
  delay(150);
/*
  }
while (buttonState1 == LOW){

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);


Wire.begin();

  buffer[0] = 19;
  buffer[1] = 3;
  buffer[2] = 3;
  buffer[3] = 1;
  Wire.beginTransmission(LCD05);
  Wire.write(buffer,4);
  Wire.endTransmission();
 
   
  String message1 = "  hi ";      // The message to be put on the screen
  int len1 = message1.length() + 1;       // Length of the message
  message1.toCharArray(stringBuf, len1);  // Convert the message to a car array
  stringBuf[0] = 0;                     // First byte of message to 0 (LCD05 command register)
  Wire.beginTransmission(LCD05);
  Wire.write(stringBuf, len1);
  Wire.endTransmission();
*/

}

 
  }
 
Title: Re: Missing first character on screen
Post by: UKHeliBob on Dec 09, 2019, 12:37 pm
Please edit your post and put the code in code tags.

You obviously know how to do it because you did it in post #2

What does the code do ?
What should it do ?
Title: Re: Missing first character on screen
Post by: LukeOliver2001 on Dec 09, 2019, 12:46 pm
I have hidden most of the code to make the part I'm looking at easier and less confusing which is what the /* is for
Title: Re: Missing first character on screen
Post by: david_prentice on Dec 09, 2019, 02:26 pm
Go on.   It is easy enough for you to remove redundant lines and format nicely with ctrl-T

Making that tiny effort makes all the difference to any reader response.

David.