LEDControl.h scrolling program

I have basically managed this : (This is just the scroll part, for the whole code check the attachment )

void scroll(char x,int cur,int last){
     byte bHelper;
     int  iHelper=33;
     int  n=5;
          for(cur;cur<last;cur++)
              for(int i=0;i<n;i++)
               { if(x==' ')n=3;
                 placeholder[iHelper]=make(x,i);
                 iHelper++;
                 return;
                }
                n = sizeof(placeholder) / sizeof(placeholder[0]);
                bHelper=placeholder[0];
          for(int i=0;i<n;i++)
             {   
              for(int i=0;i<2;i++)
                 lc.setColumn(1,i,placeholder[i]);
              for(int i=2;i<10;i++)
                 lc.setColumn(2,i-2,placeholder[i]);
              for(int i=10;i<18;i++)
                 lc.setColumn(3,i-10,placeholder[i]);
                 delay(50);
              
              while(i+1!=n)
              placeholder[i]=placeholder[i+1];
              placeholder[n]=bHelper;
             }
  
      
}

void loop() { 
char s[20]="Aman Dwivedi";//scrollable name through device 1,2 and 3 ...
  for(int i=0;i<20;i++)
     scroll(s[i],i,21);}

The problem is that it doesn't work .. It compiles without any errors (I might be making a simple error.)
Can you people check it out ?

(LED matrix has 4 devices attached.)

I appreciate every help.

test.ino (7.04 KB)

    for (int i = 0; i < n; i++)
    {
      if (x == ' ')n = 3;
      placeholder[iHelper] = make(x, i);
      iHelper++;
      return;
    }

What exactly should happen if x == ' ' ?
What exactly will happen whatever the value of x ?

If you see the sketch , when x=' ' , only three rows should be left blank , hence that IF(x==' ') n=3;
so that only 0,1,2 with B00000000 are stored in the placeholder .

The value of x is the character , the make() function takes the character and column number and then 'spits' out it's data . Every character has 5 rows (some exceptions are there eg-' ' (space) )
and that data is then stored into the array which will realign the characters to form a sentence in the placeholder array . Then this placeholder is used to 'scroll' the text .

Guys please help me out , I need to finish it by today..

The following flaw (?) in the below: it will execute exactly zero times and continue in your scroll() function or one time and next return to the function that calls scroll(). Is that the intention?

  for (cur; cur < last; cur++)

for (int i = 0; i < n; i++)
    {
      if (x == ' ')n = 3;
      placeholder[iHelper] = make(x, i);
      iHelper++;
      return;
    }

Only if n equals zero that inner loop will not cause a return. Or if cur is greater than or equal to last.

sterretje:
The following flaw (?) in the below: it will execute exactly zero times and continue in your scroll() function or one time and next return to the function that calls scroll(). Is that the intention?Only if n equals zero that inner loop will not cause a return. Or if cur is greater than or equal to last.

Well , if you see carefully n=5 is declared. So that isn't the problem. And the first loop is meant to execute only once . Like , if cur<last , then it will go to the inner for loop at whose end is a return to the function that calls scroll(). To not execute the inner for loop the first for loop ,

for(int i=0;i<20;i++)
scroll(s*,i,21);}*

which sends the 'cur' and 'last' value will have a certain value .
Once the outer loop isn't able to execute , the rest of the program executes . According to me , that shouldn't be an error .
On compiling I got no errors , so , the problem maybe my logic , and that's why I wanted you guys to check ..
I am frustrated by this piece of program .
If you are able to find some solution , it would be a great help.
Thanks in advance. (Side-note : You can also check out the whole attached code.)

    for (int i = 0; i < n; i++)
    {
      if (x == ' ')n = 3;
      placeholder[iHelper] = make(x, i);
      iHelper++;
      return;
    }

How many times will this loop iterate? If you think that it will iterate more than once, you are wrong. Using a for loop when you know it will iterate 0 or 1 times is pointless. Stop doing that.

"It doesn't work" is too lame for words. The code does something. It would help if you said what it actually does.

You expect it to do something different. It would help if you said what you actually expect it to do.

PaulS:

    for (int i = 0; i < n; i++)

{
      if (x == ' ')n = 3;
      placeholder[iHelper] = make(x, i);
      iHelper++;
      return;
    }



How many times will this loop iterate? If you think that it will iterate more than once, you are wrong. Using a for loop when you know it will iterate 0 or 1 times is pointless. Stop doing that.

This will work 5 times, n has been declared before and it is 5 times... BUT if the character is ' ' (space) , it will change n to 3 times , ie. then it will execute three times.

"It doesn't work" is too lame for words. The code does something. It would help if you said what it actually does.

Well, sorry for being lame. The thing is that the code has to scroll a sentence , and it doesn't do that , i.e it doesn't work...

You expect it to do something different. It would help if you said what you actually expect it to do.

I expect it to stole the byte values into an place holder array and then scroll the text..

This will work 5 times, n has been declared before and it is 5 times.

The return statement causes the function containing that for loop to end NOW. The for loop will iterate ONCE, at most.

Exactly what I was hinting at in reply #1
Perhaps a more explicit hint would have been in order.

Holy ,

How did I miss that...
I am real sorry for the inconvenience.. stupid mistake..

So , I should use goto instead?

So , I should use goto instead?

Absolutely not.

What are you trying to accomplish with prematurely exiting the for loop?

All those "i"s sure get confusing. Plenty of other letters to choose from.

char s[20]="Aman Dwivedi"
I'm not counting 20 characters

char s[20]="Aman Dwivedi"
I'm not counting 20 characters

So? The 20 defines the maximum size of the array. It can be initialized with fewer initializers than that.

So? It seemed like he was trying to space out the scrolled text since he's got for loops running 0-20 and I was pointing it out to adjust all appropriate code to his actual message length.

INTP:
So? It seemed like he was trying to space out the scrolled text since he's got for loops running 0-20 and I was pointing it out to adjust all appropriate code to his actual message length.

This string should be able to handle the name of 20 characters including
' '(space), this part just says the maximum no of characters possible. The real end of the no. of characters is given using sizeof();.

PaulS:
What are you trying to accomplish with prematurely exiting the for loop?

I want the function to first store the data into the placeholder , if it reaches the limit of 20 characters , it should stop storing the data and start 'scrolling' it according to the program. Instead , I could also create another function which stores the data , and another function which scrolls the data , would that be better ?

would that be better ?

That depends on how you name your function(s). If the single function name does not communicate the fact that it does two not-closely-related things, then two functions would be better.

The fact that one function would need to do tow not-closely-related things should be a clue that one function is not the best approach.

tgr8db:
How did I miss that...

By not listening to what people say :smiley:

Nevermind guys, I have managed to solve it ...

Here is the code for others who want to scroll their stuff too... :stuck_out_tongue: :stuck_out_tongue: :stuck_out_tongue:

#include <LedControl.h>
#include <ESP8266WiFi.h>

int din = 2, clk = 13, cs = 15;
LedControl lc = LedControl(din, clk, cs, 4);

int devices = lc.getDeviceCount();
char sex = 'm'; //m for male , f for female (To add Mr. OR Mrs. before the name...)
char thename[] = "Aman Dwivedi";
int y = sizeof(thename) / sizeof(byte); 
byte plch[92];  // After changing the name , run the program once, use the value of r from the serial monitor (approx. r-7 use trial and error method) and use it as the size of this array...
int r;

const char isSSID[] = "Network";
const char isPass[] = "password";
WiFiServer server(80);

/************************************/
/*  All the letters and numbers...  */
/************************************/
const byte ch[315] =
{
  B01111100, B10100010, B10010010, B10001010, B01111100, /*0*/
  B00100010, B01000010, B11111110, B00000010, B00000010, /*1*/
  B01001110, B10001010, B10001010, B10001010, B01111010, /*2*/
  B10000100, B10010010, B10110010, B11010010, B10001100, /*3*/
  B00011000, B00101000, B01001000, B11111110, B00001000, /*4*/
  B11110100, B10010010, B10010010, B10010010, B10001100, /*5*/
  B01111100, B10010010, B10010010, B10010010, B01001100, /*6*/
  B10000000, B10001110, B10010000, B10100000, B11000000, /*7*/
  B01101100, B10010010, B10010010, B10010010, B01101100, /*8*/
  B01100100, B10010010, B10010010, B10010010, B01111100, /*9*/
  B01111110, B10001000, B10001000, B10001000, B01111110, /*A*/
  B11111110, B10000010, B10000010, B10010010, B01101100, /*B*/
  B01111100, B10000010, B10000010, B10000010, B01000100, /*C*/
  B10000010, B11111110, B10000010, B10000010, B01111100, /*D*/
  B11111110, B10010010, B10010010, B10000010, B10000010, /*E*/
  B11111110, B10010000, B10010000, B10010000, B10010000, /*F*/
  B11111110, B10000010, B10001010, B10001010, B10001110, /*G*/
  B11111110, B00010000, B00010000, B00010000, B11111110, /*H*/
  B10000010, B10000010, B11111110, B10000010, B10000010, /*I*/
  B10001110, B10000010, B11111110, B10000000, B10000000, /*J*/
  B11111110, B00010000, B00101000, B01000100, B10000010, /*K*/
  B11111110, B00000010, B00000010, B00000010, B00000010, /*L*/
  B11111110, B01000000, B00100000, B01000000, B11111110, /*M*/
  B11111110, B01000000, B00100000, B00010000, B11111110, /*N*/
  B01111100, B10000010, B10000010, B10000010, B01111100, /*O*/
  B01111110, B10010000, B10010000, B10010000, B01100000, /*P*/
  B01111100, B10000010, B10000010, B10000010, B01111101, /*Q*/
  B11111110, B10010000, B10001000, B10010100, B01100010, /*R*/
  B01100100, B10010010, B10010010, B10010010, B01001100, /*S*/
  B10000000, B10000000, B11111110, B10000000, B10000000, /*T*/
  B11111100, B00000010, B00000010, B00000010, B11111100, /*U*/
  B11111000, B00000100, B00000010, B00000100, B11111000, /*V*/
  B11111110, B00000100, B00001000, B00000100, B11111110, /*W*/
  B11100110, B00101000, B00010000, B00101000, B11100110, /*X*/
  B11100100, B00010010, B00010010, B00010010, B11111100, /*Y*/
  B11000110, B10001010, B10010010, B10100010, B11000110, /*Z*/
  B00101110, B00101010, B00111110, B00000010, B00000000, /*a*/
  B11111110, B00010010, B00010010, B00001100, B00000000, /*b*/
  B00111110, B00100010, B00100010, B00100010, B00000000, /*c*/
  B00011110, B00010010, B00010010, B11111110, B00000000, /*d*/
  B00111110, B00101010, B00101010, B00111010, B00000000, /*e*/
  B00010000, B11111110, B10010000, B10010000, B11000000, /*f*/
  B00011010, B00101001, B00101001, B00011110, B00100000, /*g*/
  B11111110, B00010000, B00010000, B00001110, B00000000, /*h*/
  B00000010, B01011110, B00000010, B00000000, B00000000, /*i*/
  B00000110, B00000001, B01011110, B00000000, B00000000, /*j*/
  B11111110, B00001000, B00010100, B00100010, B00000000, /*k*/
  B11111110, B00000000, B00000000, B00000000, B00000000, /*l*/
  B00111110, B00010000, B00011000, B00010000, B00011110, /*m*/
  B00100000, B00111110, B00010000, B00011110, B00000000, /*n*/
  B00011100, B00100010, B00100010, B00011100, B00000000, /*o*/
  B00111111, B00010100, B00011100, B00000000, B00000000, /*p*/
  B00011100, B00010100, B00111111, B00000001, B00000000, /*q*/
  B00111110, B00010000, B00100000, B00100000, B00000000, /*r*/
  B00011010, B00101010, B00101010, B00101100, B00000000, /*s*/
  B00100000, B11111100, B00100010, B00000100, B00000000, /*t*/
  B00111100, B00000010, B00000010, B00111100, B00000010, /*u*/
  B00111000, B00000100, B00000010, B00000100, B00111000, /*v*/
  B00111100, B00000010, B00000100, B00000010, B00111100, /*w*/
  B00100010, B00010100, B00001000, B00010100, B00100010, /*x*/
  B00111001, B00001001, B00001001, B00111111, B00000000, /*y*/
  B00100110, B00101010, B00110010, B00100010, B00000000, /*z*/
  B00000000, B00000000, B00000000,                       /* */
  B00000010                                              /*.*/
};
const byte divider   = {B11111111};

int initloc(char x) {   //works
  int location;
  if (x >= '0' && x <= '9')  location = 5 * (x - 48)       ;
  if (x >= 'A' && x <= 'Z')  location = 5 * (x - 65) + 50  ;
  if (x >= 'a' && x <= 'z')  location = 5 * (x - 97) + 180 ;
  if (x == ' ')              location = 310                ;
  if (x == '.')              location = 313                ;
  return location;
}
byte make(char c, int i) { //works
  byte x;
  int loc = initloc(c);
  x = ch[loc + i];
  return x;
}

void stationary(char x, char y) {  //works
  for (int i = 2; i < 4; i++)
    lc.setColumn(1, i, divider);
  lc.setColumn(1, 4, 0);
  byte pholder[12];
  for (int i = 0; i < 5; i++)
    pholder[i] = make(x, i);
  pholder[5] = B00000000;
  for (int i = 0; i < 5; i++)
    pholder[i + 6] = make(y, i);
  for (int i = 5; i < 8; i++)
    lc.setColumn(1, i, pholder[i - 5]);
  for (int i = 0; i < 8; i++)
    lc.setColumn(0, i, pholder[i + 3]);

}
void onehelp(char curchar , int i) {
  int w;
  for (w = 0; w < i; w++) {
    plch[w + r] = make(curchar, w);
  }
  plch[i + r] = B00000000;
  Serial.print(" ");
  Serial.print(plch[w + r]);
  Serial.println();
}
void makeplaceholder() {

  for (int x = 0; x < y; x++) {
    char curchar = thename[x];

    if (curchar == ' ') {
      onehelp(curchar, 2);
      r += 3;
    }
    else if (curchar == 'a' || curchar == 'b' || curchar == 'c' || curchar == 'd' || curchar == 'e' || curchar == 'h' || curchar == 'k' || curchar == 'n' || curchar == 'o' || curchar == 'q' || curchar == 'r' || curchar == 's' || curchar == 't' || curchar == 'y' || curchar == 'z') {
      onehelp(curchar, 5);
      r += 5;
    }
    else if (curchar == 'i' || curchar == 'j' || curchar == 'p') {
      onehelp(curchar, 5);
      r += 4;
    }
    else if (curchar == 'l') {
      onehelp(curchar, 5);
      r += 2;
    }
    else if (curchar != ' ' && curchar != '.') {
      onehelp(curchar, 5);
      r += 6;
    }
    else {
      onehelp(curchar, 1);
      r += 2;
    }
  }
  Serial.println();
  Serial.print("r is : ");
  Serial.print(r);
  Serial.println();
}
void scroll() {
  int w = sizeof(plch) / sizeof(byte);
  byte zhelp;
  zhelp = plch[0];

  for (int i = 0; i < 2; i++)
    lc.setColumn(1, i, plch[i + 16]);
  for (int j = 0; j < 8; j++)
    lc.setColumn(2, j, plch[j + 8]);
  for (int k = 0; k < 8; k++)
    lc.setColumn(3, k, plch[k]);

  delay(75);
  for (int q = 0; q < w; q++)
    plch[q] = plch[q + 1];
  plch[w] = zhelp;

}
void initHardware() {
  for (int i = 0 ; i < devices ; i++) {
    lc.shutdown(i, false);
    lc.setIntensity(i, 0);
    lc.clearDisplay(i)  ;
  }
  Serial.begin(115200);
  Serial.println();
  Serial.println("Started...");
  for (int l = 0; l < 18; l++)
    plch[l] = B00000000;
  byte secm[] = {B11111110, B01000000, B00100000, B01000000, B11111110, B00000000, B00111110, B00010000, B00100000, B00100000, B00000010, B00000000, B00000000, B00000000};
  byte secf[] = {B11111110, B01000000, B00100000, B01000000, B11111110, B00000000, B00111110, B00010000, B00100000, B00100000, B00000000, B00011010, B00101010, B00101010, B00101100, B00000000, B00000010, B00000000, B00000000, B00000000};
  if (sex == 'm') {
    for (int h = 0; h < 14; h++)
      plch[18 + h] = secm[h];
    r = 32;
  }
  else if (sex == 'f') {
    for (int h = 0; h < 20; h++)
      plch[18 + h] = secf[h];
    r = 38;
  }
  makeplaceholder();
  
}
void increment() {

}
void setup() {

  initHardware();

}

void loop() {
  scroll();
  stationary('0','0');
}