two timers based on fixed starting time and interval, - but unexpected duration of the interval
in
Programming Questions
•
5 months ago
Hi,
I looked at a lot of timer solutions in the forum, but they don't seem to 'fit' my problem. Perhaps you can help?
I'm trying to get a sketch working where the ball starts moving horizontally at a given digital clock-time - say 9.00 ( but for the purpose of the sketch, I set it only to seconds) pauses for a few seconds, than drops vertically.
It seems to work, but I notice that the DISPLAY_DURATION is shorter than the expected 2 seconds. Also I'm not sure about the function of interval here. It does influence, but in an odd way.
What's wrong and how can it be solved?
many thanks for your time and thought
cheers
nanette
Clock clock;
//---------------------------
void setup() {
size(200, 200);
smooth();
clock = new Clock ();
}
//---------------------------
void draw () {
background (255);
clock.display ();
clock.timeDisplay();
clock.moveBall();
}
//---------------------------
//---------------------------
class Clock {
int hour;
int minute;
int second;
int x, y;
int speedX= 2;
int speedY= 2;
int startTime; // The (last) time when the mouse have been displayed
int DISPLAY_DURATION = 2000; // in milliseconds = 1s
float interval=1000;
//---------------------------
Clock () {
startTime= 0;
}
//---------------------------
void display () {
hour = hour(); //Get the current time
minute = minute();
second = second();
hour = hour % 24; //Wrap the hour around to conform with 12-hour time
if (hour == 0) hour = 24; //Make zero-o-clock twelve-o-clock
backgroundChange ();
}
//---------------------------
void timeDisplay () {
if (second >=0 && second <30) {
fill (0);
textAlign (CENTER, BOTTOM);
textSize (15);
text ("Current Time", width/2, height/2);
textAlign (CENTER, TOP);
text (hour + ":" + minute + ":" + second + " ", width/2, height/2);
}
else
{
fill (255);
textAlign (CENTER, BOTTOM);
textSize (15);
text ("Current Time", width/2, height/2);
textAlign (CENTER, TOP);
text (hour + ":" + minute + ":" + second + " ", width/2, height/2);
}
}
//---------------------------
void backgroundChange () { // at a given time color change happens
fill (0, (second*4.25));
noStroke();
rect (20, 20, width-40, height-40);
}
//---------------------------
void moveBall()// at a stated second the ball starts moving to x=100 then stops
{
if (( second >= 10)&& (x<=100)) {
speedX=2;
x= x+speedX;
y=20;
fill (150);
ellipse ( x, y, 20, 20); // ball
}
else
{
speedX=0;
fill (0);
ellipse ( x, y, 20, 20); // ball
}
if (x>=100) { // I want it to stop for 3 seconds
startTime= 0;
if (millis() - startTime > DISPLAY_DURATION)
{
fill (0);
ellipse ( x, y, 20, 20); // ball
speedY=2;
y=y+speedY;
}
}
}
}// final bracket
I looked at a lot of timer solutions in the forum, but they don't seem to 'fit' my problem. Perhaps you can help?
I'm trying to get a sketch working where the ball starts moving horizontally at a given digital clock-time - say 9.00 ( but for the purpose of the sketch, I set it only to seconds) pauses for a few seconds, than drops vertically.
It seems to work, but I notice that the DISPLAY_DURATION is shorter than the expected 2 seconds. Also I'm not sure about the function of interval here. It does influence, but in an odd way.
What's wrong and how can it be solved?
many thanks for your time and thought
cheers
nanette
Clock clock;
//---------------------------
void setup() {
size(200, 200);
smooth();
clock = new Clock ();
}
//---------------------------
void draw () {
background (255);
clock.display ();
clock.timeDisplay();
clock.moveBall();
}
//---------------------------
//---------------------------
class Clock {
int hour;
int minute;
int second;
int x, y;
int speedX= 2;
int speedY= 2;
int startTime; // The (last) time when the mouse have been displayed
int DISPLAY_DURATION = 2000; // in milliseconds = 1s
float interval=1000;
//---------------------------
Clock () {
startTime= 0;
}
//---------------------------
void display () {
hour = hour(); //Get the current time
minute = minute();
second = second();
hour = hour % 24; //Wrap the hour around to conform with 12-hour time
if (hour == 0) hour = 24; //Make zero-o-clock twelve-o-clock
backgroundChange ();
}
//---------------------------
void timeDisplay () {
if (second >=0 && second <30) {
fill (0);
textAlign (CENTER, BOTTOM);
textSize (15);
text ("Current Time", width/2, height/2);
textAlign (CENTER, TOP);
text (hour + ":" + minute + ":" + second + " ", width/2, height/2);
}
else
{
fill (255);
textAlign (CENTER, BOTTOM);
textSize (15);
text ("Current Time", width/2, height/2);
textAlign (CENTER, TOP);
text (hour + ":" + minute + ":" + second + " ", width/2, height/2);
}
}
//---------------------------
void backgroundChange () { // at a given time color change happens
fill (0, (second*4.25));
noStroke();
rect (20, 20, width-40, height-40);
}
//---------------------------
void moveBall()// at a stated second the ball starts moving to x=100 then stops
{
if (( second >= 10)&& (x<=100)) {
speedX=2;
x= x+speedX;
y=20;
fill (150);
ellipse ( x, y, 20, 20); // ball
}
else
{
speedX=0;
fill (0);
ellipse ( x, y, 20, 20); // ball
}
if (x>=100) { // I want it to stop for 3 seconds
startTime= 0;
if (millis() - startTime > DISPLAY_DURATION)
{
fill (0);
ellipse ( x, y, 20, 20); // ball
speedY=2;
y=y+speedY;
}
}
}
}// final bracket
1