Question about splicing an array

Hi,
In the following code, serial monitor gets the value as a,b,c,d,e,f. But I want it as ab,cd,ef. How can I do that?

void displayString(char* s)
{
  for (int i = 0; i<=strlen(s); i++)
  {
  displayChar(s[i]);
  }
}

void loop()
{
  displayString("abcdef");
}

void displayChar(char c)
{
Serial.println(c);
}

I tried changing the code to the following one,

void setup(){
  Serial.begin(9600);
}
void displayString(char* s)
{
  for (int i = 0; i<=strlen(s); i+2)
  {
  char e=s[i]+s[i+1];
  displayChar(e);
  }
}

void loop()
{
  displayString("abcdef");
}

void displayChar(char c)
{
  Serial.println(c);
}

but i am getting output like this.

I also tried strcat and strcpy, but didn't worked.

for (int i = 0; i<=strlen(s); i+2)

Two things wrong there.

i < strlen(s)
i += 2
char e=s[i]+s[i+1];

I can't see the point of summing two ASCII codes.
Why not just print them, one after the other?

AWOL:

for (int i = 0; i<=strlen(s); i+2)

Two things wrong there.

i < strlen(s)
i += 2

Thank you for reply AWOl. I modified my code with your suggestion, but its still not working.

char e=s[i]+s[i+1];

I can't see the point of summing two ASCII codes.
Why not just print them, one after the other?

I want to make a function like this.

void displayChar(char c)
{
  if (c == 'ab'){code}
if (c=='cd'){code}
}
 if (c == 'ab')

Yes, you can do that (the compiler won't complain), but you'll need an "int" variable or an array or a union.
Or a string.
As I said, summing ASCII code isn't what you want.

AWOL:

 if (c == 'ab')

Yes, you can do that (the compiler won't complain), but you'll need an "int" variable or an array or a union.
Or a string.
As I said, summing ASCII code isn't what you want.

Can you show me how to o that?

I'm not absolutely clear what it is you want to do, and what would be the best (aka most appropriate) approach.

int x = ('a' << 8) | 'b';
if (x == 'ab')

is valid (ish), but not portable, and will not expand to three character sequences.

It may be that short strings are better for your application.

AWOL:
I'm not absolutely clear what it is you want to do, and what would be the best (aka most appropriate) approach.

int x = ('a' << 8) | 'b';

if (x == 'ab')



is valid (ish), but not portable, and will not expand to three character sequences.

It may be that short strings are better for your application.

Here is the code that I am trying to modify. It for making a led pov display.

int delayTime = 1000;
int charBreak = 500;

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT); 
}

int a[] = {1, 6, 26, 6, 1};
int b[] = {31, 21, 21, 10, 0};
int c2[] = {14, 17, 17, 10, 0};
int d[] = {31, 17, 17, 14, 0};
int e[] = {31, 21, 21, 17, 0};
int f[] = {31, 20, 20, 16, 0};
int g[] = {14, 17, 19, 10, 0};
int h[] = {31, 4, 4, 4, 31};
int i[] = {0, 17, 31, 17, 0};
int j[] = {0, 17, 30, 16, 0};
int k[] = {31, 4, 10, 17, 0};
int l[] = {31, 1, 1, 1, 0};
int m[] = {31, 12, 3, 12, 31};
int n[] = {31, 12, 3, 31, 0};
int o[] = {14, 17, 17, 14, 0};
int p[] = {31, 20, 20, 8, 0};
int q[] = {14, 17, 19, 14, 2};
int r[] = {31, 20, 22, 9, 0};
int s[] = {8, 21, 21, 2, 0};
int t[] = {16, 16, 31, 16, 16};
int u[] = {30, 1, 1, 30, 0};
int v[] = {24, 6, 1, 6, 24};
int w[] = {28, 3, 12, 3, 28};
int x[] = {17, 10, 4, 10, 17};
int y[] = {17, 10, 4, 8, 16};
int z[] = {19, 21, 21, 25, 0};

int eos[] = {0, 1, 0, 0, 0};
int excl[] = {0, 29, 0, 0, 0};
int ques[] = {8, 19, 20, 8, 0};

void displayLine(int line)
{
  int myline; myline = line;
  if (myline>=16) {digitalWrite(LED1, HIGH); myline-=16;} else {digitalWrite(LED1, LOW);}
  if (myline>=8) {digitalWrite(LED2, HIGH); myline-=8;} else {digitalWrite(LED2, LOW);}
  if (myline>=4) {digitalWrite(LED3, HIGH); myline-=4;} else {digitalWrite(LED3, LOW);}
  if (myline>=2) {digitalWrite(LED4, HIGH); myline-=2;} else {digitalWrite(LED4, LOW);}
  if (myline>=1) {digitalWrite(LED5, HIGH); myline-=1;} else {digitalWrite(LED5, LOW);}
  
}

void displayChar(char c)
{
  if (c == 'a'){for (int i = 0; i <5; i++){displayLine(a[i]);delay(delayTime);}displayLine(0);}
  if (c == 'b'){for (int i = 0; i <5; i++){displayLine(b[i]);delay(delayTime);}displayLine(0);}
  if (c == 'c'){for (int i = 0; i <5; i++){displayLine(c2[i]);delay(delayTime);}displayLine(0);}
  if (c == 'd'){for (int i = 0; i <5; i++){displayLine(d[i]);delay(delayTime);}displayLine(0);}
  if (c == 'e'){for (int i = 0; i <5; i++){displayLine(e[i]);delay(delayTime);}displayLine(0);}
  if (c == 'f'){for (int i = 0; i <5; i++){displayLine(f[i]);delay(delayTime);}displayLine(0);}
  if (c == 'g'){for (int i = 0; i <5; i++){displayLine(g[i]);delay(delayTime);}displayLine(0);}
  if (c == 'h'){for (int i = 0; i <5; i++){displayLine(h[i]);delay(delayTime);}displayLine(0);}
  if (c == 'i'){for (int it = 0; it <5; it++){displayLine(i[it]);delay(delayTime);}displayLine(0);}
  if (c == 'j'){for (int i = 0; i <5; i++){displayLine(j[i]);delay(delayTime);}displayLine(0);}
  if (c == 'k'){for (int i = 0; i <5; i++){displayLine(k[i]);delay(delayTime);}displayLine(0);}
  if (c == 'l'){for (int i = 0; i <5; i++){displayLine(l[i]);delay(delayTime);}displayLine(0);}
  if (c == 'm'){for (int i = 0; i <5; i++){displayLine(m[i]);delay(delayTime);}displayLine(0);}
  if (c == 'n'){for (int i = 0; i <5; i++){displayLine(n[i]);delay(delayTime);}displayLine(0);}
  if (c == 'o'){for (int i = 0; i <5; i++){displayLine(o[i]);delay(delayTime);}displayLine(0);}
  if (c == 'p'){for (int i = 0; i <5; i++){displayLine(p[i]);delay(delayTime);}displayLine(0);}
  if (c == 'q'){for (int i = 0; i <5; i++){displayLine(q[i]);delay(delayTime);}displayLine(0);}
  if (c == 'r'){for (int i = 0; i <5; i++){displayLine(r[i]);delay(delayTime);}displayLine(0);}
  if (c == 's'){for (int i = 0; i <5; i++){displayLine(s[i]);delay(delayTime);}displayLine(0);}
  if (c == 't'){for (int i = 0; i <5; i++){displayLine(t[i]);delay(delayTime);}displayLine(0);}
  if (c == 'u'){for (int i = 0; i <5; i++){displayLine(u[i]);delay(delayTime);}displayLine(0);}
  if (c == 'v'){for (int i = 0; i <5; i++){displayLine(v[i]);delay(delayTime);}displayLine(0);}
  if (c == 'w'){for (int i = 0; i <5; i++){displayLine(w[i]);delay(delayTime);}displayLine(0);}
  if (c == 'x'){for (int i = 0; i <5; i++){displayLine(x[i]);delay(delayTime);}displayLine(0);}
  if (c == 'y'){for (int i = 0; i <5; i++){displayLine(y[i]);delay(delayTime);}displayLine(0);}
  if (c == 'z'){for (int i = 0; i <5; i++){displayLine(z[i]);delay(delayTime);}displayLine(0);}
  if (c == '!'){for (int i = 0; i <5; i++){displayLine(excl[i]);delay(delayTime);}displayLine(0);}
  if (c == '?'){for (int i = 0; i <5; i++){displayLine(ques[i]);delay(delayTime);}displayLine(0);}
  if (c == '.'){for (int i = 0; i <5; i++){displayLine(eos[i]);delay(delayTime);}displayLine(0);}
  delay(charBreak);
}

void displayString(char* s)
{
  for (int i = 0; i<=strlen(s); i++)
  {
  displayChar(s[i]);
  }
}

void loop()
{
  displayString("hello");
}

I am trying to make a rgb led pov. So, instead of typing "hello", i will give "rhreglglbo", which will show HELLO on the led. Thats why I want to split the text as rh,re,gl,gl,bo.
I am trying to do this by changing the following line

if (c == 'a'){for (int i = 0; i <5; i++){displayLine(a[i]);delay(delayTime);}displayLine(0);}

to

if (c == 'ra'){for (int i = 0; i <5; i++){displayLine(ra[i]);delay(delayTime);}displayLine(0);}

I will make an array for ra[] and change displayLine() accordingly.

A char litteral in single quotes like 'a' is a single character. One character.

If you want to looks for things like ab and cd then you are needing to deal with very short strings, not chars.

A char litteral in single quotes like 'a' is a single character. One character

int x = 'ab';

is perfectly legal C (though not very portable), but note that it is an "int", not a "char".

I think this can be done in a single function, you just need to make sure you send the proper input data.

The first one is the color and the second is the char, so you can use a FOR loop to go through them all and put them where they need to go.

What are you using to display the strings with the different colors?

Added:

This works:

void setup()
{
  Serial.begin(115200);
  delay(1);
  DisplayString("rhreglglbo"); 
}

void loop(){ }

void DisplayString(char * STR)
{
  Serial.println(strlen(STR));
  for(byte i = 1; i < strlen(STR); i+=2)
  {
    switch(STR[i-1])
    {
      case 'r':
        Serial.print("255,0,0");
        break;
      case 'g':
        Serial.print("0,255,0");
        break;
      case 'b':
        Serial.print("0,0,255");
        break;
    }
   Serial.print(" ");
   Serial.println(STR[i]); 
  }
}
// You need to have the size here,

It's a string - what's wrong with "strlen" ?

Your correct, I haven't had my coffee yet, my mistake.

Code is fixed.

Edit:
I now have my coffee XD

Better function, looks for a proper string.

void DisplayString(char * STR)
{
  if(strlen(STR)%2 != 0)
    Serial.println("You are missing a color or string letter");
  
  else
  {  
    for(byte i = 1; i < strlen(STR); i+=2)
    {
      switch(STR[i-1])
      {
      case 'r':
        Serial.print("255,0,0");
        break;
      case 'g':
        Serial.print("0,255,0");
        break;
      case 'b':
        Serial.print("0,0,255");
        break;
      }
      Serial.print(" ");
      Serial.println(STR[i]); 
    }
  }
}

HazardsMind:
Better function, looks for a proper string.

void DisplayString(char * STR)

{
  if(strlen(STR)%2 != 0)
    Serial.println("You are missing a color or string letter");
 
  else
  { 
    for(byte i = 1; i < strlen(STR); i+=2)
    {
      switch(STR[i-1])
      {
      case 'r':
        Serial.print("255,0,0");
        break;
      case 'g':
        Serial.print("0,255,0");
        break;
      case 'b':
        Serial.print("0,0,255");
        break;
      }
      Serial.print(" ");
      Serial.println(STR[i]);
    }
  }
}

Thank you, but I want the displayChar() to get "ra" as the input, so I can use it in the following code.

void displayChar(char c)
{
if (c == 'ra'){for (int i = 0; i <5; i++){displayLine(ra[i]);delay(delayTime);}displayLine(0);}
}

but I want the displayChar() to get "ra" as the input, so I can use it in the following code

That code cannot work.
Get over it..

You still haven't said what your using to show the strings.

but I want the displayChar() to get "ra" as the input, so I can use it in the following code.

What is wrong with what I gave you, it will do exactly what you want, but you just need to fill in the rest to work with your mystery display.

Hell, you can even send in just "rH" and it will display a H

HazardsMind:
You still haven't said what your using to show the strings.

I am trying to make a POV display with 5 RGB leds. I have posted the code for pov display using single color leds in reply #6.

int a[] = {1, 6, 26, 6, 1};
int b[] = {31, 21, 21, 10, 0};
int c2[] = {14, 17, 17, 10, 0};
int d[] = {31, 17, 17, 14, 0};
int e[] = {31, 21, 21, 17, 0};
int f[] = {31, 20, 20, 16, 0};
int g[] = {14, 17, 19, 10, 0};
int h[] = {31, 4, 4, 4, 31};
int i[] = {0, 17, 31, 17, 0};
int j[] = {0, 17, 30, 16, 0};
int k[] = {31, 4, 10, 17, 0};
int l[] = {31, 1, 1, 1, 0};
int m[] = {31, 12, 3, 12, 31};
int n[] = {31, 12, 3, 31, 0};
int o[] = {14, 17, 17, 14, 0};
int p[] = {31, 20, 20, 8, 0};
int q[] = {14, 17, 19, 14, 2};
int r[] = {31, 20, 22, 9, 0};
int s[] = {8, 21, 21, 2, 0};
int t[] = {16, 16, 31, 16, 16};
int u[] = {30, 1, 1, 30, 0};
int v[] = {24, 6, 1, 6, 24};
int w[] = {28, 3, 12, 3, 28};
int x[] = {17, 10, 4, 10, 17};
int y[] = {17, 10, 4, 8, 16};
int z[] = {19, 21, 21, 25, 0};

int eos[] = {0, 1, 0, 0, 0};
int excl[] = {0, 29, 0, 0, 0};
int ques[] = {8, 19, 20, 8, 0};

Chuck away > 50% of your precious RAM?
Bad idea.

I agree,

  1. There is no negative value and it does not go over 255, so you can just use type byte.
  2. put them into PROGMEM and structure it like a font file. Attached is a sample font file. It is made for a TFT display, so each char needs to be big enough to see on screen, 8x12 pixels. Yours will be roughly the same.

TRONFont.c (6.65 KB)

AWOL:

int a[] = {1, 6, 26, 6, 1};

int b[] = {31, 21, 21, 10, 0};
int c2[] = {14, 17, 17, 10, 0};
int d[] = {31, 17, 17, 14, 0};
int e[] = {31, 21, 21, 17, 0};
int f[] = {31, 20, 20, 16, 0};
int g[] = {14, 17, 19, 10, 0};
int h[] = {31, 4, 4, 4, 31};
int i[] = {0, 17, 31, 17, 0};
int j[] = {0, 17, 30, 16, 0};
int k[] = {31, 4, 10, 17, 0};
int l[] = {31, 1, 1, 1, 0};
int m[] = {31, 12, 3, 12, 31};
int n[] = {31, 12, 3, 31, 0};
int o[] = {14, 17, 17, 14, 0};
int p[] = {31, 20, 20, 8, 0};
int q[] = {14, 17, 19, 14, 2};
int r[] = {31, 20, 22, 9, 0};
int s[] = {8, 21, 21, 2, 0};
int t[] = {16, 16, 31, 16, 16};
int u[] = {30, 1, 1, 30, 0};
int v[] = {24, 6, 1, 6, 24};
int w[] = {28, 3, 12, 3, 28};
int x[] = {17, 10, 4, 10, 17};
int y[] = {17, 10, 4, 8, 16};
int z[] = {19, 21, 21, 25, 0};

int eos[] = {0, 1, 0, 0, 0};
int excl[] = {0, 29, 0, 0, 0};
int ques[] = {8, 19, 20, 8, 0};



Chuck away > 50% of your precious RAM? 
Bad idea.

HazardsMind:
I agree,

  1. There is no negative value and it does not go over 255, so you can just use type byte.
  2. put them into PROGMEM and structure it like a font file. Attached is a sample font file. It is made for a TFT display, so each char needs to be big enough to see on screen, 8x12 pixels. Yours will be roughly the same.

Thanks for your helps.
I will try using PORGMEM.