# Easy typing program

**URL:** https://discourse.processing.org/t/easy-typing-program/15226
**Category:** Coding Questions
**Created:** [November 4, 2019, 5:37pm UTC](https://discourse.processing.org/t/easy-typing-program/15226 "2019-11-04T17:37:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![helpme123](https://avatars.discourse-cdn.com/v4/letter/h/c5a1d2/32.png) [@helpme123](https://discourse.processing.org/u/helpme123)
#### Post date: [November 4, 2019, 5:37pm UTC](https://discourse.processing.org/t/easy-typing-program/15226/1 "2019-11-04T17:37:08Z")

</div>

Im trying to create a program where a random letter appears on the screen and the user has to type it to gain 1 point and if they type it within a time frame they get bonus points (I haven’t wrote the point system yet because my current code in not working). The letters in my code fill up the canvas constantly instead of only appearing once.

```auto
void setup(){
  size(500,500);
}
void draw(){
  background(255); 
  String[] letter = { "A", "B", "C", "D" };
  int index = int(random(letter.length));
  textSize(25);
  fill(100,0,0);
  text((letter[index]),200,200);
}

```

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [November 4, 2019, 7:32pm UTC](https://discourse.processing.org/t/easy-typing-program/15226/2 "2019-11-04T19:32:54Z")

</div>

Good day helpme123, welcome to the forum.

Could you please edit your post and format the code? You can press the button that looks like `< /> (Preformatted text)` and place your Processing code in there. The outcome should look similar to this:

```auto
void setup() {
  size(500, 500);
}

void draw() {
  // rest of your code
}

```

You have to take in account that `void draw()` works like a constant loop. Once your sketch runs, all the code inside of it gets repeated around 60 times a second (unless specified otherwise or if the sketch is too heavy to run it smoothly). That being said, do you understand why the letter keeps switching constantly?

I think it will be a good exercise for you to figure out the difference between local and global variables. If you make `int index` a global variable, you could –for now as a first step– use [mouseReleased](https://processing.org/reference/mouseReleased_.html) to switch the displayed letter.

---

<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: [November 8, 2019, 10:01am UTC](https://discourse.processing.org/t/easy-typing-program/15226/3 "2019-11-08T10:01:57Z")

</div>

> [@helpme123](#):
>
> The letters in my code fill up the canvas constantly instead of only appearing once.

I often use these as a simple “timer” to control when I update data on screen:

> **[frameCount / Reference](https://processing.org/reference/frameCount.html)**
>
> The system variable frameCount contains the number o frames displayed since the program started. Inside setup() the value is 0 and during the first iteration of draw it is 1, etc.

> **[% (modulo) / Reference](https://processing.org/reference/modulo.html)**
>
> Calculates the remainder when one number is divided by another. For example, when 52.1 is divided by 10, the divisor (10) goes into the dividend (52.1) five times (5 \* 10 == 50), and there is a remain…

> **[if / Reference](https://processing.org/reference/if.html)**
>
> Allows the program to make a decision about which code to execute. If the test evaluates to true, the statements enclosed within the block are executed and if the test evaluates t…

I keep frameRate at default (60 fps).

Consider what this does:

```auto
  if (frameCount%60 == 0) 
    {
    // Update something
    }
   // Display something

```

This is also available to you:

> **[millis() / Reference](https://processing.org/reference/millis_.html)**
>
> Returns the number of milliseconds (thousandths of a second) since starting the sketch. This information is often used for timing animation sequences.

🙂

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 8, 2019, 11:05am UTC](https://discourse.processing.org/t/easy-typing-program/15226/4 "2019-11-08T11:05:16Z")

</div>

I wouldn’t use frameRate

In that scenario the Sketch would just wait until a key is pressed

**To do**

So this line

```auto
int index = int(random(letter.length));

```

becomes:

- `int index;` before setup()

- AND `index = int(random(letter.length));` inside setup() and in a new function `void keyPressed() { }`

---

<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: [November 8, 2019, 5:30pm UTC](https://discourse.processing.org/t/easy-typing-program/15226/5 "2019-11-08T17:30:06Z")

</div>

> [@Chrisir](#):
>
> I wouldn’t use frameRate

I was able to implement this with my suggestion so that the random number selection was made every 2 sec and displayed with each frame.

The rest of the code is easily achievable so I will not assist further.

@helpme123  
millis() may be helpful:

> **[millis() / Reference](https://processing.org/reference/millis_.html)**
>
> Returns the number of milliseconds (thousandths of a second) since starting the sketch. This information is often used for timing animation sequences.

Keep track of time of events and take the difference.

How you right your code is up to you.

🙂
