I am trying to create a timer that changes the color of the number as it counts down. To do this i use random(). But every time I put in random() somewhere in the code the display starts drawing more stuff than just the numbers on the screen and without random() it works as normal.
Interesting.
Sounds like a coding error, probably an out-of-bounds array access. When you turn Compiler Warnings up to "All", did you get ANY warnings when you compiled your code? The compiler is usually good about pointing out such potential mistakes.
The compiler is not pointing out any out-of-bound array access. I tried Serial. output on the values I tried to randomize. And in the Serial monitor I get the correct numbers and RGB values that are being sent to the display. It also does not matter if I try to randomize the values being sent to the display, if there is a random() being executed somewhere in the code then the display will start drawing things next to the numbers that I try to show.
That IS odd. If you want any help with figuring it out, please post your entire sketch, or at least a working sketch that demonstrates the problem.
Have you tried checking for updates to your libraries?
Tools -> Manage Libraries...
Select "Updatable" from the Type menu.
#include <TFT.h>
#include <SPI.h>
#define CS 10
#define DC 9
#define RESET 8
TFT myScreen = TFT(CS, DC, RESET);
int CLK = 4;
int DT = 3;
int button = 2;
int currentStateCLK;
int currentStateDT;
int previousStateCLK;
int m1 = 5;
int m2 = 35;
int s1 = 95;
int s2 = 125;
int amountOfColors = 10;
int background[3] = {7, 12, 18};
int textColor[3] = {255, 255, 255};
char minute[2] = {'0','0'};
char second[2] = {'0','0'};
char bar = ':';
char counter[6];
char printChar[1] = {'0'};
bool countdown = false;
bool done = false;
void setup(){
randomSeed(analogRead(5));
pinMode (button,INPUT);
pinMode (DT,INPUT);
pinMode (CLK,INPUT);
Serial.begin (9600);
myScreen.begin();
//Sets the start value on screen to 00:00
myScreen.background(background[2], background[1], background[0]); // clear the screen
myScreen.setTextSize(5);
color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.text(":",65,50);
color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.text("0",s1,50);
color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.text("0",s2,50);
color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.text("0",m1,50);
color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.text("0",m2,50);
previousStateCLK = digitalRead(CLK);
}
//Randomizes color
int color_randomiser()
{
textColor[0] = int(random(0,255));
textColor[1] = int(random(0,255));
textColor[2] = int(random(0,255));
}
//Counts down seconds like a timer on the display
void counterdown(bool countDownBool = false){
String temp = "";
if(second[1] == '0')
{
if(second[0] == '0')
{
if(minute[1] == '0')
{
if(minute[0] == '0')
{
if(countDownBool == true)
{
//KLAAAAAR
done = true;
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
printChar[0] = minute[0];
myScreen.text(printChar,m1,50);
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
myScreen.text("00:00",5,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.setTextSize(2);
myScreen.text("KLAAAAAAR",15,50);
}
}else{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
printChar[0] = minute[0];
myScreen.text(printChar,m1,50);
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(minute[0]).toInt() - 1);
minute[0] = temp[0];
printChar[0] = minute[0];
myScreen.text(printChar,m1,50);
minute[1] = '9';
myScreen.text("9",m2,50);
second[0] = '5';
myScreen.text("5",s1,50);
second[1] = '9';
myScreen.text("9",s2,50);
}
}else{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(minute[1]).toInt() - 1);
minute[1] = temp[0];
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
second[0] = '5';
myScreen.text("5",s1,50);
second[1] = '9';
myScreen.text("9",s2,50);
}
}else{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(second[0]).toInt() - 1);
second[0] = temp[0];
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
second[1] = '9';
myScreen.text("9",s2,50);
}
}else
{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
//color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(second[1]).toInt() - 1);
second[1] = temp[0];
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
}
}
//Counts up to set the timer value
void counterup(){
String temp = "";
if(second[1] == '9')
{
if(second[0] == '5')
{
if(minute[1] == '9')
{
if(minute[0] == '9')
{
//KLAAAAAR
}else{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
printChar[0] = minute[0];
myScreen.text(printChar,m1,50);
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(minute[0]).toInt() + 1);
minute[0] = temp[0];
printChar[0] = minute[0];
myScreen.text(printChar,m1,50);
minute[1] = '0';
myScreen.text("0",m2,50);
second[0] = '0';
myScreen.text("0",s1,50);
second[1] = '0';
myScreen.text("0",s2,50);
}
}else{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(minute[1]).toInt() + 1);
minute[1] = temp[0];
printChar[0] = minute[1];
myScreen.text(printChar,m2,50);
second[0] = '0';
myScreen.text("0",s1,50);
second[1] = '0';
myScreen.text("0",s2,50);
}
}else{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(second[0]).toInt() + 1);
second[0] = temp[0];
printChar[0] = second[0];
myScreen.text(printChar,s1,50);
second[1] = '0';
myScreen.text("0",s2,50);
}
}else
{
myScreen.stroke(background[2], background[1], background[0]);
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
//color_randomiser();
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
temp = (String(second[1]).toInt() + 1);
second[1] = temp[0];
printChar[0] = second[1];
myScreen.text(printChar,s2,50);
}
}
void loop(){
printChar[0] = '0';
String screentext = "";
int A = digitalRead(button);
//Check if button is pressed and if the counter should start or enter change time mode
if(A==0)
{
if(done == true && countdown == true)
{
done = false;
myScreen.stroke(background[2], background[1], background[0]);
myScreen.text("KLAAAAAAR",15,50);
myScreen.stroke(textColor[2], textColor[1], textColor[0]);
myScreen.setTextSize(5);
myScreen.text("00:00",5,50);
}
if (countdown == true)
{
countdown = false;
while(A == 0)
{
A = digitalRead(2);
}
delay(500);
}else//Ser till så att den inte startar och stoppar när man försöker stoppa
{
countdown = true;
double startTimer = millis();
while(A == 0)
{
A = digitalRead(2);
}
delay(500);
}
}
if(countdown == false && done == false)
{
currentStateCLK = digitalRead(CLK);
currentStateDT = digitalRead(DT);
if(currentStateCLK != previousStateCLK){
if(currentStateDT == currentStateCLK){
//gå ner
counterdown();
delay(100);
}else{
//gå upp
counterup();
delay(100);
}
}
}
if(countdown == true && done == false)
{
counterdown(true);
delay(1000);
}
}
//Fixa så att det syns att man måste trycka när man är klar
An image of the project all wired up would be nice.
It might work a bit better if the I2C line was not used, randomSeed(analogRead(3));
?
I think this is your problem. You are passing 'printChar' to functions that expect a string but your 1-character array doesn't have room for a terminator. If you are lucky, the byte in memory after your array contains a 0 which acts as a terminator. If you aren't lucky, whatever data is in memory after the array gets printed as part of the 'string'.
Try changing it to:
char printChar[2] = {"0"};
That will make it a valid 1-character string.
That worked! But what is the reason it only happened when I used random() in my code even when that random() did not affect printChar?
The forum did not let me upload images.
Including 'random()' in the sketch caused the byte after printChar to not be zero. The same might have happened with any other change to your sketch: adding code, deleting code, re-arranging lines... You found one change that triggered the problem and stopped looking.
oh ok! did not know that. Thanks for the help and the explanation!!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.