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

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.

/* 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
}
//
1 Like