Created
June 22, 2016 02:26
-
-
Save ArduinoBasics/fb77a19ede0e807ef321e0944b09cef6 to your computer and use it in GitHub Desktop.
Processing 3.1.1 Random Font Display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* STAGE 8 : Random Font Selector with help from Arduino | |
ScottC : http://arduinobasics.blogspot.com/ | |
Some of the Serial code was adapted from Tom Igoe's example | |
on this site: http://processing.org/reference/libraries/serial/Serial.html | |
and http://processing.org/reference/libraries/serial/serialEvent_.html | |
The rest of this code was written by ScottC on 11/07/2012 | |
Updated for Processing 3.1.1 (32bit): on 22/06/2016 | |
*/ | |
import processing.serial.*; /* You need this for Serial communication*/ | |
/* Global Variables-------------------------------- */ | |
Serial comPort; | |
String [] comPortList; | |
String comPortString; | |
PFont font; | |
String [] myFontList; | |
int fontNumber; | |
String fontString=""; | |
Boolean update=false; | |
/*--------------------------------------------------*/ | |
void setup(){ | |
size(500,500); /* set the size of the screen, 500x500 */ | |
background(0); /* change the background of the screen to black */ | |
comPortList=Serial.list(); /* Get Available COM ports */ | |
myFontList = PFont.list(); /* Get Available Fonts */ | |
if(comPortList.length>0){ /* Check if there is at least one available COM port */ | |
comPort=new Serial(this, comPortList[0], 9600); /* Open the first COM port in the list */ | |
comPort.bufferUntil('\n'); /* Listen for the carriage return */ | |
} /* which will generate a serialEvent */ | |
} | |
/*--------------------------------------------------*/ | |
void draw(){ | |
if(update){ | |
/* Display the font on the screen in a random position */ | |
text(fontString, random(width)-100,random(height)+10); | |
update=false; | |
} | |
} | |
/*--------------------------------------------------*/ | |
void serialEvent(Serial cPort){ | |
comPortString = cPort.readStringUntil('\n'); | |
if(comPortString != null){ | |
comPortString=trim(comPortString); | |
/* Map the Arduino value to the number of fonts */ | |
fontNumber = int(map(Integer.parseInt(comPortString),1,1000,1,myFontList.length)); | |
/* Select the font based on the Arduino Value */ | |
fontString = myFontList[fontNumber]; | |
/* Set the font */ | |
font=createFont(fontString,48); | |
textFont(font); | |
// background(0); //Undo the comment for a different effect | |
/* Choose a random Earthy colour for the font */ | |
fill(random(200),random(100),0); | |
update=true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment