CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« on: August 09, 2012, 11:27:40 am » |
Good morning everyone,
I am new to Arduino and have a question about the serial communication with the Arduino. I have looked at quite a few examples and forums, but I am having some trouble understanding. I am still testing, using the Arduino and Motor Shield to control two DC motors via Serial Monitor to send commands. My question is, is there any way in which I can send commands to the Arduino without having to open the Serial Monitor and press "Enter" when I want to send data? I want to be able to just press the "f" key and have my car move forward, rather than having to send one command at a time, pressing "Enter" for each command.
I hope I have been as clear as possible. I'll post my code along with this question.
Parts I currently am using for this project: -New Bright RC Car (Ford Mustang GT) -Arduino Uno BT -Arduino Motor Shield R3 -Laptop (OS Windows 7) -XBOX 360 Controller
|
|
|
|
« Last Edit: August 09, 2012, 11:30:02 am by Vendiga »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: August 09, 2012, 12:03:15 pm » |
My question is, is there any way in which I can send commands to the Arduino without having to open the Serial Monitor and press "Enter" when I want to send data? I want to be able to just press the "f" key and have my car move forward, rather than having to send one command at a time, pressing "Enter" for each command. Using the serial monitor, no. Writing your own application that sends data to the serial port is easy, though. Make it send data when a key is pressed, and again when the key is released. That way, your car can go forward while the f key is pressed, and stop when it is released. Or, just send when the key is pressed, and have another key (or any subsequent key) for "stop doing that".
|
|
|
|
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #2 on: August 09, 2012, 02:33:43 pm » |
So would I have to find a source outside of the Arduino IDE? (Create my own application) If so, any recommendations? Also, is there any example code on the tutorials page?
By the way, thank you for the quick response, much appreciated.
|
|
|
|
« Last Edit: August 09, 2012, 02:36:47 pm by Vendiga »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 1
Posts: 63
Arduino rocks
|
 |
« Reply #3 on: August 11, 2012, 01:01:09 am » |
My suggestion would be to use a Processing sketch to program the computer side of things. The Arduino IDE is actually based on Processing if I'm not mistaken. Take a look at the book "Making Things Talk" as it has several good examples of setting up serial communications with the Arduino.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 43
|
 |
« Reply #4 on: August 12, 2012, 03:52:08 pm » |
Your question seems a bit confusing, but there are a few ways you can input data to the Arduino without using the Serial Monitor: 1. Set up your own External buttons to use as controls: http://arduino.cc/it/Tutorial/Button 2. As previously suggested, use Processing to capture keyboard events and send them or other data over a serial connection like so: import processing.serial.*; //use the Serial library Serial myPort; // Create object from Serial class
void setup() { size(200, 200); //set window size to 200 by 200 pixels String portName = Serial.list()[0]; //list serial ports, save the first one as portName myPort = new Serial(this, portName, 9600); //initialize the serial port object }
void draw() { //this is like the 'loop' section of code on Arduino background(255); // set background color if(keyPressed) { //if a key is pressed, do the following if ( key == 'f' || key == 'F' ){ myPort.write(key); println(key); delay(200); } // if the key is f or F, write it to the serial port, then wait 200 miliseconds } }
3. Embed an Arduino Nano or comparablly small device in the Xbox controller to monitor joystick/button actions and send the data to the device directly, or via an RF (Radio) module. example: http://tmrh20.blogspot.ca/2012_03_01_archive.html Update: Added comments to code per request by OP
|
|
|
|
« Last Edit: August 13, 2012, 06:23:09 pm by TMRh20 »
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #5 on: August 13, 2012, 11:28:48 am » |
Just wanted to say thanks for all the advice. I'll post some updates as soon as possible. Maybe along with some photos.
|
|
|
|
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #6 on: August 13, 2012, 11:34:52 am » |
Also, TMRh20 could you please explain your code?
|
|
|
|
|
Logged
|
|
|
|
|
Greenville, IL
Offline
Edison Member
Karma: 11
Posts: 1288
Warning Novice on board! 0 to 1 chance of errors!
|
 |
« Reply #7 on: August 13, 2012, 12:41:11 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 43
|
 |
« Reply #8 on: August 13, 2012, 06:45:19 pm » |
I added some comments to the processing code above too.
|
|
|
|
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #9 on: August 13, 2012, 08:45:23 pm » |
HAHA! Wow guys thanks soo much! Much appreciated. Works perfectly with my code, so now I asm going to use the keyboard to control two (2) DC motors (both direction and speed). Then I will move onto the XBOX code. I am a little intimidated to use the XBOX controller but I have found some individuals who have done a similar project before. But thanks once again for all the help!  I will keep updating everyone on my progress. Pics should be coming soon once my internship is up. 
|
|
|
|
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #10 on: August 15, 2012, 09:31:12 am » |
Hey everyone,
Just an update, so far so good! The RC Car is moving Forward/Backward/Left/Right! However, I do have one question, is there an easier way for me to have the Processing and/or Arduino receive multiple I/O's? respectively. Right now, I plan on creating "if" statements for those cases.
ex: Processing Code: Arduino Code:
char upRight = 'E' if(input == 'E'){ if(keyCode == UP && keyCode == RIGHT){ analogWrite(pwmA, 255); analogWrite(pwmB, 255); myPort.write(upRight) digitalWrite(dirA, LOW); digitalWrite(dirB, HIGH); } }
Open to any suggestions, just want my code to be "better", so to speak. I feel this is a crude way of doing this control. Also, I want to be able to make sure that while I am driving forward, I can press on the right arrow key and it will read the two inputs (both up and right arrow key) as soon as I press it. Let me know if this is not concise.
|
|
|
|
« Last Edit: August 15, 2012, 09:34:19 am by Vendiga »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: August 15, 2012, 09:50:54 am » |
However, I do have one question, is there an easier way for me to have the Processing and/or Arduino receive multiple I/O's? Easier than what? Also, I want to be able to make sure that while I am driving forward, I can press on the right arrow key and it will read the two inputs (both up and right arrow key) as soon as I press it. You won't be able to use delay() anywhere. Not that you should want to, or should be doing so... Why would you need to have two inputs? The Processing application can see that forward and right are pressed, and send some different value. It might send 0 for forward, 1 for forward, right, 2 for right, 3 for reverse, right, 4 for reverse, etc.
|
|
|
|
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #12 on: August 15, 2012, 10:09:35 am » |
Easier than the code I had posted with my question. But I will be removing the delay() from my code (not necessary). And thanks for the quick response Paul, that's just what I needed answered!
|
|
|
|
|
Logged
|
|
|
|
|
CA
Offline
Newbie
Karma: 0
Posts: 8
|
 |
« Reply #13 on: August 21, 2012, 03:17:09 pm » |
Ok, so I was able to get it to work with the arrow keys, however, I changed my code in order to process 'Letter Keys' rather than the "Arrow Keys", my Arduino Code looks fine, but I will send both the Arduino Code and the Processing Code for any input. Please do let me know of dumb mistakes.
|
|
|
|
|
Logged
|
|
|
|
|
|