Sabertooth 2x25 Simpified Serial Mode ( Tank Style

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.

void keyPressed()
{
  switch(key)
 {
    case('w'):
    case('W'):
    case('d'):
    case('D'):
    case('s'):
    case('S'):
    case('a'):
    case('A'):
    case('q'):
    case('Q'):
       myPort.print(key);
       break;
  }
}

Hi Paul,

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

Regards

Use Processing to write data to the serial port. On the Arduino, read from the serial port.

Paul,

Sorry, but It's not quite clear to me..

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?

Regards

So I will use the serial.write command in Processing that is linked with the keypressed ()

Yes.

then on the arduino, I will use serial.read() and link it with the setEngineSpeed( traverse ) and setEngineSpeedDir( negotiate )?

You will uncomment the code that you already have for reading serial data.

Thanks Paul it's clearer now?I will try it and update you.

Hi Paul,

I wish to simmulate what this code: http://wiki.processing.org/w/W-A-S-D_control_keyboard_input with keyrelease and keypress
to my motor control using processing to arduino?

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
}

setEngineSpeed( traverse );
setEngineSpeedDir( negotiate );
}
}

My Processing Code: I'm not sure on the Serial.list()[0]

import processing.serial.*;
int result;

void setup()
{

}

void keyPressed(){
switch(key) {
case('w'):case('W'):result |=FORWARD;break;
case('d'):case('D'):result |=RIGHT;break;
case('s'):case('S'):result |=REVERSE;break;
case('a'):case('A'):result |=LEFT;break;
case('q'):case('Q'):result |=STOP;break;

}
}

void keyReleased(){
switch(key) {
case('w'):case('W'):result ^=FORWARD;break;
case('d'):case('D'):result ^=RIGHT;break;
case('s'):case('S'):result ^=REVERSE;break;
case('a'):case('A'):result ^=LEFT;break;
case('q'):case('Q'):result ^=STOP;break;
}
}

import processing.serial.*;

Serial myPort;

println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600);

myPort.write(key);

Hope for your support

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()):wink: 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.

Paul,

So in the Arduino:

switch(data)
{
case 'w':
// Do something
break;
// Add remaining cases for 'a', 's', 'd', and 'q'
}

For the Processing

void keyPressed()
{
switch(key)
{
case('w'):
case('W'):
case('d'):
case('D'):
case('s'):
case('S'):
case('a'):
case('A'):
case('q'):
case('Q'):
myPort.print(key);
break;
}
}

import processing.serial.*;

Serial myPort;

println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600); // I will edit [0] which ever is for COM 5 which I saw in my Arduino IDE..

myPort.write(key);

Am I on the right track?

Am I on the right track?

Except for this:

myPort.write(key);

The value is being sent in the keyPressed() event handler.

Watch out for trains.

So myPort.write(); So I will just leave it empty? Assuming it already sent data to COM 5.. ?

Remove it.

Noted.. I will link it all and update you.

Thanks

Hi Paul,

So how will I run processing to arduino? I will just compile my sketches for my processing and arduino and then upload my arduino sketch to the board?

My Arduino Code ( edited keypress part only, the others are same)

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'w':traverse = 100;break; //full forward
case's':traverse = -50;break; // half reverse
case'q':traverse = 0;break; // Stop
case'a':negotiate = -100;break; // left
case'd':negotiate = 100;break; // right
}

setEngineSpeed( traverse );
setEngineSpeedDir( negotiate );
}
}

My Processing Code: ( edited)

void keyPressed()
{
switch(key)
{
case('w'):
case('W'):
case('d'):
case('D'):
case('s'):
case('S'):
case('a'):
case('A'):
case('q'):
case('Q'):
myPort.print(key);
break;
}
}

import processing.serial.*;

Serial myPort;

println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600); // I will edit [0] which ever is for COM 5 which I saw in my Arduino IDE..

Regards

So how will I run processing to arduino? I will just compile my sketches for my processing and arduino and then upload my arduino sketch to the board?

I'm going to quit holding your hand now. There are just some things you are going to have to try on your own.

Paul,

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)

[highlight]I already verified that 0 is my COM 5

The valuation of myPort (the myPort = statement) belongs in the setup() function. You also need a draw() function, even if it is empty.

draw()
{
}

Paul,

My Processing sketch still won't compile:

Still with the same error of:

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

//void setup (){

void keyPressed()
{

switch(key)
{
case('w'):
case('W'):
case('d'):
case('D'):
case('s'):
case('S'):
case('a'):
case('A'):
case('q'):
case('Q'):
myPort.print(key);
break;
Serial myPort;
}
}

import processing.serial.*;

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...

Please help :cry:

Regards

The valuation of myPort (the myPort = statement) belongs in the setup() function.

Where is your setup() function?

Why is this code in the keyPressed() event handler?

    Serial myPort;

Hi Paul,

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)

My Processing Code:

void keyPressed()
{

switch(key)
{
case('w'):
case('W'):
case('d'):
case('D'):
case('s'):
case('S'):
case('a'):
case('A'):
case('q'):
case('Q'):
myPort.print(key);
break;
//Serial myPort;
}
}

import processing.serial.*;

Serial myPort;
//println(Serial.list());

void setup (){

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

Regards