Hello people,i'm trying to get arduino to work in conjunction with processing but i'm not getting the wright results.I just wont to read a digital output from arduino into processing and color up my high or low input.The code i'm using is:
arduino code
void setup() {
// initialize serial:
Serial.begin(9600);
pinMode(2, INPUT);
}
void loop() {
int a = digitalRead(2);
Serial.println(a); //send value to serial
}
processing code
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// Open whatever port is the one you're using.
String portName = Serial.list()[1]; My arduino is working on com4 and is listed 2nd in line after com3
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
background(255); // Set background to white
if (val == 0) { // If the serial value is 0,
fill(0); // set fill to black
}
else { // If the serial value is not 0,
fill(204); // set fill to light gray
}
rect(50, 50, 100, 100);
}
All i get is either a grey box or a black one but no change appears when i change the value of my input.First i load arduino and upload the scetch.Then i run the processing code