Pages: [1]   Go Down
Author Topic: Serial Communication  (Read 447 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am building a remote control robot with two aruinos(UNO), and a RF link. You put serial data in the transmitter and it comes out the reciever. I need to control two motors(with PWM). Any ideas on how to control two motors with PMW capabilities through a serial interface?
Logged

Austin, TX
Online Online
Faraday Member
**
Karma: 71
Posts: 6127
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Any ideas on how to control two motors with PMW capabilities through a serial interface?
There are tons of tutorials on how to control motors and how to communicate over serial.  So what is your specific question?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know how to control motors. I need to send two numbers, one for each of the motors, but how can I make code to tell which number(from serial connection), goes to which motor. Like... Left motor PWM 200, and right 100. How can I send serial data and differentiate the two values for the two motors?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think about how you'd tell another person two values.

I'd say, "The first value is 38 and the second value is 97." So, why don't you have your arduino do it, but with more concise phrases than "the first value is" (maybe "1" or "a")

It's also usuallly a good idea to send a signal at the start and end of each message - so your message may look like "b1[motor1value]2[motor2value]e"

Then you'd send it with Serial.write(), and read it, one letter at a time, with Serial.read().
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand that part. I need to know how to interpret and then make use of the serial data.
Logged

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

Simple test code for two servos that could be adapted for PWM for two motors with drivers.

Code:
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1); 
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
     
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
     
      int n1; //declare as number 
      int n2;
     
      char carray1[6]; //magic needed to convert string to a number
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1);
     
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2);
     
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}

Logged

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, the easiest way to convert a single character to a number, 0 to 9, (if you're SURE it's a number) is to subtract '0' from the character. Since, in ASCII, all of the numbers are in order, when you subtract the numeric representation of the character '0', you'll get 0 if the character is '0', 1 if the character is '1', and so on. From there, you can multiply the higher characters by multiples of 10 and add them together to get a final, multidigit number.
Logged

Pages: [1]   Go Up
Jump to: