As long as a piece of String(); ????

Hello Guys

I wonder if there is anybody charitable enough to help me with this … … … … I don’t need telling how poor my code is - I already know … it’s because I’m a novice !

Here is the code … …

#include <UTFT.h>
#include <UTFT_Buttons.h>


extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t Ubuntu[];

const int SwitchUp = 9;
const int SwitchDown = 15;
const int SwitchRight = 16;
const int SwitchLeft = 8;
const int SwitchMenu = 10;
const int SwitchEnter = 14;



int long pressed_button = 0;
int long pressed_buttonB = 0;

char Str1[4] = {'A', 'B', 'C'};
char Str2[4] = {'D', 'E', 'F'};
char Str3[4] = {'G', 'H', 'I'};
char Str4[4] = {'J', 'K', 'L'};
char Str5[4] = {'M', 'N', 'O'};
char Str6[4] = {'P', 'R', 'S'};
char Str7[4] = {'T', 'U', 'V'};
char Str8[4] = {'W', 'X', 'Y'};


UTFT          myGLCD(ITDB32S, 38, 39, 40, 41);
UTFT_Buttons  myButtons();

void setup()
{
  pinMode(SwitchUp, INPUT_PULLUP);
  pinMode(SwitchDown, INPUT_PULLUP);
  pinMode(SwitchRight, INPUT_PULLUP);
  pinMode(SwitchLeft, INPUT_PULLUP);
  pinMode(SwitchEnter, INPUT_PULLUP);
  pinMode(SwitchMenu, INPUT_PULLUP);

  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.fillScr(VGA_GREEN);


  Serial.begin(9600);

  myGLCD.setBackColor(VGA_LIME);
  myGLCD.setColor(VGA_WHITE);
  myGLCD.setFont(Ubuntu);

}

void loop()
{

  if (digitalRead(15) == LOW) {
    pressed_buttonB = 1;
    Serial.println ("But X");
    myGLCD.setColor(VGA_BLACK);
    myGLCD.print(Str1, 150, 180);

  }
}

This is part of a much larger sketch which is a maze game for my grandkids Xmas stocking.

I’m trying to get the last line to print a single character of the string;
I’ve tried Str1(1); String.charAt; and various other suggestions I’ve found on the net … but they all return a variety of errors, some of which I understand, some not so.

Any pearls of wisdom around please ?

S

char Str1[4] = {'A', 'B', 'C'};this is not a 'String' but a char array, you can print the character bymyGLCD.print(Str1[1], 150, 180);which should print the 'B' (the 'A' is Str1[0] )

There is a few more things i would want to tell you,
-if you declare Str[8][4] = {{'A','B','C'} , {'D','E' .. etc.. 'Y'} };
you can refer to Str[0][2] == 'C'

  • your array has 4 characters and you only use 3, now it is probably Null terminated by accident Str1[3] == '\0' and there can be printed as a whole c-string but there is no guarantee that the final character is 'Null' if you want the char-array to be use-able as a c-string you should explicitly declare it as such.char Str1[4] = {'A', 'B', 'C', '\0'};

Hi Deva_Rishi

Thanks for your very prompt response … … …

LOL … … … I’ve spent 2 days looking for that simple solution … Thank you very much.

I was aware of this … Str[8][4] = {{‘A’,‘B’,‘C’} , {‘D’,‘E’ … etc… ‘Y’} }; … but the array is also used to label a series of 8 buttons, arranged like an old fashioned telephone keypad. I listed them in separately to avoid tying myself up in knots which I find easily done !

However, your further points are well received … … …

Thanks again

I’m back … … tried it … no go … …

This is what I’ve done … …

#include <UTFT.h>
#include <UTFT_Buttons.h>


extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t Ubuntu[];

const int SwitchUp = 9;
const int SwitchDown = 15;
const int SwitchRight = 16;
const int SwitchLeft = 8;
const int SwitchMenu = 10;
const int SwitchEnter = 14;



int long pressed_button = 0;
int long pressed_buttonB = 0;

char Str1[4] = {'A', 'B', 'C'};
char Str2[4] = {'D', 'E', 'F'};
char Str3[4] = {'G', 'H', 'I'};
char Str4[4] = {'J', 'K', 'L'};
char Str5[4] = {'M', 'N', 'O'};
char Str6[4] = {'P', 'R', 'S'};
char Str7[4] = {'T', 'U', 'V'};
char Str8[4] = {'W', 'X', 'Y'};


UTFT          myGLCD(ITDB32S, 38, 39, 40, 41);
UTFT_Buttons  myButtons();

void setup()
{
  pinMode(SwitchUp, INPUT_PULLUP);
  pinMode(SwitchDown, INPUT_PULLUP);
  pinMode(SwitchRight, INPUT_PULLUP);
  pinMode(SwitchLeft, INPUT_PULLUP);
  pinMode(SwitchEnter, INPUT_PULLUP);
  pinMode(SwitchMenu, INPUT_PULLUP);

  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.fillScr(VGA_GREEN);


  Serial.begin(9600);

  myGLCD.setBackColor(VGA_LIME);
  myGLCD.setColor(VGA_WHITE);
  myGLCD.setFont(Ubuntu);

}

void loop()
{

  if (digitalRead(15) == LOW) {
    pressed_buttonB = 1;
    Serial.println ("But X");
    myGLCD.setColor(VGA_BLACK);
    myGLCD.print(Str1[1], 150, 180);

  }
}

Prints nothing at all, but if I revert to just Str1, it prints the whole array as expected …

Wrong ??

S

Wrong ??

No in a way you did it correctly, but if the 'print()' function takes a char * as an argument then it may not behave as expected, try

char ch = Str1[1];
myGLCD.print(ch, 150, 180);

I'll be damned if you have to convert it to an actual 'char *' first, but them it would come out as something like this

char * ch= (char *) " ";
  *ch = Str1[1];
  myGLCD.print(ch);

in general it does work if the .print() function inherits from the stream class

void setup() {
  Serial.begin(115200);
  char Str1[4] = {'A', 'B', 'C'};
  Serial.print(Str1[1]);
}

void loop() {}

print the 'B'

Does your serial monitor show the "But X"? If so, one possible way to debug is to add a serial print before or after the myGLCD print.

If you don't see "But X" in the serial monitor, you have another problem.

Hi Deva Rishi

Firstly, I must apologise for disappearing suddenly .. . at my age, if I stare at code on a screen too long it makes my brain hurt and a good dose of Aspirin and a few zzzzzz's are required to remedy.

This works !! .. solves my problem .. .. .

char * ch= (char *) " ";
*ch = Str1[1];
myGLCD.print(ch);

The Serial.print option I had already tried and it also worked.

Many Thanks again for your help

Best Regards

S

This works !! .. solves my problem .. .. .

Great, elegant enough i suppose.
The bug is in the library, this is the work-around.

Deva_Rishi:
Great, elegant enough i suppose.
The bug is in the library, this is the work-around.

Works well for me, and since I'm only just getting my head around basic code, if I start messing about with libraries we're ALL in trouble !!

Thanks very much for your help

S