Adding on/off button functionality to bluetooth servo.

Discussion in 'Programmer's Corner' started by jgreene44, Feb 6, 2017.

  1. jgreene44

    Thread Starter Member

    Dec 8, 2016
    107
    2
    Hello,
    Thanks in advance for being here. The title pretty much says it. What the situation is. I have the Bluetooth servo setup going no problem.
    However. One of the servos is set for continuous rotation. it will not stay at a halted position for long. It will stop. but i guess the tiny bit of
    miscommunication is causing it to drift and start moving. Im wondering how can I add a button to this sketch that will turn that pin/servo off.
    getting it setup in MIT app inventor I think I can handle. It is the code that is killing me. I have tried a bunch of stuff, no luck. Here is the code
    I am working with:

    (this code is setup and working properly, I just need to devise a way to make that one servo stop completely.)

    #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);
    }

    }


    }

    I'm sure the ways I tried where plenty ignorant. So I won't bother to add that info. However I did find this bit of code that I was trying to utilize:\

    String voice;
    void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600);
    }
    void loop() {
    while (Serial.available())
    {
    delay(10);
    char c = Serial.read();
    voice += c;
    }
    if (voice.length() > 0)
    {
    Serial.println(voice);
    if(voice == "on")
    {
    digitalWrite(13, HIGH);
    }
    if(voice == "off")
    {
    digitalWrite(13, LOW);
    }
    voice="";
    }
    }

    I know someone is going to say this is easy. I simply am not knowledgeable enough yet. I am working on it!
     
Loading...