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

Well done!

Congratulations!

A few remarks.

Remark

if (counter>=full.length()) {  
    counter++;  **// not necessary here !!!!**
    counter = 0; // reset

just use if (counter > full.length()) {

Remark

Not necessary to call typeWriter(); from setup()

The function typeWriter()

The function typeWriter() could hold more code:


void typeWriter() {
  text(full.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>=full.length()) { //this if statement resets counter to 0 when text is finsihed 
    counter = 0; // reset
    state=1;
  }
}

Remark

You can move some variables into functions, when the variables are only used there.

Remark

This is unnecessary, in case 2 you can go to 0 directly

case 3:
    state=0;
    break; //break for final break

Here is my version:



/* 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 

void setup() {
  size(1000, 1000); //size
  // fullScreen(); //sets output to full screen 

  PFont font; //font 

  table = loadTable("ip.csv", "header");
  smooth(); 
  font = createFont("Arial", 48); //creates font
  textFont(font, 120); //text font 
  textAlign(CENTER, CENTER); //aligns text to Center
  selectRow(); //calls selectRow fucntion 
  timer=millis();
  // typeWriter();
}

void draw() {

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

  case 0:
    // this will be exectued if state = 0
    // the typewriter 
    background (0); //background 
    typeWriter();
    break; // break for case 0 

  case 1:
    // this will be exectued if state = 1
    // the text stays on screen for 5 seconds.
    if (millis()-timer > 5000) { //if 5 seconds has passed 
      timer=millis();
      state=2;  // move on
    }
    break; //break for case 1 

  case 2:
    // this will be exectued if state = 2
    // the screen stays blank for 5 seconds before starting again. 
    background(0); //background 
    if (millis()-timer > 5000) {
      selectRow();
      state=0;
    }
    break; //break for case 2
    //
  }//switch
}//func 

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

void typeWriter() {
  text(full.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 > full.length()) { //this if statement resets counter to 0 when text is finished 
    counter = 0; // reset
    state=1;
  }
}

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("amount"); //a string with amount
  name = row.getString("name"); // prints name for row
  full = amount + " " +  name;  //combines name and row into new String called full 
  // println (full); //just prints to serial monitor so I can test all is working
}
//

Congratulations again!!!

Chrisir

4 Likes