1

Click here to see my program running

I have created a 2D array (see my full code below), and each row contains one state of a battery here is a schematic :

row [0] : "▯▢▢▷" // this row represents an empty AAA battery 
row [1] : "▮▩▢▷"
row [2] : "▮▩▩▷"
row [3] : "▮▩▩ ▶"  // this row represents a Full AAA battery    

Each "▩" character is a custom made character with this function:

lcd.createChar(0, customLcdfont_squareONE);

As you saw in my video it works at the end, but it's absolutely not the way I want it to work. The code that works is a mess!

I think the problem is that the Raw n°1 ([0]) starts with a 0, (try it out it's very strange). If we replace

char batteryFonts [10][5] = {
  { 0, 1, 1, 2 }, // you see { this "0", 1, 1, 2 }, is a problem I think 
  { 3, 1, 1, 2 },
  {etc}, }
lcd.print( batteryFonts[0]);  // this sentence does not work

with

"char batteryFonts [10][5]={{ 4, 1, 1, 2 }, // 4 or what ever you want 
  {etc}, 
  {etc}, }
 lcd.print( batteryFonts[0]);  // this sentence behind does work, but only with a row that does not start with a 0. Why?

It works!

Here is my full code. Just connect the pins 12, 13, 7, 4, 3, 2 to the LCD.

//This is a simple program that charges alkaline batteries 
//Copyleft
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 13, 7, 4, 3, 2); // why pin 13 and not 12 ? because I love the led attached to it <3


// the empty battery - end 
byte customLcdfont_A [8] = { // B stand for "Binary" and then the pixel ... 
B01111,
B11000,
B10101,
B10100,
B10100,
B10100,
B11000,
B01111

// the empty battery body with fancy graphics enabled

};
byte customLcdfont_B [8] = { // B stand for "Binary" and then the pixel ... 
B11111,
B00000,
B11001,
B00000,
B00000,
B00000,
B00000,
B11111
};


// the empty battery + end

byte customLcdfont_C [8] = { // B stand for "Binary" and then the pixel ... 
B11100,
B00100,
B10111,
B00101, 
B00111,
B00111,
B00100,
B11100
};


// the full battery - end

byte customLcdfont_D [8] = { // B stand for "Binary" and then the pixel ... 
B1111,
B11111,
B10110,
B10111,
B10111,
B10111,
B11111,
B1111
};


// the FULL battery + end
byte customLcdfont_E [8] = { // B stand for "Binary" and then the pixel ... 
B11100,
B11100,
B01111,
B11101,
B11111,
B11111,
B11100,
B11100
};

// the full battery  body

byte customLcdfont_F [8] = {
B11111,
B11111,
B00110,
B11111,
B11111,
B11111,
B11111,
B11111
};

// the 1/3 battery body 

byte customLcdfont_G [8] = {
B11111,
B11000,
B00001,
B11000,
B11000,
B11000,
B11000,
B11111
};
// the 2/3 battery body 

byte customLcdfont_H [8] = {
B11111,
B11110,
B00111,
B11110,
B11110,
B11110,
B11110,
B11111
};



/**************************************************************************/
void setup() {
  int i = 0 ; 
  // initialize serial communications at 9600 bps:
  lcd.createChar(0, customLcdfont_A);
  lcd.createChar(1, customLcdfont_B);
  lcd.createChar(2, customLcdfont_C); 
  lcd.createChar(3, customLcdfont_D);
  lcd.createChar(4, customLcdfont_E);
  lcd.createChar(5, customLcdfont_F);
  lcd.createChar(6, customLcdfont_G);
  lcd.createChar(7, customLcdfont_H);

  lcd.begin(16, 2); 

// WATCH HERE IS THE PROBLEM !!!!

//the char table behind creates my different battery states as shown in the video ;) 

 char batteryFonts [10][5]={

 { 0, 1, 1, 2 }, // this is an empty one  f%*# this does not work !!!
 { 3, 1, 1, 2 },
 { 3, 6, 1, 2 },
 { 3, 7, 1, 2 },// this is a 50 % full charged battery ! 

 { 3, 5, 1, 2 },
 { 3, 5, 6, 2 },
 { 3, 5, 7, 2 }, 
 { 3, 5, 5, 2 },
 { 3, 5, 5, 4 },// this is nice full one  
 };



 // this sentence behind does not work !!! but only with the raw [0]

 lcd.print( batteryFonts[0]);    // I F***ing want this to display the first raw !!!! 
 lcd.setCursor(0,1 );

  lcd.print(" not working ");
 lcd.setCursor(0,0 );




        delay(9300); // this make a short pause  for us so that we can see the result
lcd.setCursor(0,1 );

  lcd.print("    working      ");
 lcd.setCursor(0,0 );
      for (i=0; i=10 ; i+=1 ){
       delay(300);

  lcd.setCursor(6,0 );

  lcd.write(byte(0));   lcd.write(byte(1)); lcd.write(byte(1)); lcd.write(byte(2));
    delay(700); //  batterry 0/8 empty aka discharged


  lcd.setCursor(6,0 );
  lcd.write(byte(3));   lcd.write(byte(1)); lcd.write(byte(1)); lcd.write(byte(2));
    delay(1000); //batterry 1 /8

  lcd.setCursor(6,0 );
  lcd.write(byte(3));   lcd.write(byte(6)); lcd.write(byte(1)); lcd.write(byte(2));
   delay(1000);//batterry 2 /8

  lcd.setCursor(6,0 );
   lcd.write(byte(3));   lcd.write(byte(7)); lcd.write(byte(1)); lcd.write(byte(2)); //batery 3 /8

  delay(1000);

  lcd.setCursor(6,0 );
   lcd.write(byte(3));   lcd.write(byte(5)); lcd.write(byte(1)); lcd.write(byte(2));
  delay(1000); //batery 4 /8

  lcd.setCursor(6,0 );
        lcd.write(byte(3));  lcd.write(byte(5));     lcd.write(byte(6));   lcd.write(byte(2));
  delay(1000); //batery 5 /8

  lcd.setCursor(6,0 );
      lcd.write(byte(3));  lcd.write(byte(5));   lcd.write(byte(7));  lcd.write(byte(2));
        delay(1000); //batery 6 /8

  lcd.setCursor(6,0 );
      lcd.write(byte(3));  lcd.write(byte(5));   lcd.write(byte(5));  lcd.write(byte(2));
      delay(1000); //batery 7 /8

  lcd.setCursor(6,0 );
    lcd.write(byte(3));  lcd.write(byte(5));     lcd.write(byte(5));    lcd.write(byte(4)); //batery 8 /8

    delay(1000); 

      }


    lcd.setCursor(15, 1);

    delay(115000);

}



/**************************************************************************/
void loop() {

//This is not interesting here 
  lcd.print(" ~ The end of the code ... thank you for reading ;) ~ ");

}

1 Answer 1

2

You are correct; in C a NUL (0x00) is used to indicate the end of a char*. You will need to use LiquidCrystal::write() in order to send the character data byte by byte instead of depending on LiquidCrystal::print() to handle it.

Also, consider moving all of that static data into flash; you're going to need to handle it manually so you may as well do it right in the first place.

5
  • This confirms my doubs ! It's sloppy isn't it ? It's the same as in the C89 I studied, but slightly uncomfortable and I think the arduino compiler should be updated to avoid this "issue" Dec 1, 2014 at 19:51
  • It has nothing at all to do with the compiler. It is a fundamental tenet of C/C++, and LiquidCrystal::print() adheres to it. Dec 1, 2014 at 19:56
  • Oh realy ? So I should study more ! Dec 1, 2014 at 19:59
  • I don't say thanks... as the comment box told me to avoid to say thanks (y) But I realy want to ! Dec 1, 2014 at 20:00
  • It's okay, your sentiment has reached me. Dec 1, 2014 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.