# Random rows/columns in a csv file and print text with typewriter effect

**URL:** <https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889>\
**Category:** Coding Questions\
**Created:** [July 22, 2019, 3:16pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889 "2019-07-22T15:16:50Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [July 30, 2019, 11:08am UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/62 "2019-07-30T11:08:50Z")

</div>

Wow, thanks for the feedback @Chrisir, it’s really helpful.

And again, a big thanks for your patience and guidance on what has been a pretty long thread, really appreciate it.

Opt out.

---

<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:** [July 30, 2019, 11:10am UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/63 "2019-07-30T11:10:07Z")

</div>

You are welcome!

Well done!

Chrisir

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 3, 2019, 5:06pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/64 "2019-08-03T17:06:46Z")

</div>

And I’m back @Chrisir !!

I decided to try something else with this project and combine the early project with the one above. What I am now trying to achieve is the following:

- First Column appears on screen using typewriter effect.
- It stays on screen by itself for 5 seconds
- Then second line appears also with typewriter effect (it appears on a different part of the screen.
- Both lines of text then stay on screen for five seconds.
- Then the screen goes blank for 5 seconds and then reiterates through programme.

To start, I have been trying to get:

- the first line to stay for 5 seconds (I can’t get this working - although there is a 5 second gap between texts
- then the second one to appear and stay for 5 seconds (Yes, this works!)
- and then screen clears and a gap of 5 seconds before iterating again (This doesn’t work it just reiterates straight away)

So my main problem seems to be with `typeWriter1()` and `case 4`:

full code below and any suggestion/advice would be most welcome.

```auto
/* This sketch randomly selects rows from a CSV file
 these rows are then printed out to screen using 
 a 'typewriter' style effect.
 I want the text to print out and then stay on screen for 5 seconds.
 I then want the screen to stay blank for 5 seconds before starting again. 
 */

int state=0; // state of the program 
Table table; // table object 
int timer; //creates a a timer variable
int counter = 0; //creates a counter variable and sets it to zero 
String full; //a string to combine amount + name 
String amount; //a string to combine amount + name 
String newAmount; // a string with new amount 
String newName; // a string ith newNames

void setup() {
  // size(1000, 1000); //size
  fullScreen();
  PFont font; //font 
  table = loadTable("nameISP.csv", "header");
  smooth(); 
  font = createFont("HoeflerText-Italic-48", 48); //creates font
  textFont(font, 120); //text font 
  textAlign(CENTER, CENTER); //aligns text to Center
  selectRow(); //calls selectRow fucntion 
  timer=millis();
}

void draw() {

  switch (state) { //this is the start of switch 

    //this calls typeWriter1
  case 0:
    // this will be exectued if state = 0
    background (0); //background 
    typeWriter1();

    break; // break for case 0 

  case 1:
    //this is the break between typewriter 1 + 2 
    background (0); 
    if (millis()-timer > 5000) { //if 5 seconds has passed 
      timer=millis();
      state=2; // move on
    }
    break; //break for case 1 

    //this calls typeWriter2 
  case 2:
    background(0); //background 
    typeWriter2();
    break; //break for case 2

//this should keep typewriter2 text on screen for 5 seconds 
  case 3: 

    if (millis()-timer > 5000) {
      state=4;
    }
    break;
//this should wait 5 seconds before iteratating again. 
  case 4:
    if (millis()-timer > 5000) {
      selectRow();
      state=0;
    }
    break;
  }
}
//---------------------------------------------------------------------

// Tyewriter1 pulls the first column, prints it too screen, and waits a few seconds 
void typeWriter1() {

  text(newAmount.substring(0, counter), 
    0, 40, width, height); //text to print 
  if (millis()-timer > 100) { //this is the typewriter effect 
    counter++; // go to next letter 
    timer=millis(); // reset timer
  }

  if (counter > newAmount.length()) { //this if statement resets counter to 0 when text is finished 
    counter = 0; // reset
    state = 1; //go to state one 
    timer=millis(); // reset timer
  }
}

void typeWriter2() {

  text(newName.substring(0, counter), 
    100, 40, width, height ); //text to print 
  if (millis()-timer > 100) { //this is the typewriter effect 
    counter++; // go to next letter 
    timer=millis(); // reset timer
  }
  if (counter > newName.length()) { //this if statement resets counter to 0 when text is finished 
    counter = 0; // reset
    state = 3;
  }
}

void selectRow() {  

  TableRow row; //Table row object 
  String amount="", name=""; //two strings representing columns in csv
  row = table.getRow((int)random(table.getRowCount())); //print a random row  
  amount = row.getString("IP"); //a string with amount
  name = row.getString("ISP"); // prints name for row
  full = amount + " " + name; //combines name and row into new String called full 
  newAmount = amount;
  newName = name; 
  println (full); //just prints to serial monitor so I can test all is working
}
//

```

---

<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:** [August 3, 2019, 8:42pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/65 "2019-08-03T20:42:18Z")

</div>

> [@OptOut](#):
>
> timer=millis();

You forgot to write this at the end of case 3?

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 5, 2019, 12:02pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/66 "2019-08-05T12:02:49Z")

</div>

Thanks @Chrisir . I tried it

```auto
case 3: 

    if (millis()-timer > 5000) {
    timer=millis();
      state=4;
       
    }
    
    break;

```

But it hasn’t changed anything…will keep trying 🙂

---

<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:** [August 5, 2019, 2:56pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/67 "2019-08-05T14:56:08Z")

</div>

> [@OptOut](#):
>
> // this is the start of switch

This is not a useful comment because everyone sees that.

I guess useful comments kind of tell the purpose of a paragraph/ section or give an over all view.

Eg // here we distinguish between different states of the sketch

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 5, 2019, 3:15pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/68 "2019-08-05T15:15:31Z")

</div>

fair comment, to be honest I had put it in there to explain to myself as I went over the code…will avoid in future.

---

<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:** [August 5, 2019, 3:21pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/69 "2019-08-05T15:21:56Z")

</div>

> [@OptOut](#):
>
> timer=millis(); // reset timer

This is missing in typewriter2 !!!

**Remark**

You copied typewriter1 to typewriter2. I understand why. In general it’s better to write a more abstract version of the function and use it for both cases. Eg you could pass the String and what’s the next state as a parameter

---

<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:** [August 5, 2019, 3:23pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/70 "2019-08-05T15:23:08Z")

</div>

Remember to hit ctrl-t regularly in processing especially prior to posting

Also, You should have empty lines between the case sections not inside them

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 5, 2019, 3:40pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/71 "2019-08-05T15:40:47Z")

</div>

Yeah, I thought I was being smart, by reusing the same code, as in I thought the logic was the same:

> This is missing in typewriter2 !!!

Yes, but even with it in, it is still not changing anything.

> In general it’s better to write a more abstract version of the function and use it for both cases. Eg you could pass the String and what’s the next state as a parameter

Could I ask you to elaborate on this, as I’m not sure what you mean, in particular ‘pass the String and what’s the next state as a parameter’

Thanks

---

<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:** [August 5, 2019, 3:50pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/72 "2019-08-05T15:50:48Z")

</div>

What distinguishes the 2 functions is the string and the next state we go to.

So we have to clearify this when we want to unify the two.

We could move the state line out of the function two both respectively case sections

We could pass the String as parameter

[quote=“OptOut, post:64, topic:12889”]  
void typeWriter1() {  
[/ quote]

void typeWriterNew(String stringLocal) {

and then use stringLocal in the function

When we call the function typeWriterNew use 2 different variables as parameter: newAmount and newName

---

<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:** [August 5, 2019, 3:54pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/73 "2019-08-05T15:54:19Z")

</div>

> [@OptOut](#):
>
> the first line to stay for 5 seconds (I can’t get this working

No background in case 1

---

<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:** [August 5, 2019, 3:56pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/74 "2019-08-05T15:56:50Z")

</div>

Prior to using the timer you need to reset it using timer=millis(); every time

You forgot this twice

Does it work now?

If not please post your entire code

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 5, 2019, 3:57pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/75 "2019-08-05T15:57:23Z")

</div>

> [@Chrisir](#):
>
> No background in case 1

I just spent 3 days trying to fix/find that 🤦‍♂️

---

<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:** [August 5, 2019, 4:18pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/76 "2019-08-05T16:18:00Z")

</div>

Not using `background()` command makes the text less _crisp_ optically.

Alternatively you would `text()` out the text after `background()` in this `case` section.

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 5, 2019, 4:38pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/77 "2019-08-05T16:38:28Z")

</div>

I’ve just checked again and from what I can see timer is reset each time.

```auto
/* This sketch randomly selects rows from a CSV file
 these rows are then printed out to screen using 
 a 'typewriter' style effect.
 I want the text to print out and then stay on screen for 5 seconds.
 I then want the screen to stay blank for 5 seconds before starting again. 
 */

int state=0; // state of the program 
Table table; // table object 
int timer; //creates a a timer variable
int counter = 0; //creates a counter variable and sets it to zero 
String full; //a string to combine amount + name 
String amount; //a string to combine amount + name 
String newAmount; // a string with new amount 
String newName; // a string ith newNames

void setup() {
  // size(1000, 1000); //size
  fullScreen();
  PFont font; //font 
  table = loadTable("nameISP.csv", "header");
  smooth(); 
  font = createFont("HoeflerText-Italic-48", 48); //creates font
  textFont(font, 120); //text font 
  textAlign(CENTER, CENTER); //aligns text to Center
  selectRow(); //calls selectRow fucntion 
  timer=millis();
}

void draw() {
  switch (state) { 

    //this calls typeWriter1
  case 0:
    // this will be exectued if state = 0
    background (0); //background 
    typeWriter1();
    break; // break for case 0 

  case 1:
    //this is the break between typewriter 1 + 2 

    if (millis()-timer > 5000) { //if 5 seconds has passed 
      timer=millis();
      state=2; // move on
    }
    break; //break for case 1 

    //this calls typeWriter2 
  case 2:
    background(0); //background 
    typeWriter2();
    break; //break for case 2

    //this should keep typewriter2 text on screen for 5 seconds 
  case 3: 

    if (millis()-timer > 5000) {
      timer=millis();
      state=4;
    }

    break;

    //this should wait 5 seconds before iteratating again. 
  case 4:
    if (millis()-timer > 5000) {
      timer=millis();
      selectRow();
      state=0;
    }
    break;
  }
}

//---------------------------------------------------------------------

// Tyewriter1 pulls the first column, prints it too screen, and waits a few seconds 
void typeWriter1() {

  text(newAmount.substring(0, counter), 
    0, 40, width, height); //text to print 
  if (millis()-timer > 100) { //this is the typewriter effect 
    counter++; // go to next letter 
    timer=millis(); // reset timer
  }

  if (counter > newAmount.length()) { //this if statement resets counter to 0 when text is finished 
    counter = 0; // reset
    state = 1; //go to state one 
    timer=millis(); // reset timer
  }
}

void typeWriter2() {

  text(newName.substring(0, counter), 
    100, 40, width, height ); //text to print 
  if (millis()-timer > 100) { //this is the typewriter effect 
    counter++; // go to next letter 
    timer=millis(); // reset timer
  }
  if (counter > newName.length()) { //this if statement resets counter to 0 when text is finished 
    counter = 0; // reset
    state = 3;
    timer=millis(); // reset timer
  }
}

void selectRow() {  

  TableRow row; //Table row object 
  String amount="", name=""; //two strings representing columns in csv
  row = table.getRow((int)random(table.getRowCount())); //print a random row  
  amount = row.getString("IP"); //a string with amount
  name = row.getString("ISP"); // prints name for row
  full = amount + " " + name; //combines name and row into new String called full 
  newAmount = amount;
  newName = name; 
  println (full); //just prints to serial monitor so I can test all is working
}
//

```

---

<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:** [August 6, 2019, 3:11pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/78 "2019-08-06T15:11:20Z")

</div>

when it works now, all timers are reset correctly

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 7, 2019, 12:31pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/79 "2019-08-07T12:31:19Z")

</div>

Almost!

Now first line appears for 5 seconds as does second…however I would like for there to be a blank screen after the second line appears - so a five second blank screen between iterations.

I tried to introduce another state to acheive this, but it just keeps the second line on screen for longer:

```auto

void draw() {
  switch (state) { 

    //this calls typeWriter1
  case 0:
    // this will be exectued if state = 0
    background (0); //background 
    typeWriter1();
    break; // break for case 0 

  case 1:
    //this is the break between typewriter 1 + 2 

    if (millis()-timer > 5000) { //if 5 seconds has passed 
      timer=millis();
      state=2; // move on
    }
    break; //break for case 1 

    //this calls typeWriter2 
  case 2:
    background(0); //background 
    typeWriter2();
    break; //break for case 2

    //this should keep typewriter2 text on screen for 5 seconds 
  case 3: 

    if (millis()-timer > 5000) {
      timer=millis();
      state=4;
    }

    break;

    // trying to create a 5 second delay before iterations
  case 4: 

    if (millis()-timer > 5000) {
      timer=millis();
      state=5;
    }

    break;

    //this should wait 5 seconds before iteratating again. 
  case 5:
    if (millis()-timer > 5000) {
      timer=millis();
      selectRow();
      state=0;
    }
    break;
  }
}

```

Also, If I wanted to keep the first line on screen for the whole iteration, would I need to implement a boolean?

Thanks again.

---

<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:** [August 7, 2019, 1:40pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/80 "2019-08-07T13:40:32Z")

</div>

> [@OptOut](#):
>
> however I would like for there to be a blank screen after the second line appears -

Okay, both typewriter need a timer=millis();

when you want to show a blank screen use background() in this case section

When you want a message to stay on the screen: just have a text() command in all appropriate case sections

---

<div class="post-metadata">

**Author:** ![OptOut](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@OptOut](https://discourse.processing.org/u/OptOut)\
**Post date:** [August 7, 2019, 3:35pm UTC](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889/81 "2019-08-07T15:35:47Z")

</div>

> [@Chrisir](#):
>
> when you want to show a blank screen use background() in this case section
> 
> When you want a message to stay on the screen: just have a text() command in all appropriate case sections

So simple, so obvious …yet so frustratingly difficult to do!

I won’t say goodbye this time (learnt my lesson last time) but for now, I think I’m good 🙂

Thanks again @Chrisir

[Previous page](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889.md?page=3)

[Next page](https://discourse.processing.org/t/random-rows-columns-in-a-csv-file-and-print-text-with-typewriter-effect/12889.md?page=5)
