// 10-12-04 revised by Perry Hoberman for USC/CNTV/IM534 class to // communicate with Basic Stamp 2 by writing the number 100 // to trigger input from the Stamp. Use with serialCableTest.bs2 // running on the Basic Stamp (which counts up and down between // 0 and 400). // Analog In // by Josh Nimoy // Reads a value from the serial port and sets the background color. // Running this example requires you have a BX-24 microcontroller // and peripheral hardware. More information can be found on the tutorial // pages of Tom Igoe: http://stage.itp.nyu.edu/%7Etigoe/pcomp/examples.shtml // Make sure to have the correct port selected in the "sketch" menu. Because // this program uses the serial port, it will not work within a web browser. // Created 8 February 2003 String buff = ""; // serial input string int val = 0; // to convert serial input string to a number int RETURN = 13; // ASCII number for seperator (carriage return) int data = 100; // number to write to BS2 to trigger input void setup() { size(200, 200); // window size beginSerial(9600); // open serial port } void loop() { if (val <= 255) { background(val); // set background color to current value } else { background(val-145,0,0); // if value is > 255, set background to a shade of red } if (buff == ""); // buff set to empty string after RETURN received { serialWrite(data); // send a 100 to the BS2 to trigger new value } } void serialEvent() { // If the variable "serial" is not equal to the value for // a new line, add the value to the variable "buff". If the // value "serial" is equal to the value for a new line, // save the value of the buffer into the variable "val". if(serial != RETURN) { buff += char(serial); } else { val = Integer.parseInt(buff); // Parse the String into an integer println(val); buff = ""; // Clear the value of "buff" } }