TFT LCD Screen Saver - Dancing Lines


#1

Ok, so I got bored one evening and decided to play around with the TFT LCD Screen to figure out how to use variables in the String parameters.

Here is what I came up with.

/*************************************************************************
* File Name          : ScreenSaver.ino
* Author             : Mike Dickey
* Version            : V 0.6.0
* Date               : 09-OCT-2015
* Parts required     : Me TFT
* Description        : Create a dancing line screen saver 
*                      for the MakeBlock TFT screen
**************************************************************************/
#include <SoftwareSerial.h>

// Constants - Stuff the program can not change
#define dlay 27
#define xMax 320 //change this depending on your screen size
#define yMax 240
#define vMax 19 // this should be an odd number
#define colorCount 6
const int C[colorCount] = {1, 4, 5, 2, 3, 6}; //red, yellow, cyan, green, blue, pink

// Variables - Stuff the program can change
String line[colorCount], blackOut[colorCount];
int X1, Y1, X2, Y2;
int vX1, vY1, vX2, vY2;
boolean onceThrough = false;

void setup(){
  Serial.begin(9600);
  Serial.print("CLS(0);");  // clear the screen with CLS(color) and resets DR to DR0
  Serial.println("CLS(0);");  // Not sure why I need to do this again, but one CLS simple does not work
  delay(dlay); // Change the delay to get a new randomSeed
  // initialize starting coordinates, and velocity 
  randomSeed(millis());
  X1 = random(xMax);
  X2 = random(xMax);
  Y1 = random(yMax);
  Y2 = random(yMax);
  changeV1();
  changeV2();
}

void loop(){
  for(int i = 0; i < colorCount; i++){
    if(onceThrough){
      //  black out the previous line with the current [i] value
      Serial.print(blackOut[i]); 
    }
    //  record the current line for erasing
    blackOut[i] = "PL(" + String(X1) + "," + String(Y1) + "," + String(X2) + "," + String(Y2) + ",0);";
    //  find the next position (X1, Y1, X2, Y2) of the line
    newPos();
    //  define the line string
    line[i] = "PL(" + String(X1) + "," + String(Y1) + "," + String(X2) + "," + String(Y2) + "," + String(C[i]) + ");"; 
    //print the line string
    Serial.println(line[i]);
    delay(dlay);
  }
  //need to have lines (and X, Y values) to black them out
  onceThrough = true; 
}

void changeV1(){
  vX1 = random(vMax) - ((vMax - 1) / 2);
  vY1 = random(vMax) - ((vMax - 1) / 2);
}

void changeV2(){
  vX2 = random(vMax) - ((vMax - 1) / 2);
  vY2 = random(vMax) - ((vMax - 1) / 2);
}

void newPos(){
  vX1 = borderBounce(X1, xMax, vX1, 1);
  X1 += vX1;
  vY1 = borderBounce(Y1, yMax, vY1, 1);
  Y1 += vY1;
  vX2 = borderBounce(X2, xMax, vX2, 2);
  X2 += vX2;
  vY2 = borderBounce(Y2, yMax, vY2, 2);
  Y2 += vY2;
}

int borderBounce(int Pos, int Mx, int Vel, int endBounce){
  int bounce = 0;
  if((Pos + Vel <= 0 && Vel < 0) || (Pos + Vel >= Mx && Vel > 0)){
    Vel = -Vel;
    bounce = endBounce;
  }
  if(bounce == 1){
    changeV1(); //get a new set of velocity values for end 1 of the line
  }
  if(bounce == 2){
    changeV2(); //get a new set of velocity values for end 2 of the line
  }
  return Vel;
}

Questions are welcome.

Mike


#2

Thanks for your post. I’v tried your program and it works perfectly.

However, I don’t know why it’s only working on the port 5 and not on the other such as : 3, 4 and 6!!
Can I have an explanation?

Manolo


#3

@manolo,

I have tried all of the listed ports, but as you have noticed, it will only work on port 5. According to the User Manual, it should work on ports 3, 4, 5, & 6 at 9600 baud, and will only work on port 5 at 115200 baud. I wish I could provide an explanation, but the information is not available to me.

Mike

Note: After doing some reading about the Arduino, I found that the you can comment out the

//#include <SoftwareSerial.h>

since it is not required for this particular sketch.


#4

I decided to play with this a little more and found that I could get it to work on the mBot as well.

/*************************************************************************
* File Name          : ScreenSaver.ino
* Author             : Mike Dickey
* Version            : V 0.8.0
* Date               : 09-OCT-2015
* Updated            : 19-FEB-2016
* Parts required     : Me TFT
* Description        : Create a dancing line screen saver 
*                      for the MakeBlock TFT screen
**************************************************************************/
#include <MeMCore.h>    //uncomment for mBot/MCore boards
//#include "MeOrion.h"  //uncomment for MeOrion boards
#include <SoftwareSerial.h>

MeSerial mySerial(PORT_4);  //should run on any Blue connector, PORT_4 is common to both boards

// Constants - Stuff the program can not change
#define dlay 27
#define xMax 320 //change this depending on your screen size
#define yMax 240
#define vMax 19 // this should be an odd number
#define colorCount 6
const int C[colorCount] = {1, 4, 5, 2, 3, 6}; //red, yellow, cyan, green, blue, pink

// Variables - Stuff the program can change
String line[colorCount], blackOut[colorCount];
int X1, Y1, X2, Y2;
int vX1, vY1, vX2, vY2;
boolean onceThrough = false;

void setup(){
  mySerial.begin(9600);   //keep this at 9600, other speed might not work
  mySerial.print("CLS(0);");  // clear the screen with CLS(color) and resets DR to DR0
  mySerial.println("CLS(0);");  // Not sure why I need to do this again, but one CLS simple does not work
  delay(dlay); // Change the delay to get a new randomSeed
  // initialize starting coordinates, and velocity 
  randomSeed(millis());
  X1 = random(xMax);
  X2 = random(xMax);
  Y1 = random(yMax);
  Y2 = random(yMax);
  changeV1();
  changeV2();
}

void loop(){
  for(int i = 0; i < colorCount; i++){
    if(onceThrough){
      //  black out the previous line with the current [i] value
      mySerial.print(blackOut[i]); 
    }
    //  record the current line for erasing
    blackOut[i] = "PL(" + String(X1) + "," + String(Y1) + "," + String(X2) + "," + String(Y2) + ",0);";
    //  find the next position (X1, Y1, X2, Y2) of the line
    newPos();
    //  define the line string
    line[i] = "PL(" + String(X1) + "," + String(Y1) + "," + String(X2) + "," + String(Y2) + "," + String(C[i]) + ");"; 
    //print the line string
    mySerial.println(line[i]);
    delay(dlay);
  }
  //need to have lines (and X, Y values) to black them out
  onceThrough = true; 
}

void changeV1(){
  vX1 = random(vMax) - ((vMax - 1) / 2);
  vY1 = random(vMax) - ((vMax - 1) / 2);
}

void changeV2(){
  vX2 = random(vMax) - ((vMax - 1) / 2);
  vY2 = random(vMax) - ((vMax - 1) / 2);
}

void newPos(){
  vX1 = borderBounce(X1, xMax, vX1, 1);
  X1 += vX1;
  vY1 = borderBounce(Y1, yMax, vY1, 1);
  Y1 += vY1;
  vX2 = borderBounce(X2, xMax, vX2, 2);
  X2 += vX2;
  vY2 = borderBounce(Y2, yMax, vY2, 2);
  Y2 += vY2;
}

int borderBounce(int Pos, int Mx, int Vel, int endBounce){
  int bounce = 0;
  if((Pos + Vel <= 0 && Vel < 0) || (Pos + Vel >= Mx && Vel > 0)){
    Vel = -Vel;
    bounce = endBounce;
  }
  if(bounce == 1){
    changeV1(); //get a new set of velocity values for end 1 of the line
  }
  if(bounce == 2){
    changeV2(); //get a new set of velocity values for end 2 of the line
  }
  return Vel;
}

I have added the board specific includes, and a few other lines at the top of the code. You should be able to use any ‘Blue’ PORT on either board.

For a side project, I thought I would try this with mBlocks, I will post the results here, if I succeed.

Mike