Hi guys, i have tryed to use the serialEvent() function:
void serialEvent() {
Serial.println("wtf?");
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;
}
}
As i understanded, it should be called automatically when data is recieved, but it never happens.
Maybe i should connect it with some interrupts "manually" or am i missing something?
It gets called at the end of each iteration of loop(), if there is something in the serial buffer. Without seeing the rest of your sketch, its hard to tell what the problem is.
import processing.serial.*;
Serial myport; // Create object from Serial class
void setup() {
// The next 3 lines are needed to call the event
String arduinoPort = Serial.list()[0];
myport = new Serial(this, arduinoPort, 115200);
myport.bufferUntil('\n');
}
// This function must be called serialEvent for this to work!!!
// it is part of the processing.serial library and is defined this way
void serialEvent(Serial myport)
{
String inString = myport.readStringUntil( '\n' );
if(inString != null )
{
inString = trim(inString);
// process the code as you wish from here
}
/*
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(9600);
}
void loop() {
/*
if (Serial.available()) {
Serial.print((char)Serial.read());
}
*/
// 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() {
Serial.println("wtf?");
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;
}
}
}
taken and modified a little bit from the tutorial.
as it is, serialEvent is never called, but if i uncomment the first lines of loop(), those:
if (Serial.available()) {
Serial.print((char)Serial.read());
}
It prints me back successfully all that i send.
Tryed even sending some '\n' (the real char ofc), CR, ecc. but nothing.
I'll try the code of buteman and let u know
Tnx for the moment for your answers =)
Ah, i didn't notice at the first sight...
Buteman, your code is for Processing, not for arduino, am i right?.
I'm looking on the code for arduino board.
zamy:
Ah, i didn't notice at the first sight...
Buteman, your code is for Processing, not for arduino, am i right?.
I'm looking on the code for arduino board.
Yes code is for Processing.
Sorry but I thought you were sending data from arduino for your PC to read.
I am using a MakeyMakey to input single numbers or letters instead of a keyboard. I need these numbers and letters to trigger events in my arduino script.
simply a loop
if L start the motor
if K stop the motor
if J reverse the motor
I tried SerialInput but this requires the user to always click the SerialMonitor send button.