Go Down

Topic: Sabertooth 2x25 Simpified Serial Mode ( Tank Style (Read 19345 times) previous topic - next topic

Erac

Hey Paul,

I commented out the keypress part because it does not respond to the keys when pressed, so I tried to assign the specific chars to the motor and it worked fine.

My setup is: 2 motors, batteries, sabertooth ( motor controller), and the arduino.. so I want it to respond to the keyboard press on my computer, but it won't..I don't know why.

any ideas?

Regards

PaulS

Do you have an application running on the PC that is capturing the key presses and sending them to the serial port? If so, what does that code look like.

If not, you need one.
The art of getting good answers lies in asking good questions.

Erac

#17
Oct 17, 2010, 04:30 pm Last Edit: Oct 17, 2010, 04:45 pm by Erac Reason: 1
My only application is arduino 019 compiler/loader.. What applications captures the keypresses( using laptop keyboard)?

PaulS

Quote
What applications captures the keypresses?

You need to create one. There are a variety of ways to go about this, depending on your skill- and tool-set. Visual Studio (Windows only; free versions are available from microsoft.com) allows you to create C++ or C# applications that can capture keystrokes, and send them to the serial port. If you send them to the same one that the Arduino is connected to, the Arduino can read the characters sent.

Processing is another possibility. It includes several examples that send and receive serial data, and it includes examples that get keyboard input. Combining two sketches is pretty simple.
The art of getting good answers lies in asking good questions.

Erac

#19
Oct 18, 2010, 01:05 am Last Edit: Oct 18, 2010, 01:17 am by Erac Reason: 1
Hi Paul,

I'm quite interested with the Microsoft Visual Studio that creates C++ or C# applications that can capture keystrokes, and send them to the serial port....

I'm just not sure how to set this up both software and hardware so that it can communicate with my arduino. Do you have samples?


For the Processing, I found this code: http://wiki.processing.org/w/W-A-S-D_control_keyboard_input ,but how can I incorporate it with my arduino? for the control?



Regards

PaulS

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.

Code: [Select]
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;
 }
}
The art of getting good answers lies in asking good questions.

Erac

#21
Oct 18, 2010, 02:49 am Last Edit: Oct 18, 2010, 10:03 am by Erac Reason: 1
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

PaulS

Use Processing to write data to the serial port. On the Arduino, read from the serial port.
The art of getting good answers lies in asking good questions.

Erac

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

PaulS

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

Yes.

Quote
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.
The art of getting good answers lies in asking good questions.

Erac

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

Erac

#26
Oct 19, 2010, 08:44 am Last Edit: Oct 19, 2010, 08:46 am by Erac Reason: 1
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 http://www.arduino.cc/playground/Interfacing/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

PaulS

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.
Code: [Select]
switch(data)
{
  case 'w':
     // Do something
     break;
// Add remaining cases for 'a', 's', 'd', and 'q'
}


Quote
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
  • 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.
The art of getting good answers lies in asking good questions.

Erac

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
  • which ever is for COM 5 which I saw in my Arduino IDE..

    myPort.write(key);


    Am I on the right track?




PaulS

Quote
Am I on the right track?

Except for this:
Code: [Select]
myPort.write(key);
The value is being sent in the keyPressed() event handler.

Watch out for trains.
The art of getting good answers lies in asking good questions.

Go Up