Control servos through app trouble

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.

do you think it was the loop? i dont think its an apple thing it seems to single down to the code the blink test workder great but ill will test things out and see what happens

do you think it was the loop?

I really don't see how the code can be interactive with the app, but for now there is a mystery. The most important this is to get back to something that works and we can build on.

Unfortunately, I thought we did that after the first blink code success. :frowning:

I don't have it independently powered yet so I plug it in via the USB do you think I'm getting serial interference or may that be the reason I get the extra data? I just ran the code and it works its just really laggy due to the random serial digits

or do you think possibly the timmer at the bottom is the one producing the data and lagging up my program?

I don't have it independently powered yet so I plug it in via the USB do you think I'm getting serial interference or may that be the reason I get the extra data?

I'm not totally familiar with the Trinket, but here the reference on the pinout

The soft servo libary sets the usb pins to outputs, so it should be OK

USB Pins
The next 2 pins are also used for USB programming. That means that when the Trinket is connected to a computer and in bootloader mode or in the middle of uploading a new program, they are used for sending data to/from the computer! It's possible to share these pins if you are careful. The best use of these pins is as outputs to things like LEDs. We didn't want to keep these pins off the board but we strongly recommend not using them unless you're sure you need them since you might have to disconnect any connections to reprogram the Trinket!

It's certainly worth trying to power the Trinket not through the USB connector. Or, just modify a usb cable to only contain power and ground (or perhaps a cell phone charging cable might only have the two wires).

I just ran the code and it works its just really laggy due to the random serial digits

I asked you to run the blink code. Is it now "laggy" and showing the mystery screen output?

do you think possibly the timmer at the bottom is the one producing the data and lagging up my program?

You seem to be getting ahead of yourself. The timer code should not be in the blink code.

If the blink code works without "spam" the timer can be the first thing put back in. If the blink code runs with spam we'll need to back up a bit to find out why. The app should not be producing extra data.

Sorry I meant that the blink code ran fine but as I ran my code for the servos they tended to run laggy due to the extra data being transmitted ik going to try with an independent power source I feel that that is the root of the problem because my code works and is responsive just laggy and the BT issue

the blink code ran fine but as I ran my code for the servos they tended to run laggy due to the extra data being transmitted

Great you now have a place to start and you add back small pieces of the servo code until something breaks. Keeps us informed about what you find.

It certainly can't hurt to try an independent power source. If one end of the usb cable is into the Trinket, what is the other end currently attached to? What is downloading the program to the Trinket when you are in boot mode? Is it by any change an ipad, iphone, or a Mac terminal which is also running the wireless BT?

For clarity, and perhaps some possible interaction due to reserved names in the ide, you may want to rename the Software serial instance to something like BT_Serial. However, since the blink example works with the SoftwareSerial Serial(0,1) call, it may not be the root cause of the problem.