I decided to upgrade my bread plate and drink cup circuit from the the beginning of the semester. Now instead of red and green LEDs, Processing provides a red or green window instead. I wanted to add sounds as well but Minim was being… difficult. You’ll find my codes and my circuit below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
int circuit; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, INPUT); } void loop() { // put your main code here, to run repeatedly: circuit = map(digitalRead(9),0,1,0,255); Serial.println(circuit); delay(500); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import processing.serial.*; Serial myPort; color bgColor; int check; void setup () { // set the window size: size(600, 600); // List all the available serial ports println(Serial.list()); // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[2], 9600); // don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n'); bgColor = color(255, 0 , 0); } void draw () { background(bgColor); } void serialEvent (Serial myPort) { // make sure we have a valid byte if (myPort == null) { return; } // get the ASCII string: String inString = myPort.readStringUntil('\n'); if (myPort != null) { inString = trim(inString); } int check = int(trim(inString)); if (check == 255) { //change background bgColor = color(0, 255, 0); } else{ bgColor = color(255, 0, 0); } } |