Processing and Arduino via Serial issue
in
Integration and Hardware
•
3 months ago
Greetings all,
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
- pirState = LOW;
- }
- }
- }
1