I'm attempting to trip an IR Sensor connected to my Arduino board that will then trigger an event in Processing.
I think I'm close, but I believe I'm having trouble in the Processing side of things.
My Arduino is communicating either a 1 or a 0 based on the HIGH and LOW states of the PIR Sensor (I'll include that code just in case)
Processing doesn't notice any problems, but when the PIR sensor is tripped (i have an LED attached to be sure), the sketch doesn't initiate the desired function. Help please! and Thank you in advance
Processing code:
import processing.serial.*;
XML xml;
PFont f;
XML[] comments;
int index;
Serial port;
int lf = 2;
void setup ()
{
f = createFont("Helvetica-48.vlw",16,true);
size(displayWidth, displayHeight);
background(0);
xml = loadXML("comments.xml");
comments = xml.getChildren("comment");
println(Serial.list());
port = new Serial(this, Serial.list()[4], 9600);
port.bufferUntil('\n');
}
void draw() {
//draw needs to be here to continue looping
}
void serialEvent (Serial port) {
String inString = port.readStringUntil(lf);
float inByte = float(inString);
if (inByte == '1') {
//put in here info for signalHigh
int index = int(random(comments.length));
String name = comments[index].getContent();
textFont(f, 24);
fill(245);
textAlign(LEFT);
text(name,width/4,height/4,width/2,height/2);
} else if (inByte == '0') {
//put in here info for signalLow
background(0);
}
}
Arduino Sketch
//set the addresses
int ledPin = 13;
int inputPin = 2;
int pirState = LOW;
int val = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
delay(150);
if (pirState == LOW) {
// we have just turned on
Serial.println(digitalRead(inputPin));
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
delay(300);
if (pirState == HIGH){
// we have just turned of
Serial.println(digitalRead(inputPin));
// We only want to print on the output change, not state
I'm a bit of a newbie, but I'm able to follow tutorials, and have made some nice progress in the past few weeks. I'm trying to write a small program that will draw or display text on the screen that is loaded from an XML file when an interaction is detected. At the moment, I have the following:
XML xml;
PFont f;
void setup() {
f = createFont("Arial",16,true);
size(600,400);
background(255);
//load XML content
xml = loadXML("comments.xml");
XML[] comment = xml.getChildren("comment");
int index = int(random(comment.length));
String name = comment[index].getContent();
textFont(f, 24);
fill(0);
text(name,width/2,60);
}
This gets me close, every time the program is run a bit of text is pulled successfully from my XML file and displayed. I know the way I have it written is causing part of my issue, but obviously the program has to be closed and re-ran to display another selection. Can anyone point me in the right direction to keep the display window open, and display a new bit of text from the XML file when, say, a mouse click is detected, or the spacebar is pressed? If you want to offer pointers, they're also appreciated. Thanks!