Reading inputs into processing
in
Integration and Hardware
•
2 years ago
I'm trying to read several digital inputs from an Arduino with shift registers into Processing, and run some separate action for each switch.
I can't figure out why it won't read anything but
A=0 :(
My Arduino sends out info like this:
- if (zero) {
- Serial.print("A"); // button from chip1, pin1 HIGH
- Serial.print(0);
- Serial.print(10, BYTE); // ASCII 10 = newline character, used to separate data strings
- }
- else {
- Serial.print("A"); // button from chip1, pin1 LOW
- Serial.print(1);
- Serial.print(10, BYTE);
- }
So from the Arduino serial monitor I see:
- A0
- B1
- A0
- B1
- A0
- B1
etc.
My Processing code is this:
- import processing.serial.*;
- String buff = "";
- String temp = "";
- int NEWLINE = 10; // this is the ASCII code for a newline character
- int input_A = 0;
- int input_B = 0;
- int val = 0; // for debugging and testing
- Serial myPort;
- void setup() {
- size(210, 480);
- noStroke();
- background(204);
- //println(Serial.list());
- // print a list of all available ports
- String portName = Serial.list()[0];
- myPort = new Serial(this, portName, 9600);
- }
- void draw() {
- while (myPort.available() > 0) {
- serialEvent(myPort.read());
- val = myPort.read();
- //call to function "serialEvent" --> see below
- }
- //do something with sensor A input:
- switch (input_A) {
- case 0:
- fill(255, 255, 255);
- break;
- case 1:
- fill(100, 127, 100);
- break;
- }
- rect(30, 120, 60, 240);
- //do something with sensor B input:
- switch (input_B) {
- case 0:
- fill(255, 255, 255);
- break;
- case 1:
- fill(100, 127, 100);
- break;
- }
- rect(120, 120, 60, 240);
- println(val);
- delay(200);
- }
- void serialEvent(int serial) {
- if(serial != NEWLINE) {
- buff += char(serial);
- }
- else {
- if (buff.length() > 1) {
- temp = buff.substring(0, buff.length()-(buff.length()-1));
- //this isolates just the first letter to identify the sensor
- }
- if (temp.equals("A") == true) { // identifies pin A value
- temp = buff.substring(1, buff.length());
- input_A = int(temp); //get pin A value;
- }
- if (temp.equals("B") == true) { // identifies pin B value
- temp = buff.substring(1, buff.length());
- input_B = int(temp); //get pin B value;
- }
- }
- }
1