The keyPressed function is called whenever a key is pressed. The value in the key variable corresponds to the key that was pressed. Use a switch statement, like the sample code, but change the body of the case statements to print the key to the serial port.
Look at one of the serial examples to see how to create a connection to the serial port.
I just downloaded the processing loader/ compiler, but how can I connect it to the arduino code? For it to read my keyboard inputs and send data to my motor controller via my arduino?
Is it possible for my case? Sorry Its my first time to see processing
So I will use the serial.write command in Processing that is linked with the keypressed () then on the arduino, I will use serial.read() and link it with the setEngineSpeed( traverse ) and setEngineSpeedDir( negotiate )? Is it correct?
Will my codes below work? I did everything as said on Arduino Playground - Processing , but how will I connect processing to arduino ( what sketch to be typed)?
My Arduino Code ( part to be applied): (not sure about the key part)
void loop()
{
signed char traverse;
signed char negotiate;
if(Serial.available()>0)
{
int data = Serial.read();
//digitalWrite(buttonPin,LOW);
switch(data)
{ // key is from the Processing keypress
case'key':traverse = 100;break; //full forward
case'key':traverse = -50;break; // half reverse
case'key':traverse = 0;break; // Stop
case'key':negotiate = -100;break; // left
case'key':negotiate = 100;break; // right
}
I showed you, in reply #20, what the keyPressed() function should look like. For every key press event, there will be a be a key release event. But, you don't need to do anything in the event handler, so don't supply one.
On the Arduino, you need to read the serial data, as you are doing. The value in data will be 'w', 'a', 's', 'd', or 'q', so those are the values that you need to supply cases for.
switch(data)
{
case 'w':
// Do something
break;
// Add remaining cases for 'a', 's', 'd', and 'q'
}
I'm not sure on the Serial.list()[0]
The Serial class has a list method that returns an array of port names that the Arduino might be connected to. The [0] says to use the first name in the list. If that is not the correct value, you will need to change it. You can print the list (print(Serial.list()) to see what is in it. Compare the names in the list to the names in the Tools + Serial Port list in the Arduino IDE, and see that you are, in the Processing application, using the correct index.
Sorry to bother you again,but I'm already pulling my hair off because of the expecting EOF, found 'myPort' problem when I run my processing code
processing.app.debug.RunnerException: expecting EOF, found 'myPort'
at processing.app.Sketch.preprocess(Sketch.java:1326)
at processing.app.Sketch.preprocess(Sketch.java:1205)
at processing.app.Sketch.build(Sketch.java:1568)
at processing.app.Sketch.build(Sketch.java:1553)
at processing.app.Editor$DefaultRunHandler.run(Editor.java:1485)
at java.lang.Thread.run(Thread.java:619)
processing.app.debug.RunnerException: expecting EOF, found 'myPort'
at processing.app.Sketch.preprocess(Sketch.java:1326)
at processing.app.Sketch.preprocess(Sketch.java:1205)
at processing.app.Sketch.build(Sketch.java:1568)
at processing.app.Sketch.build(Sketch.java:1553)
at processing.app.Editor$DefaultRunHandler.run(Editor.java:1485)
at java.lang.Thread.run(Thread.java:619
Serial myPort;
//println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
void draw(){
}
//}
I already placed an empty draw function as you advised, but still with same error, but when I try to place a setup function, it has an error or unexpected void...
I can't possibly get my Processing code to work, now the error is:
processing.app.debug.RunnerException: The function print(char) does not exist.
at processing.app.Sketch.placeException(Sketch.java:1543)
at processing.app.debug.Compiler.compile(Compiler.java:149)
at processing.app.Sketch.build(Sketch.java:1573)
at processing.app.Sketch.build(Sketch.java:1553)
at processing.app.Editor$DefaultRunHandler.run(Editor.java:1485)
at java.lang.Thread.run(Thread.java:619)
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw(){
}
I got impatient with processing that I tried to go back to my arduino code.. And luckily I made the keypress work.. I added Serial.begin (9600); in my setup function.
The only odd thing is that the stop button Q wont respond, I don't get it? Is there a possible way in the arduino coding that when I release the W ( forward button) then the motor will also cease going forward? Because when I press/ send the keypress button in the serial monitor it goes on until a new command is sent.. Help