Pages: [1]   Go Down
Author Topic: beginning serial robot control help  (Read 2085 times)
0 Members and 1 Guest are viewing this topic.
The present
Offline Offline
Newbie
*
Karma: 0
Posts: 20
nom nom nom
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey guys,
i want to make an arduino robot controlled from a PC, using two 360deg. servos and an arduino uno on one side, and (most likely) processing on the other end, with usb in between for beginning/troubleshooting stages, then (maybe) switch to xbee.
i know how to control servos and most everything hardware-related, but can't find much on the software side. for example, i would like to drive with arrow keys, but can find very little documentation on this, and the code i did find didn't make sense to me.
can someone show me some simple processing code, that, for example, would light different led's on an arduino for different arrows pressed? i could definitely get a big start from that. thanks so much.
Logged

where am I?

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i would like to drive with arrow keys, but can find very little documentation on this
Have you installed Processing? Have you looked at the zillions of examples? File + Examples... + Basics + Input + Keyboard, for instance.

Modify that to print the index of the key that was pressed. Then, when all you see is a ? for the arrow keys, select the function name (keyPressed) and select Find in Reference on the help menu.

Look at the related links. keyCode, for instance. Print the keyCode, instead of the key. Notice that the arrow keys do have unique values. You can then send those values to the Arduino, and have it interpret them as "Go left", "Go right", "Speed up", and "Slow down" or whatever makes sense.

Quote
and the code i did find didn't make sense to me.
What code was that?
Logged

The present
Offline Offline
Newbie
*
Karma: 0
Posts: 20
nom nom nom
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks Paul. however, I don't know how to program in processing,so I'm a little lost here... smiley-confuse this is what I don't quite understand.

Quote
Have you installed Processing? Have you looked at the zillions of examples? File + Examples... + Basics + Input + Keyboard, for instance.
yes. this doesn't demonstrate use of arrow keys, though.

Quote
Modify that to print the index of the key that was pressed. Then, when all you see is a ? for the arrow keys, select the function name (keyPressed) and select Find in Reference on the help menu.
Look at the related links. keyCode, for instance. Print the keyCode, instead of the key.
this is exactly what i don't understand how to do. I guess I'm not as good with processing as i thought smiley-lol
Quote
What code was that?
the examples in processing. should have been more clear here. thanks.
Logged

where am I?

The present
Offline Offline
Newbie
*
Karma: 0
Posts: 20
nom nom nom
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is my processing code i patched together:
Code:
import processing.serial.*;
Serial myport;
int val;

void Setup()
{
  size(200,200);
  String portName = Serial.list()[4];
  myport = new Serial(this, portName, 9600);
}

void draw() {
  background(255,255,0,255);
}

void keyPressed() {
  if (key == CODED){
    if (key == UP) {
      myport.write('F');
    }
    else if (key == DOWN){
      myport.write('B');
    }
    else if (key == LEFT){
      myport.write('L');
    }
    else if (key == RIGHT){
      myport.write('R');
    }
    else{
      myport.write('S');
    }
  }
}
and arduino:
Code:
#include <Servo.h>
Servo Left;
Servo Right;
//int Lspeed = 0;
//int Rspeed = 0; 
 int val;
 
 void setup() {
 Left.attach(12);
 Right.attach(13);
 Serial.begin(9600);
 }
 
 void loop() {
 if (Serial.available()) {
 val = Serial.read();
 }
 if (val == 'F') { // If Forward command was received
 Left.write(180); // go forward
 Right.write(0);  //invert direction for opposite motors
 }
 else if (val == 'B') {
   Left.write(0);
   Right.write(180);
 }
  else if (val == 'R') {
   Left.write(0);
   Right.write(0);
 }
 else if (val == 'L') {
   Left.write(180);
   Right.write(0);
 }
  else  {
   Left.write(90);
   Right.write(90);
 }
 delay(100); // Wait 100 milliseconds for next reading
 }
should this work? unfortunately I don't have any 360 degree servos to test this with at the moment... smiley-roll-blue
Anyone can feel free to test this- just connect your servos to pins 12 and 13.
Logged

where am I?

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
should this work?

You can always test the arduino code by sending the characters using the arduino serial monitor. You can also send the generated servo command values back to the serial monitor to see if you get what you expect. You might also need to write the pc program such that a pressed character is sent, and a different released character is sent when the key is released.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess I missed the print() and println() statements in the Processing code that demonstrate that CODED, UP, DOWN, etc. are valid names and that they correspond to the arrow keys.
Logged

The present
Offline Offline
Newbie
*
Karma: 0
Posts: 20
nom nom nom
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey, so i finally got my servos, and tried to run the code connected over usb, but got this error in processing:

Code:
Exception in thread "Animation Thread" java.lang.NullPointerException
at serial_control.keyPressed(serial_control.java:52)
at processing.core.PApplet.handleKeyEvent(Unknown Source)
at processing.core.PApplet.dequeueKeyEvents(Unknown Source)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)

can anyone tell me what is wrong? It doesn't list a code line that the error occurs in, so I have no idea what to fix. also the rx led on the arduino isn't blinking, so it obviously isn't recieving anything (right?). thanks.
Logged

where am I?

The present
Offline Offline
Newbie
*
Karma: 0
Posts: 20
nom nom nom
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wow... 4 days and no reply? is this just some weird error that no one knows how to fix? what could I do to get around this error?
thanks
Logged

where am I?

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

wow... 4 days and no reply? is this just some weird error that no one knows how to fix? what could I do to get around this error?
thanks

What issues are you having with your arduino code? Have you tried a processing forum for your processing issues?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

The present
Offline Offline
Newbie
*
Karma: 0
Posts: 20
nom nom nom
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would this be more appropriate in another forum? I didn't want to start a new one when i already made this one. and i'm not having arduino issues, but I may after i get this bug fixed, so i wanted to stick to a single topic. thanks.
Logged

where am I?

Pages: [1]   Go Up
Jump to: