# Timer problem with the value

**URL:** <https://discourse.processing.org/t/timer-problem-with-the-value/28858>\
**Category:** Electronics (Arduino, etc.)\
**Tags:** homework\
**Created:** [March 26, 2021, 4:13am UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858 "2021-03-26T04:13:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![david27266361313](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@david27266361313](https://discourse.processing.org/u/david27266361313)\
**Post date:** [March 26, 2021, 4:13am UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858/1 "2021-03-26T04:13:49Z")

</div>

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!

```auto
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);
  {
    val=serial.read();
    
    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;
    
    }
  
  
  }

}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [March 26, 2021, 5:21am UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858/2 "2021-03-26T05:21:34Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![david27266361313](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@david27266361313](https://discourse.processing.org/u/david27266361313)\
**Post date:** [March 26, 2021, 5:34am UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858/3 "2021-03-26T05:34:48Z")

</div>

Ok I’ll try it later  
Thanks for your reply!

---

<div class="post-metadata">

**Author:** ![david27266361313](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@david27266361313](https://discourse.processing.org/u/david27266361313)\
**Post date:** [March 26, 2021, 2:08pm UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858/4 "2021-03-26T14:08:24Z")

</div>

```auto
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);
  {
    val=serial.read();
    
    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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 27, 2021, 3:04am UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858/5 "2021-03-27T03:04:22Z")

</div>

> [@david27266361313](#):
>
> when val==4, the timer will start to countdown

Consider this:

```auto
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.

`:)`

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [March 27, 2021, 10:21pm UTC](https://discourse.processing.org/t/timer-problem-with-the-value/28858/6 "2021-03-27T22:21:23Z")

</div>

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.

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

if(k) {
sec += 1;
} 

```
