# Timer problem with the value

I have problem with my timer ,this homework is connect with arduino. My problem is when val==4,the value of sec won’t go properly, it just decrease maybe ten or more at a time,and I want it to decrease one second at a time. Can someone help me with this problem. Thanks a lot!

``````import ddf.minim.*;
import ddf.minim.effects.*;
import processing.serial.*;
Minim minim;
Serial serial;
int hour=0,min=0,sec=0,val,times,a;
void setup(){
size(350,350);
serial=new Serial(this,"COM3",9600);

}
void draw(){
if(serial.available()>0);
{

background(0);
textSize(30);
fill(255);
text(hour,50,50);
text(":",80,50);
text(min,100,50);
text(":",130,50);
text(sec,150,50);

println(a);

}
if(val==1){
hour=hour+1;
}
if(val==2){
min=min+1;
}
if(val==3){
sec=sec+1;
}
if(val==4){
for(int i=0;i<sec;i=(millis()/1000)){
println(i);
a=(millis()/1000);
sec=sec-i;

}

}

}
``````

you are changing the value of sec in the for loop therefore the value is going to get larger quickly as for every second that your sketch is open you are counting up to the total seconds and for each one of those seconds you are adding the then total seconds time. Meaning if your sketch has ben opened for two seconds then sec will be 3, or 1 + 2, for 4 seconds and sec will be 10 or 4 + 3 + 2 + 1, or currently your doing sec = sec!.

To solve this simply take it out of the for loop.

and say sec += 1, or sec = sec + 1;

1 Like

Ok I’ll try it later

``````import ddf.minim.*;
import ddf.minim.effects.*;
import processing.serial.*;
Minim minim;
Serial serial;
int hour=0,min=0,sec=0,val,times,a;
void setup(){
size(350,350);
serial=new Serial(this,"COM3",9600);

}
void draw(){
if(serial.available()>0);
{

background(0);
textSize(30);
fill(255);
text(hour,50,50);
text(":",80,50);
text(min,100,50);
text(":",130,50);
text(sec,150,50);

println(a);

}
if(val==1){
hour=hour+1;
}
if(val==2){
min=min+1;
}
if(val==3){
sec=sec+1;
}
if(val==4){
a=millis()/1000;
sec=sec-a;
println(a);
}

}
``````

I am wondering how to make sec decrease one second at a time , my idea is when val==4, the timer will start to countdown.But my code will only decrease one second when I press the button, once I release the button it just stop.
Thanks for solving my problem.

Consider this:

``````int sec, dir = 1;

void setup()
{
frameRate(1);
}

void draw()
{
sec = sec+dir;
println(sec);
}

void keyPressed()
{
if (key == '4')
dir = -dir;
}
``````

I had this working with the Arduino as a the timer and sending a 4 to change the direction (up\down) of the seconds.

I changed the code to only provide you with a hint.

`:)`

It sounds like the value you are testing is reverting back to something else as soon as the if statement runs. Perhaps attach a bool condition to 4 instead.

``````bool k = false;
If (key == 4) k = true;

if(k) {
sec += 1;
}
``````