old text from LED Display doesn't clear

Hello. Sorry for the title of the post. But I don’t know what to put in the subject. :smiley:
I have this problem since my first multiplexing project with a control using an android application.
Please continue reading …

I have two projects:
My first project is a SCROLLING DISPLAY IN A LED MATRIX CONTROLLED BY AN ANDROID APPLICATION. I made that a year ago.
My current and 2nd project is a Android Controlled POV.

The problem occurs in these two projects. Say a default message in my led matrix display is: i love God (that is 10 characters) and I want to replace that string by a new one, say: hi (2 characters), the display would be, in reality: “hilove God”.

I tried many different ways in the program part. Such as, double clearing the string variable in the program, used a strcpy, replace the variable with a null, etc… but they didn’t help.

Also, I thought that the bug was in my android app. But no. I tried viewing the variable in a serial monitor and it is ok. it replaces the old string fine, but not in the display.

Here is my code for the POV that can be controlled serially: (I lost my first project’s code):

//============================================================

//============================================================

//CODE VERSION 2.0.3
// numframes is constant to 80
// framDuration is 210
// letterSpace 5
// circumference 90k
// 
// RESULTS:
//    BLUETOOTH OK!!!!
//    BLUETOOTH COLOR - ok but with green
//    rotation is quite fine, but fast if long text.
// DECISION:
//    -FINAL


#undef int() // fixes a bug with the stdio.h
#include <stdio.h> // gives access to function sprintf
#include <string.h>

// defining the alphabet
#include "font.h"

// get access to better timekeeping. ref: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1193623343
extern volatile unsigned long timer0_overflow_count;

// define the Arduino LED pins in use
const int LEDpins[] = {
   8,7,6,5,4,3,2};

 const char s[2] = "~"; //for cutting;
 String inData;
 char inData2[100]; // Allocate some space for the string
 

   char *token;
   char *token2;
   char *token3;
   char x1[100];
    char x2[100];
     char x3[100];
 
 String tryData = "";
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

// number of LEDs
const int charHeight = sizeof(LEDpins);
const int charWidth = 5;

// set up display parameters
const int numFrames = 80;	// number of frames per rotation - The width of the Letters (the larger the smaller the text)
unsigned long frameDuration = 210;	// time for each frame in microseconds - updated from rotation speed -SPACING, the larger the value, the larger the space.
const int letterSpace = 5;	// number of frames between letters
const int textOffset = 10;  // start distance from magnet in frames
unsigned long startTime = microSeconds();	// start time for wheel rotation in microseconds
unsigned long resetTime = 60000000;	// reset time for text in microseconds (10 sec)

// sensor setup
const int sensorPIN = 12;  // define the Arduino sensor pin
const int mean = 508;  // sensor at rest (no magnet)
const int sensitivity = 30;  // sensor sensitivity

// distance and speed variables (floats are avoided because they slow the Arduino down)
unsigned long numWheelRotations;  // the base from which all other data is calculated
const unsigned long wheelCircum = 90000;  // bike wheel 

// text string
char textString[100] = "WELCOME TO ACRLDS v2.0  x"; // declared large enough to hold a full string

void setup()
{
     pinMode(13, OUTPUT);

    pinMode(2, OUTPUT);
     pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
     pinMode(7 , OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(12, INPUT);

   
     Serial.begin(9600);

     analogWrite(10,0);
     //analogWrite(11,255);
    // analogWrite(9,255);
  // future version should get this value from EEPROM memory
  // store the value in m then (numWheelRotations = distance * 1000 / wheelCurcum)
  numWheelRotations = 0;  // for now just let it equal 0
}

void loop()
{
   // Serial.write(inData);
    
    if(Comp("blue")==0){
          analogWrite(9, 0);
    analogWrite(11, 255);
    analogWrite(10, 255);
    Serial.println("BLUE SELECTED");
    strcpy(textString, '\0');
    //delay(500);
    strcpy(textString, x2);


  }
  if(Comp("green")==0){
      analogWrite(10,0);
 analogWrite(11,255);
analogWrite(9,255);
   Serial.println("GREEN SELECTED");
    strcpy(textString, '\0');
    //delay(500);
   strcpy(textString, x2);
 

  }
   
    if(Comp("red")==0){
      analogWrite(10,255);
 analogWrite(11,0);
analogWrite(9,255); 
   Serial.println("RED SELECTED");
    strcpy(textString, '\0');
    //delay(500);
   strcpy(textString, x2);
 

  }
  
  // printing every letter of the textString
  for (int i=0; i<sizeof(textString); i++){
    printLetter(textString[i]);
    // space between letters
    pauseFrame(letterSpace);
    if(microSeconds() > resetTime){
      resetTime += 10000000; // add 10 seconds to it
      break;  // restart the text string
    }
  }
}

void printLetter(char ch)
{
  byte b;
  // make sure the character is within the alphabet bounds (defined by the font.h file)
  // if it's not, make it a blank character
  if (ch < 32 || ch > 126)
    ch = 32;
  // converts the ASCII number to the font index number
  ch -= 32;
  // step through each byte of the character array
  for (int i=0; i<charWidth; i++) {
    b = font[ch][i];
    // bit shift through the byte and output it to the pin
   for (int j=charHeight; j>=0; j--) {
      digitalWrite(LEDpins[j], !!(b & (1 << j)));
    }
    // space between columns
    pauseFrame(1);
  }
  //clear the LEDs
digitalWrite(2 , LOW);   // set the LED on
digitalWrite(3 , LOW);   // set the LED on
digitalWrite(4 ,LOW);   // set the LED on
digitalWrite(5 , LOW);   // set the LED on
digitalWrite(6 , LOW);   // set the LED on
    digitalWrite(7 , LOW);   // set the LED on
digitalWrite(8 , LOW);   // set the LED on
   // space between letters
  // delayMicroseconds(700);
}

void pauseFrame(int frames){
  unsigned long currentTime;
  int sensVal;  // variable to store the value coming from the sensor
  unsigned long endTime = microSeconds() + (frames * frameDuration);  // calculate the end time

  while(microSeconds() < endTime && microSeconds() < resetTime){
    // check the sensor
    sensVal = analogRead(sensorPIN);  // read the Hall Effect Sensor
   // Serial.println(sensVal);
    if (sensVal > (mean + sensitivity) || sensVal < (mean - sensitivity)) {
      currentTime = microSeconds();  // in microseconds
      if(currentTime < (startTime + (20 * frameDuration))){  // de-bounce the sensor reading
        startTime = currentTime;  // reset the start time
      } 
      else {
        resetTime = currentTime + (textOffset * frameDuration);  // set the sensor flag variable
        frameDuration = (currentTime - startTime) / numFrames;  // recalculate frameDuration
        if(frameDuration > 10000)
          frameDuration = 10000;  // limit the frame duration to a reasonable time
        numWheelRotations++;  // add 1 to the number of wheel rotations
        startTime = currentTime;  // reset the start time
      }
    }
  }
}


// get an accurate time reading. ref: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1193623343
unsigned long microSeconds(void)
{
  return ((timer0_overflow_count << 8) + TCNT0)*4;
}



 
char Comp(char* This) {
 
while(Serial.available()) {
      inChar = Serial.read();
      inData.concat(inChar);
  }
  
  strcpy(inData2, inData.c_str());
   token = strtok(inData2, s);
            strcpy(x1,token);

                token = strtok(NULL, s);
              strcpy(x2,token);

                  token = strtok(NULL, s);
                  strcpy(x3,token);

if (strcmp(x1,This)  == 0) {

Serial.println(x1);
Serial.println(x2);
Serial.println(x3);

   inData = "";
   inChar = '\0';
    return(0);
}
else {
    return(1);
}

}

By the way, I think posting the code won’t help :slight_smile: I already tried connecting the project (2nd) to my laptop and then change the text display via serial monitor. Still the same.

in the IDE you can press CTRL-T to auto format the code. Makes it far more readable, try it.

In general, you append a string containing as many spaces as the length of the display, to the new text.

You may then need to clip the length of the resulting string to the size of the display, or printing beyond the display length may not matter. If it does matter, you probably should make it a point to clip the length in all cases.

Paul__B:
In general, you append a string containing as many spaces as the length of the display, to the new text.

You may then need to clip the length of the resulting string to the size of the display, or printing beyond the display length may not matter. If it does matter, you probably should make it a point to clip the length in all cases.

thanks for your input sir. But I don't get what you are trying to say.
Perhaps my app should count first the length of the Textbox's text, then compare to the length of the default text. if the counted is less than the default, the app would add spaces to the new text to be equal to the length of the default text.

DEFAULT TEXT: i love God (10 characters).

CASE 1:
NEW TEXT: hello (5 characters)
less than 10 characters? YES --> hello[space][space][space][space][space]

CASE2:
NEW TEXT: hello world :slight_smile: (14 characters)
less than 10 characters? NO --> do nothing.

I think this would be the best solution to my problem, whether the connection between my android phone and the arduino's bluetooth shield disconnects or not, the solution still applies/will be effective. :slight_smile:
What do you think sir?

robtillaart:
in the IDE you can press CTRL-T to auto format the code. Makes it far more readable, try it.

thanks for this sir.

Yes, you most certainly can do that.

My suggestion was simply to add so many spaces to the end that you guarantee that it will run past the end of the display under all circumstances, and if necessary, truncate the string to a fixed value as an alternative to measuring the actual length and adding spaces.

Just different - alternative - approaches really.

Hi, to save that calculation, why not just load the display with a full line of blanks just before you write the new line.
This way you don't have to calculate string lengths and ad spaces.
That is what I do with LCD control. Or if available a clear screen type command.

Tom...... :slight_smile: