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