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

Think I got it :slight_smile:

This seems to work. @Chrisir if you wouldn’t mind taking a look a the code, I’ve tested it for the last 15 minutes and seems to be stable.

/* This sketch randomly selects rows from a CSV file
 these rows are then printed out to screen using 
 a 'typewriter' style effect.
 I want to thetext 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; 
Table table; // table object 
TableRow row;  //Table row object 
int timer; //creates a a timer variable
int counter = 0; //creates a counter variable and sets it to zero 
String amount="", name=""; //two strings repseneting columns in csv
String full; //a string to combine amount + name 
PFont font; //font 

void setup() {
  size(1000, 1000); //size 
  // fullScreen(); //sets output to full screen 
  table = loadTable("ip.csv", "header");
  smooth(); 
  font = createFont("Arial", 48); //creates font
  textFont(font, 120); //text font 
  textAlign(CENTER, CENTER); //allings 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

    background (0); //background 

    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
    }

    typeWriter();


    break; // break for case 0 

  case 1: //this will be exectued if state = 1

    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
    background(0);

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

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


void typeWriter() {
  if (counter>=full.length()) { //this if statement resets counter to 0 when text is finsihed 
    counter++;
    counter = 0; // reset
    state=1;
  }
}

void selectRow() {  

  row = table.getRow((int)random(0, 3677)); //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 al is working
}
2 Likes