Go Down

Topic: Direct Code for Multi Servo Project? (Read 96 times) previous topic - next topic

tstko365

Was hoping someone can help a newb out. For the purpose of my voice control project I need to know how to send a direct code for my multi servo bluetooth project. I'm not new to arduino but I'm no expert at writing the codes. Can someone help me understand what would be a direct code to make the servos move?


#include <SoftwareSerial.h>

#include <Servo.h>
Servo myservo1, myservo2, myservo3, myservo4, myservo5, myservo6;

int bluetoothTx = 7;
int bluetoothRx = 8;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  myservo4.attach(3);
  myservo5.attach(5);
  myservo6.attach(6);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
    Serial.println(realservo);
   
    if (realservo >= 1000 && realservo <1180){
    int servo1 = realservo;
    servo1 = map(servo1, 1000,1180,0,180);
    myservo1.write(servo1);
    Serial.println("servo 1 ON");
    delay(10);

    }
   
    if (realservo >=2000 && realservo <2180){
      int servo2 = realservo;
      servo2 = map(servo2,2000,2180,0,180);
      myservo2.write(servo2);
      Serial.println("servo 2 On");
      delay(10);
     
    }
   
    if (realservo >=3000 && realservo < 3180){
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180,0,180);
      myservo3.write(servo3);
      Serial.println("servo 3 On");
      delay(10);
    }
    if (realservo >=4000 && realservo < 4180){
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180,0,180);
      myservo4.write(servo4);
      Serial.println("servo 4 On");
      delay(10);
    }
   
    if (realservo >=5000 && realservo < 5180){
      int servo5 = realservo;
      servo5 = map(servo5, 5000, 5180,0,180);
      myservo5.write(servo5);
      Serial.println("servo 5 On");
      delay(10);
    }
   
    if (realservo >=6000 && realservo < 6180){
      int servo6 = realservo;
      servo6 = map(servo6, 6000, 6180,0,180);
      myservo6.write(servo6);
      Serial.println("servo 6 On");
      delay(10);
    }
 
  }


}

12Stepper

You use the phrase "direct code" a few times. What does that mean?

I need to know how to send a direct code for my multi servo bluetooth project.

Can someone help me understand what would be a direct code to make the servos move?


slipstick

From my point of view "myservo1.write(servo1);" is code to make the servos move and I don't see how it's going to get more direct than that.

Steve

zoomkat

"For the purpose of my voice control project I need to know how to send a direct code for my multi servo bluetooth project"

Are you wanting to direct servo operation by voice command?
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

wvmarle

First of all, here are some very useful resources for you. Please read it, and when done go back to your post and add code tags.

Next, have a look at this tutorial.

Quote
Code: [Select]

 if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos;
This goes horribly wrong the moment you miss a byte, and there's no way to recover as your code is oblivious to this problem.

Quote
Code: [Select]

    int servo1 = realservo;
    servo1 = map(servo1, 1000,1180,0,180);
    myservo1.write(servo1);
A much more efficient way of doing this (map() uses floating point math!):
Code: [Select]

    myservo1.write(realservo - 1000);


Oh, and do get rid of those silly delay(10) calls.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up