Author Topic: Serial communication PC to Arduino  (Read 5415 times)

0 Members and 1 Guest are viewing this topic.

Offline FlammableTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
Serial communication PC to Arduino
« on: January 06, 2011, 11:05:53 AM »
Hi,

I'm planning my first robot to build, and I'm currently wondering about how I should control it. I have kinda settled on using the serial port on the arduino and connecting this to the PC via a TTL to USB converter.

My question is: Can I send strings or integers from the computer and program the microcontroller to recieve them and use them to control PWM outputs???

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,935
  • Helpful? 98
Re: Serial communication PC to Arduino
« Reply #1 on: January 06, 2011, 11:13:28 AM »
Quote
My question is: Can I send strings or integers from the computer and program the microcontroller to recieve them and use them to control PWM outputs???

Yes.

Offline FlammableTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
Re: Serial communication PC to Arduino
« Reply #2 on: January 06, 2011, 11:57:29 AM »
Do you have a recommendation for a program that can run in windows and send continous simple commands like 1, 2, 3... or F, B, R, L?? Or do I have to make one myself?

I have written a small sample of what I want the program to do:
Code:
Code: [Select]
#include <iostream.h>
#include <conio.h>

int main(){
char inn;
inn=getch();
while (inn!='p'){
        switch (inn){
                case 'w':
                        while (inn=='w'){
                                cout << "FWD\n";
                                inn=getch();}
                        break;
                case 's':
                        while (inn=='s'){
                                cout << "BCK\n";
                                inn=getch();}
                        break;
                case 'a':
                        while (inn=='a'){
                                cout << "LFT\n";
                                inn=getch();}
                        break;
                case 'd':
                        while (inn=='d'){
                                cout << "RGT\n";
                                inn=getch();}
                        break;}
                }
return 0;
}
The cout function here is only a stand in for a function that sends data out through a COM port. It is important that the program sends data all the time when a key is being held down.

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,935
  • Helpful? 98
Re: Serial communication PC to Arduino
« Reply #3 on: January 06, 2011, 12:25:49 PM »
You could just just HyperTerm or one of the other terminal programs like Putty or TeraTerm.
If you hold a key down then the character will repeat and continuously send.

I just use a single character command to send to a Bot. This makes interpreting the command on the u-processor side easier.

In the code you posted you may want to use the kbhit() function to determine when you do press a key.

Does that code work the way you want?

Offline FlammableTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
Re: Serial communication PC to Arduino
« Reply #4 on: January 06, 2011, 12:38:33 PM »
Ah thanks  :) I thought that with HyperTerm, one would have to press enter for each send operation... I will use single characters for the commands, I was just too lazy to change the code.

The code works just fine, changes direction like I want it to and stops when I don't press any button. The only little thing I noticed was that there is a delay from the first key press and to the continous key pressing. It seems to be some windows thing, put there so that it's more conveniant to write stuff. Don't know if it's going to cause trouble.

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,935
  • Helpful? 98
Re: Serial communication PC to Arduino
« Reply #5 on: January 06, 2011, 12:54:52 PM »
Yep, its in the Keyboard controller chip that has a delay after a key down before repeating. This is so typing a single character is possible for us very slow typists.

There are settings to change the delay in the Windows Control Panel under Keyboard.

Offline FlammableTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
Re: Serial communication PC to Arduino
« Reply #6 on: January 06, 2011, 01:15:52 PM »
Nice to know :-) Thanks.

Offline blackbeard

  • Supreme Robot
  • *****
  • Posts: 575
  • Helpful? 4
Re: Serial communication PC to Arduino
« Reply #7 on: January 06, 2011, 02:10:46 PM »
i read this and am interested too. what if i have a real 1979 smart terminal? will that work?
"sure, you can test your combat robot on kittens... But all your going to do is make kitten juice"

First step: Build androids with AI
Next step: Give them vaginas

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,935
  • Helpful? 98
Re: Serial communication PC to Arduino
« Reply #8 on: January 06, 2011, 02:45:01 PM »
Your smart terminal should work just as well as using a terminal program on a Windows (or MAC or Linux) computer.

TeraTerm emulates one of the DEC VT terminals (the VT100 is the default).

Offline airuno2l

  • Jr. Member
  • **
  • Posts: 12
  • Helpful? 0
Re: Serial communication PC to Arduino
« Reply #9 on: January 11, 2011, 06:36:33 AM »
Since you said your new to building robots I'll give you a piece of information that I wish someone would have given me. It is best to send integers through the serial port when ever possible (a short integer is the best). They are the smallest piece of info you can send (well...kind of), and hence they send faster. This is especially usefull if your sending signal very quickly. Even if I need to send a number with 2 decimal places, I just multiply it by 100 and send it as an integer, then when the PC receives it I divide by 100.

 


Get Your Ad Here

data_list