The code here use "===" as separator and ";" as message termination.
Arduino Code
/*
* Button
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7.
*
* http://www.arduino.cc/en/Tutorial/Button
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int inputPin2 =3;
int val = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
pinMode(inputPin2, INPUT); // declare pushbutton as input
digitalWrite(inputPin, HIGH);
digitalWrite(inputPin2, HIGH);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
val2 = digitalRead(inputPin2); // read input value
if (val == LOW) { // check if the input is HIGH
Serial.print("===1");
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
Serial.print("===0");
digitalWrite(ledPin, HIGH); // turn LED ON
}
if (val2 == LOW) { // check if the input is HIGH
Serial.print("===1;");
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
Serial.print("===0;");
digitalWrite(ledPin, HIGH); // turn LED ON
}
Serial.flush();
}
Processing Code
/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this,"COM16", 9600);
}
int buttonStatus=0;
void draw()
{
String serialMsg="";
if (0 < port.available()) { // If data is available,
serialMsg = port.readStringUntil(char(';')); // read it and store it in val
// println("serialMsg: "+serialMsg);
}
if (serialMsg.split("===").length>2){
println("serialMsg: "+serialMsg);
// println("parsedMsg: "+serialMsg.split("===")[1].charAt(0));
println("status 1: "+int(serialMsg.split("===")[1].charAt(0)-48));
// println("parsedMsg: "+serialMsg.split("===")[1].charAt(0));
println("status2: "+int(serialMsg.split("===")[2].charAt(0)-48));
buttonStatus=int(serialMsg.split("===")[1].charAt(0)-48);
}
port.clear();
background(255); // Set background to white
if (buttonStatus == 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);
}
/*
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
int switchPin = 4; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.print(1, BYTE); // send 1 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
*/
No comments:
Post a Comment