Go Down

Topic: [ASK] Control Standart Servo From PC using Serial Monitor (Read 2023 times) previous topic - next topic

thehoster

hi,
this is my first topic,
i'm newbie on this stuff, so i need a lot of help :D
i using  1 Arduino Uno  , 1 Motor Shield  , and 2 Standard Servo 

i want to control 2 standart servo using PC from USB,
such as forward,backward and stop from Serial Monitor

my problem is my coding, it is not works
this is my coding, please check

Quote


#include <Servo.h>
char val;
const int RIGHTSPEED = 255; 
const int LEFTSPEED  = 255;
int motorR = 5;       
int dirmotorR = 4;   
int motorL = 6;     
int dirmotorL = 7;

void setup() {
  Serial.begin(9600);         
  Serial.println("Kontrol dari Serial Monitor!");
}


void loop() {
  if (Serial.available()){
    val= Serial.read();
  }
  if (val == 'maju'){
  Serial.print("bot maju");
      RMotor(RIGHTSPEED,true);
      LMotor(LEFTSPEED,true); 
    delay(1000);
  } else if (val == 'mundur'){
  Serial.print("bot mundur");
      RMotor(RIGHTSPEED,false);
      LMotor(LEFTSPEED,false); 
       delay(1000);
  }else if (val == 'berhenti') {
 
  Serial.print("bot berhenti");
    RMotor(0,false);
    LMotor(0,false);
      delay(1000);
  }
 
}



void RMotor(int pwmspeed, boolean forward) {
  analogWrite(motorR, pwmspeed);
  if (forward) {
    digitalWrite(dirmotorR, HIGH);
  }
  else {
    digitalWrite(dirmotorR, LOW);
  }
}


void LMotor(int pwmspeed, boolean forward) {
  analogWrite(motorL, pwmspeed);
  if (forward) {
    digitalWrite(dirmotorL, HIGH);
  }
  else {
    digitalWrite(dirmotorL, LOW);
  }
}






i try to give command on Serial Monitor but its not works
any help will appreciate
thanks before


zoomkat

Test code for two servos.

Code: [Select]

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// 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("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill
    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 = servo1.toInt();
      int n2 = servo2.toInt();
     
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}


Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

thehoster

#2
Feb 10, 2012, 09:35 am Last Edit: Feb 10, 2012, 09:46 am by thehoster Reason: 1
thanks for replied,
i change my code val to single character,
and its worked

but i have other problems, can i use this coding on BT SHIELD?
i using Stackable Bluetooth Shield : BT Shield v2.2 , DAT ( D0:TX , D1:RX)


Quote


#include <Servo.h>
char val;
const int RIGHTSPEED = 255; 
const int LEFTSPEED  = 255;
int motorR = 5;       
int dirmotorR = 4;   
int motorL = 6;     
int dirmotorL = 7;

void setup() {
  Serial.begin(9600);         
  Serial.println("Kontrol dari Serial Monitor! W->Maju, A->Kiri, D->Kanan,S->Mundur,Z->Stop");
}


void loop() {
  if (Serial.available()){
   
  val= Serial.read();
  }
  if (val == 'w'){
      RMotor(RIGHTSPEED,true);
      LMotor(LEFTSPEED,true); 
    delay(1000);
  } else if (val == 's'){
      RMotor(RIGHTSPEED,false);
      LMotor(LEFTSPEED,false); 
       delay(1000);
  }   else if (val == 'a'){
      RMotor(RIGHTSPEED,true);
      LMotor(LEFTSPEED,false); 
       delay(1000);
  }     else if (val == 'd'){
         RMotor(RIGHTSPEED,false);
      LMotor(LEFTSPEED,true);   
       delay(1000);
  }else if (val == 'z') {
    RMotor(0,false);
    LMotor(0,false);
      delay(1000);
  }
}



void RMotor(int pwmspeed, boolean forward) {
  analogWrite(motorR, pwmspeed);
  if (forward) {
    digitalWrite(dirmotorR, HIGH);
  }
  else {
    digitalWrite(dirmotorR, LOW);
  }
}


void LMotor(int pwmspeed, boolean forward) {
  analogWrite(motorL, pwmspeed);
  if (forward) {
    digitalWrite(dirmotorL, HIGH);
  }
  else {
    digitalWrite(dirmotorL, LOW);
  }
}





dxw00d

Your thread title is asking about controlling servos, but your sketch (which should be in code tags, not quote) is controlling motors using PWM. Which do you actually want to do?

thehoster


Your thread title is asking about controlling servos, but your sketch (which should be in code tags, not quote) is controlling motors using PWM. Which do you actually want to do?


sorry, my bad habit,
i want to controlling servos, using BT Shield but,
my code only working using USB,
can you help me to fix it so its work using BT shield?

dxw00d

If the BT shield uses the standard Arduino hardware serial pins, then it should work fine in place of the USB connection. But, your code has nothing to do with controlling servos.

thehoster


If the BT shield uses the standard Arduino hardware serial pins, then it should work fine in place of the USB connection. But, your code has nothing to do with controlling servos.


i using this BT Shield http://iteadstudio.com/store/index.php?main_page=product_info&products_id=468
but its not work,
my code work fine using USB , its can Turn Right, Turn Left, Forward and Backward
my problem is if i connect with BT shield and doing control, its fail- _-
nothing happen

dxw00d

Just for the record then, you are using motors, not servos.

How are the jumpers set on the shield?
What position is the mode switch in?
Do you know what baud rate it is set to? According to the datasheet, it defaults to 38400, but your sketch has 9600.

What are you using to send data from?

thehoster


Just for the record then, you are using motors, not servos.

How are the jumpers set on the shield?
What position is the mode switch in?
Do you know what baud rate it is set to? According to the datasheet, it defaults to 38400, but your sketch has 9600.

What are you using to send data from?


thanks for fast replied
iam sorry for that, yes iam using motors

this my sample jumper:


position swith DAT

i upload data (sketch) using USB to arduino
when i testing, i plug out USB and connect to BT shield using TeraTerm(http://ttssh2.sourceforge.jp/) on 38400
i thought baud rate only on pc side, i dont know on coding side must be 38400

dxw00d

If I'm reading the datasheet correctly, that is the UART baud rate, meaning the speed that it uses for the serial port side of the shield. As you are using the hardware serial pins, you might be able to open the serial monitor, and see the data coming in. Try changing the Serial.begin(9600) to Serial.begin(38400) and setting the speed in serial monitor to the same.

thehoster

thank you very much,
its work, much thanks to you

my robot running now

thanks again :D

Go Up