# Increasement with ONE on keypress

**URL:** <https://discourse.processing.org/t/increasement-with-one-on-keypress/15906>\
**Category:** Coding Questions\
**Created:** [November 28, 2019, 2:41pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906 "2019-11-28T14:41:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanoevelenstef](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vanoevelenstef](https://discourse.processing.org/u/vanoevelenstef)\
**Post date:** [November 28, 2019, 2:41pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/1 "2019-11-28T14:41:44Z")

</div>

Hey everyone. I’ve founded this code online I’m reworking it, so I come to my needs but now I’ve hit a bump. This is only a section of my code, so I don’t know if this enough to have this work.

In the original code` int pointsPerRevolution` is 720, but I want it that it starts as for example 10 and every time key is pressed it would be increment with 50. I founded some else his code and tried to implement it, but no success.

Can someone help me with this? It would be a huge help for my project. If you want to see the whole code let me know. (its 712 lines long, so not needed if this can help me).

```auto
//int pointsPerRevolution = 720; 
int pointsPerRevolution;

int count = 0; //counter

void randomRevolution() {
  //pointsPerRevolution = random(10, 720);
  pointsPerRevolution = 10;
  iterate = true;
  println(pointsPerRevolution);
}

void counterUp() {
  if ( key == CODED ) {
    if (keyCode == CONTROL ) {
      count+=1;
      print(count);
    }
  }
  if (count>10) {
    count=0;
  }
  print(count);

  loop();
}

```

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 28, 2019, 8:58pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/2 "2019-11-28T20:58:47Z")

</div>

```auto
int pointsPerRevolution = 10;

void keyPressed() {
   if (keyCode == UP) {
      pointsPerRevolution++;
   } else if (keyCode == DOWN) {
      pointsPerRevolution—;
   }
}

```

---

<div class="post-metadata">

**Author:** ![vanoevelenstef](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vanoevelenstef](https://discourse.processing.org/u/vanoevelenstef)\
**Post date:** [November 28, 2019, 9:23pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/3 "2019-11-28T21:23:21Z")

</div>

Thanks! It worked! Can I increase with 10 or 50 at a time instead of 1? And how do I stop at certain point?

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 28, 2019, 9:54pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/4 "2019-11-28T21:54:27Z")

</div>

You can use :

```auto
a++; //increments by 1
a += b //increments by b, also works with -=, /=, *= and maybe some others like %=, though i never tried that one...
a = a + b; // the most Basic way. 

```

And you can make it stop by not pressing the key anymore?

Edit :

Nvm, you meant at a certain value to not increase anymore, right? In that case you can use either constrain.

```auto
a += 10;
a = constrain(a, 0, 100); // a will be set to the closest if it’s smaller than 0 or higher than 100

a = constrain(a + 10, 0, 100); // same, just in one line. 

```

Or you can use an if statement before increasing it.

```auto
if (a < 100) {
   a++;
}
//this won’t work with variable increments
// for that you‘ll Have to use

if (a <= 100 - b) {
a += b;
}

```

---

<div class="post-metadata">

**Author:** ![vanoevelenstef](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vanoevelenstef](https://discourse.processing.org/u/vanoevelenstef)\
**Post date:** [November 28, 2019, 9:59pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/5 "2019-11-28T21:59:51Z")

</div>

Alright, that also works, so thanks.  
And yeah it stops indeed by not pressing the key. But in my code I start that after 700 nothing else changes any more. (the pointsPerRevolution is var for points in a shape. After 700 points the shape doesn’t change visually a lot any more. So after 700 would be pointless to add. You get it?

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 28, 2019, 10:02pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/6 "2019-11-28T22:02:48Z")

</div>

Yeah, noticed that a bit late 😅

---

<div class="post-metadata">

**Author:** ![vanoevelenstef](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@vanoevelenstef](https://discourse.processing.org/u/vanoevelenstef)\
**Post date:** [November 28, 2019, 10:13pm UTC](https://discourse.processing.org/t/increasement-with-one-on-keypress/15906/7 "2019-11-28T22:13:45Z")

</div>

Ooh, awesome! That was just what I wanted. You were a great help! You saved my project!
