Go Down

Topic: Control servos through app trouble (Read 986 times) previous topic - next topic

WallAppAlpha

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

AWOL

Quote
there are a bunch of random data slowing my connection down
?
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

cattledog

Quote
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.

WallAppAlpha

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

AWOL

Quote
could it be the loop in the code?
What code?
I don't see any code.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

WallAppAlpha

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

Code: [Select]
#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
  }
}


Delta_G



Code: [Select]
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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

cattledog

Quote
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
Code: [Select]
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.

cattledog

#38
Aug 10, 2018, 02:09 am Last Edit: Aug 10, 2018, 02:09 am by cattledog
Quote
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.

Delta_G

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

Carry on then.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

WallAppAlpha

#40
Aug 10, 2018, 04:53 pm Last Edit: Aug 10, 2018, 07:18 pm by WallAppAlpha
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.

cattledog

#41
Aug 10, 2018, 07:33 pm Last Edit: Aug 10, 2018, 07:38 pm by cattledog
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.

Code: [Select]
#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.

Quote
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?

WallAppAlpha

"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

cattledog

#43
Aug 10, 2018, 08:11 pm Last Edit: Aug 10, 2018, 08:12 pm by cattledog
Quote
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.

Quote
and to test the assembly im using BT serial pro.
Pleas provide a link to that app.

Quote
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?




WallAppAlpha

#44
Aug 10, 2018, 08:30 pm Last Edit: Aug 10, 2018, 08:35 pm by WallAppAlpha
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

Go Up