Control servos through app trouble

You were doing well with the use of code tags. Don't stop now!

Here's some things I see:

1)You had this in the working code for basic bluetooth connection

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10
SoftwareSerial Serial(0,1); //RX|TX

You have changed the Software Serial constructor in the latest code. Are you connected to the correct pins?

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10
SoftwareSerial Serial(1,0); //RX|TX

2)You need to refresh both servos in the Timer ISR. Did you not see this in the example?

myServo1.refresh();
myServo2.refresh();

3)You need opening and closing braces to define the if conditional tests for the values of the received char. Without defining the block to be executed, you only execute the next line. In the simple BT test sketch you only had one line following the if() so it worked. It's a good habit to develop to always use braces to define the conditional block, even if its one line.

  1. I'm not sure if the w == 'z' code makes any sense where you write the constant position in the for loop().

You can try this, but if it doesn't move the servos as you want, I'd take a step backwards and write more simple servo moves in response to the BT commands.

#include <Adafruit_SoftServo.h>
#include <SoftwareSerial.h>

#define SERVO1PIN 3
#define SERVO2PIN 4

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10


int moveAmount = 1;  // change this value to change speed of servo


SoftwareSerial Serial(0,1); //RX|TX

Adafruit_SoftServo myServo1, myServo2;


void setup() {
  Serial.begin(9600);
  while (!Serial);
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)

  myServo1.attach(SERVO1PIN);
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 0 on Trinket

  myServo1.write(12);
  myServo2.write(12);
}

void loop()
{
  if (Serial.available() > 0)
  {
    char w = Serial.read();
    int pos = 90;
    int pos2 = 0;
    if (w == 'a')
    { //add brace
      for (pos = 0; pos <= 102; pos += 1)
      {
        myServo2.write(pos);
        delay(20);
      }
      for (pos2 = 0; pos2 <= 180; pos2 += 1)
      {
        myServo1.write(pos2);
        pos2 = pos2 + moveAmount;
        if (pos2 == 0 || pos2 == 180) {
          moveAmount = -moveAmount;
        }
      }
      delay(15);
    }//add brace
    if (w == 'z')
    { //add brace
      for (pos = 0; pos <= 102; pos += 1)
      {
        myServo2.write(90);
        delay(20);

      }
    }//add brace
  }
}
// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo1.refresh();
    myServo2.refresh();//add this line for second servo
  }
}

actually, the code is very responsive the only issue I have now is it seems through the serial port there are a bunch of random data slowing my connection down I just started doing this with the working servo code and not with the blink code also thank you so much for all of your help

there are a bunch of random data slowing my connection down

?

it seems through the serial port there are a bunch of random data slowing my connection down

Make sure that the phone app is sending the commands to the BT with out line endings.

You should be able to echo the Serial.read() values back to the phone with Serial.write() commands and see what the "random data" is. Perhaps if you shared the findings with us, we could help.

it just seems as if data is being sent in the serial monitor and its making actual inputs delay could it be the loop in the code? and if it is what can I do about that? also, I think it may be the loop What is a way I can have the code hold and wait for input for every action

could it be the loop in the code?

What code?
I don't see any code.

oh sorry here it is everything works independently but when trying to send commands it seems as if I'm bombarded with random data idk if it's my app of iff my code is producing weird values or taking wired values

#include <Adafruit_SoftServo.h>
#include <SoftwareSerial.h>

#define SERVO1PIN 3
#define SERVO2PIN 4

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10


int moveAmount = 1;  // change this value to change speed of servo


SoftwareSerial Serial(0,1); //RX|TX

Adafruit_SoftServo myServo1, myServo2;


void setup() {
  Serial.begin(9600);
  while (!Serial);
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)

  myServo1.attach(SERVO1PIN);
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 0 on Trinket

  myServo1.write(12);
  myServo2.write(12);
}

void loop()
{
  if (Serial.available() > 0)
  {
    char w = Serial.read();
    int pos = 90;
    int pos2 = 0;
    if (w == 'a')
    { //add brace
      for (pos = 90; pos <= 102; pos += 1)
      {
        myServo2.write(pos);
        delay(2000);
      }
      for (pos2 = 0; pos2 <= 180; pos2 += 1)
      {
        myServo1.write(pos2);
        pos2 = pos2 + moveAmount;
        if (pos2 == 0 || pos2 == 180) {
          moveAmount = -moveAmount;
        }
      }
      delay(15);
    }//add brace
    if (w == 'z')
    { //add brace
      for (pos = 0; pos >= 90; pos -= 1)
      {
        myServo2.write(90);
        delay(20);

      }
    }//add brace
  }
}
// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo1.refresh();
    myServo2.refresh();//add this line for second servo
  }
}
SoftwareSerial Serial(0,1); //RX|TX

Don't name your SoftwareSerial instance with the same name as the hardware serial instance. Not sure if that's your problem, but it is bad enough to fix before going any further.

PS - Why are you running a software serial on the hardware serial pins? If you're going to use those pins, then you might as well just use Serial instead of slow and buggy software serial.

but when trying to send commands it seems as if I'm bombarded with random data

Can you describe in more detail what this means?

Can you describe in words without reference to the code, what you want each servo to do when you enter either 'a' or 'z'? What moves, and at what speed?

There are problems with the code in that the for() loops are blocking new Serial input but non responsiveness is not what your are focusing on with the term "random data".

There appear to be some bugs in the code.

I think that in this section of code, the delay(15) wants to be inside the for loop

for (pos2 = 0; pos2 <= 180; pos2 += 1)
      {
        myServo1.write(pos2);
        delay(15);
        pos2 = pos2 + moveAmount;
        if (pos2 == 0 || pos2 == 180) {
          moveAmount = -moveAmount;
        }       
      }
     // delay(15);

As asked above, what do you want the motion of this servo to be? There are two places where pos2 is incremented and I don't think it produces the values were moveAmount changes sign. But again, I don't see this as a "random data" symptom.

PS - Why are you running a software serial on the hardware serial pins? If you're going to use those pins, then you might as well just use Serial instead of slow and buggy software serial.

@ Delta_G

This code is for a Trinket. The OP is not using a USB/Serial converter, but communicating with the Trinket through a Bluetooth module.

Oh, thought I saw UNO earlier. Guess I mixed it up with another thread.

Carry on then.

I want for one servo to move from 90 deg to 102 deg with the input 'a' and then after that the second motor will move from 0-180 deg then back to 0 then when input 'z' comes the first motor again will move back to the 90 degrees spot if that makes any sense and I appreciate the help and I agree I think I messed up on the servos

also, it seems when I run the program the " random data" seems to be something produced in the loop keeping a constant transmission of data without any input being implemented this seems to be the primary reason the program won't run correctly.

I have tried to make the code more consistent my using pos1 for myServo1 and pos2 for myServo2. See if this code is what you want for movements.

Speeds are controlled by both the delays in the .write() command in the for() loops as well as the refresh speeds controlled by the timer interrupts. See the source code for the Adafruit_SoftServo.h library for details.

If the increments in the for() loops occur significantly faster than the timer, the physical increments will be larger than expected and the motion might be jerky. I've set all the for loop increments at 15ms, and the timer updates the actual position every 20 ms. You may want to set the for() loop delays to greater than 20ms.

#include <Adafruit_SoftServo.h>
#include <SoftwareSerial.h>

#define SERVO1PIN 3
#define SERVO2PIN 4

#define TXPIN 0 //Connected to TX on HM-10
#define RXPIN 1 //Connected to RX on HM-10

int pos1 = 0; //position for servo1
int pos2 = 0; //position for servo2

SoftwareSerial Serial(0, 1); //RX|TX

Adafruit_SoftServo myServo1, myServo2;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  // Set up the interrupt that will refresh the servo for us automagically
  OCR0A = 0xAF;            // any number is OK
  TIMSK |= _BV(OCIE0A);    // Turn on the compare interrupt (below!)

  myServo1.attach(SERVO1PIN);
  myServo2.attach(SERVO2PIN);   // Attach the servo to pin 0 on Trinket

  myServo1.write(12); //0?
  myServo2.write(12); //90?
}

void loop()
{
  if (Serial.available() > 0)
  {
    char w = Serial.read();

    if (w == 'a')
    {
      for (pos2 = 90; pos2 <= 102; pos2 += 1)
      {
        myServo2.write(pos2);
        delay(15);
      }

      for (pos1 = 0; pos1 <= 180; pos1 += 1)
      {
        myServo1.write(pos1);
        delay(15);
      }
      
      
      for (pos1 = 180; pos1 >= 0; pos1 -= 1)
      {
        myServo1.write(pos1);
        delay(15);
      }
    }

    if (w == 'z')
    {
      for (pos2 = 102; pos2 >= 90; pos2 -= 1)
      {
        myServo2.write(pos2);
        delay(15);
      }
    }
  }
}
// We'll take advantage of the built in millis() timer that goes off
// to keep track of time, and refresh the servo every 20 milliseconds
// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be
// Called by the microcontroller every 2 milliseconds
volatile uint8_t counter = 0;
SIGNAL(TIMER0_COMPA_vect) {
  // this gets called every 2 milliseconds
  counter += 2;
  // every 20 milliseconds, refresh the servos!
  if (counter >= 20) {
    counter = 0;
    myServo1.refresh();
    myServo2.refresh();//add this line for second servo
  }
}

I'm unsure about the initial servo positioning at 12, and whether the jumps to starting positions 90 and 0 will be a problem.

also, it seems when I run the program the " random data" seems to be something produced in the loop keeping a constant transmission of data without any input being implemented this seems to be the primary reason the program won't run correctly.

How do you know this? What do you mean by "constant transmission of data". What is the app you are using to send the bluetooth data?

"I'm unsure about the initial servo positioning at 12, and whether the jumps to starting positions 90 and 0 will be a problem."

what should i have the initial servo position placed?

and to test the assembly im using BT serial pro.
as for the random data in the serial monitor i keep getting a constant stream of data

what should i have the initial servo position placed?

I have no idea. You have the hardware. Experiment. If thee is no problem jumping from 12 to 90 or 0, then don't change anything.

and to test the assembly im using BT serial pro.

Pleas provide a link to that app.

as for the random data in the serial monitor i keep getting a constant stream of data

I'm not sure about what you mean by the "serial monitor". Are you referring to the screen of the BT serial pro? If not, what are you talking about?

Can you provide a sample of the screen output?

I do mean the serial monitor on the app
also here is a video of my output its strange

im going to try to post a gif of the serial monitor so you can see what i mean

here is what im getting in the serial monitor

ezgif.com-crop.gif

WallAppAlpha:
here is what im getting in the serial monitor

Text please, not nearly 2MB of image.

here is a screen shot it just keeps running these random digits

Thank you for the image and the animation. I see what you are experiencing, but don't understand the cause.

You said that the simple iphone/BT-HM10/Trinket blink code in post #23 worked properly. You did not see the "spam" behaviour.

Can you still run that simple sketch with the app behaving properly? If not, then something in an app setting may have changed.

If the bluetooth blink sketch runs but the servo sketch doesn't, we can sequentially add small pieces of the servo code back in to see what broke.

You may also want to try a different iphone BLE terminal app. Is this what you are currently running?
https://itunes.apple.com/us/app/hm10-bluetooth-serial-pro/id1221924372?platform=iphone&preserveScrollPosition=true#platform/iphone

Unfortunately, I live in Android world, and don't have an iphone to help test anything. I'm also more familiar with BT classic and don't know if there is anything in the BLE protocols that can cause this.

Good Luck.