Continuing my R+D into arduino and processing I have setup a basic example of serial communication using a button connected to my Arduino that sends a value to the Processing. I also have an LED connected but this can be dropped out if not required.
The Arduino sketch
// variables for button pin and LED
int buttonInput = 11;
int LEDpin = 10;
// variable to store the value
int value = 0;
void setup(){
// declaration pin modes
pinMode(buttonInput, INPUT);
pinMode(LEDpin, OUTPUT);
// begin sending over serial port
Serial.begin(9600);
}
void loop(){
// read the value on digital input
value = digitalRead(buttonInput);
// write this value to the control LED pin
digitalWrite(LEDpin, value);
if (value)
{
Serial.println(255);
}
else
{
Serial.println(0);
}
// wait a bit to not overload the port
delay(10);
}
The Processing sketch
import processing.serial.*;
Serial port;
float brightness = 0;
void setup()
{
size (500,500);
port = new Serial(this, Serial.list()[0], 9600);
port.bufferUntil('n');
}
void draw()
{
background(0,0,brightness);
}
void serialEvent(Serial port)
{
brightness = float(port.readStringUntil('n'));
}
Published by: nick in Arduino, Processing