Skip to content

Instantly share code, notes, and snippets.

@macklinu
Created April 30, 2013 02:49
Show Gist options
  • Select an option

  • Save macklinu/5486326 to your computer and use it in GitHub Desktop.

Select an option

Save macklinu/5486326 to your computer and use it in GitHub Desktop.
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(57600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
import processing.serial.*;
Serial arduino; // Create object from Serial class
String name = "Name: Jo Deville";
void setup() {
String portName = Serial.list()[4];
// println(Serial.list());
arduino = new Serial(this, portName, 57600);
}
void draw() {
}
void mousePressed() {
int l = name.length();
// arduino.write(253);
// arduino.write(l);
// arduino.write(254);
for (int i = 0; i < l; i++) {
arduino.write((char)name.charAt(i));
// println((int) name.charAt(i));
}
arduino.write("\n");
//arduino.write(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment