Go Down

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

WallAppAlpha

Aug 05, 2018, 12:09 am Last Edit: Aug 07, 2018, 05:04 am by Coding Badly
I have a code here meant to control 2 servos through an app by sending serial codes I'm having issues with getting it to work in using aada fruit trinket 3v a hm-10 BT component and 2 linear servos I think I may not have defined the pinouts for the Rx tx I hope this is the issue but if there is anything else i need help figuring it out.

Code: [Select]


#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)
#include <SoftwareSerial.h>

#define SERVO0PIN 0
#define SERVO1PIN 1

int moveAmount = 1;  // change this value to change speed of servo
int servoPos = 0;  // variable for servo position
const int x=0;
const int y=1;
int w =1001;
SoftwareSerial BTSerial(x,y); //RX|TX

Adafruit_SoftServo myServo1;
Adafruit_SoftServo myServo2;//create servo object
   
void setup() {
  BTSerial.begin(9600); // default baud rate
  // 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(SERVO0PIN);
  myServo2.attach(SERVO1PIN);   // Attach the servo to pin 0 on Trinket
  myServo1.write(90);
  myServo2.write(5);            // Tell servo to go to position per quirk
  delay(15);                    // Wait 15ms for the servo to reach the position
}
 
void loop()  {
  if(BTSerial.available()== w)
    myServo2.write(10);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.available()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.available()== w * 2)
    myServo2.write(20);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.available()== w * 7) 
      myServo2.write(0);
      delay(20);   
  if(BTSerial.available()== w *3)
    myServo2.write(30);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.available()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.available()== w * 4)
    myServo2.write(40);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.available()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.available()== w *5)
    myServo2.write(50);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.available()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.available()== w * 6)
    myServo2.write(6w);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.available()== w  * 7) 
      myServo2.write(0);
      delay(20);
}
 
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();
  }
}


Moderator edit: [code] [/code] tags added.

Delta_G

#1
Aug 05, 2018, 01:18 am Last Edit: Aug 05, 2018, 01:18 am by Delta_G
Code: [Select]
#define SERVO0PIN 0
#define SERVO1PIN 1


And

Code: [Select]
myServo1.attach(SERVO0PIN);
  myServo2.attach(SERVO1PIN);


So the Servos use pins 0 and 1


AND:

Code: [Select]
const int x=0;
const int y=1;
int w =1001;
SoftwareSerial BTSerial(x,y); //RX|TX


SoftwareSerial will use pins 0 and 1.


Do you see the problem now?


Have you not tried to hook this up yet?  Once you realize that you're trying to connect the servos and the bluetooth to the same pins it should dawn on you. 
|| | ||| | || | ||  ~Woodstock

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

Delta_G

Code: [Select]
// SoftwareServo (works on non PWM pins)

So does the normal servo library.  It uses one of the PWM timers, but works on any pin. 
|| | ||| | || | ||  ~Woodstock

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

Delta_G

#3
Aug 05, 2018, 01:21 am Last Edit: Aug 05, 2018, 01:22 am by Delta_G
Code: [Select]
if(BTSerial.available()== w)

w is defined in your code as 1001.  Your serial buffer is only 64 bytes.  You won't ever have a thousand bytes available to read.  The receive buffer would overflow way before you got to a thousand.  You need to rethink this. 

Actually, it looks like you may have available() and read() confused.  Perhaps you should take some time to have a look at Serial Input Basics
|| | ||| | || | ||  ~Woodstock

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

WallAppAlpha


WallAppAlpha

Having issues controlling 2  servos with trinket through the app using hm-10 BT component

I have a code here meant to control 2 servos through an app by sending serial codes I'm having issues with getting it to work in using aada fruit trinket 3v a hm-10 BT component and 2 linear servos I think I may not have defined the pinouts for the Rx tx I hope this is the issue but if there is anything else i need help figuring it out.

Code: [Select]

#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)
#include <SoftwareSerial.h>

#define SERVO0PIN 3
#define SERVO1PIN 4

int moveAmount = 1;  // change this value to change speed of servo
int servoPos = 0;  // variable for servo position
const int x=0;
const int y=1;
int w =1001;

SoftwareSerial BTSerial(x,y); //RX|TX

Adafruit_SoftServo myServo1;
Adafruit_SoftServo myServo2;//create servo object
   
void setup() {
  BTSerial.begin(9600); // default baud rate
  // 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(SERVO0PIN);
  myServo2.attach(SERVO1PIN);   // Attach the servo to pin 0 on Trinket
  myServo1.write(90);
  myServo2.write(5);            // Tell servo to go to position per quirk
  delay(15);                    // Wait 15ms for the servo to reach the position
}
 
void loop()  {
  if(BTSerial.read()== w)
    myServo2.write(10);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.read()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.read()== w * 2)
    myServo2.write(20);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.read()== w * 7) 
      myServo2.write(0);
      delay(20);   
  if(BTSerial.read()== w *3)
    myServo2.write(30);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.read()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.read()== w * 4)
    myServo2.write(40);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.read()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.read()== w *5)
    myServo2.write(50);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.read()== w * 7) 
      myServo2.write(0);
      delay(20);
  if(BTSerial.read()== w * 6)
    myServo2.write(6w);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // waits 15ms for the servo to reach the position
    if(BTSerial.read()== w  * 7) 
      myServo2.write(0);
      delay(20);
}
 
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();
  }
}


Moderator edit: [code] [/code] tags added.

cattledog

Code: [Select]
SoftwareSerial BTSerial(x,y); //RX|TX
const int x=0;
const int y=1;


Quote
think I may not have defined the pinouts for the Rx tx
The constructor for SoftwareSerial is written from the perspective of the AVR controller. It needs to be cross connected with the BT. The BT tx should go the software serial rx, and the BT rx should go to the software serial tx.

So, bt tx to pin 0 and bt rx to pin 1.

WallAppAlpha


WallAppAlpha

#8
Aug 07, 2018, 01:21 am Last Edit: Aug 07, 2018, 05:03 am by Coding Badly
I have been trying all weekend to get my app working with my device i feel like its the BT code but im not sure
Please send help?

Code: [Select]

#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)
#include <SoftwareSerial.h>

#define SERVO0PIN 3
#define SERVO1PIN 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
int servoPos = 0;  // variable for servo position
int w =1001;

SoftwareSerial BTSerial(0,1)); //RX|TX
 
Adafruit_SoftServo myServo1;
Adafruit_SoftServo myServo2;//create servo object
   
void setup() {
  BTSerial.begin(9600); // default baud rate
  // 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(SERVO0PIN);
  myServo2.attach(SERVO1PIN);   // Attach the servo to pin 0 on Trinket   
}
 
void loop(){
  if(BTSerial.read()== w)
    myServo2.write(90);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // Slot 1
    if(BTSerial.read()== w * 7) 
      myServo2.write(90);
      delay(20);
  if(BTSerial.read()== w * 2)
    myServo2.write(102);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // Slot 2
    if(BTSerial.read()== w * 7) 
      myServo2.write(90);
      delay(20);   
  if(BTSerial.read()== w *3)
    myServo2.write(114);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // Slot 3
    if(BTSerial.read()== w * 7) 
      myServo2.write(90);
      delay(20);
  if(BTSerial.read()== w * 4)
    myServo2.write(126);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // Slot 4
    if(BTSerial.read()== w * 7) 
      myServo2.write(90);
      delay(20);
  if(BTSerial.read()== w *5)
    myServo2.write(138);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // Slot 5
    if(BTSerial.read()== w * 7) 
      myServo2.write(90);
      delay(20);
  if(BTSerial.read()== w * 6)
    myServo2.write(150);
    delay(20);
    myServo1.write(servoPos);                 
    servoPos = servoPos + moveAmount;
    if (servoPos == 0 || servoPos == 180){
    moveAmount = -moveAmount;
    }
    delay(15);                              // Slot 6
    if(BTSerial.read()== w  * 7) 
      myServo2.write(90);
      delay(20);
}
 
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();
  }
}


Moderator edit: [code] [/code] tags added.

cattledog

Quote
could it be something else?
Quote
I have a code here meant to control 2 servos through an app by sending serial codes
Code: [Select]
int w =1001;

Then there are several instances like this
 
Code: [Select]
if(BTSerial.read()== w)
if(BTSerial.read()== w * 2)
if(BTSerial.read()== w * 7) 


What is the app sending? What "serial codes"?

BTSerial.read() reads one byte/char. I'm not sure about what you are sending and I strongly doubt that the code shown will recognize what you send.

Can you run a basic turn an led on and off sketch to validate the bluetooth/phone and trinket set up?




Coding Badly

#10
Aug 07, 2018, 05:08 am Last Edit: Aug 07, 2018, 05:09 am by Coding Badly

@WallAppAlpha, stop cross-posting.  Threads merged.  Twice.


WallAppAlpha

im just sending a list of numbers to actvate the function i re did it and it still isnt working



#include <Adafruit_SoftServo.h>  // SoftwareServo (works on non PWM pins)
#include <SoftwareSerial.h>

#define SERVO0PIN 3
#define SERVO1PIN 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
int servoPos = 0;  // variable for servo position
char w;

SoftwareSerial BTSerial(1,0); //RX|TX
 
Adafruit_SoftServo myServo1;
Adafruit_SoftServo myServo2;//create servo object
   
void setup() {
  BTSerial.begin(9600); // default baud rate
  while (!BTSerial)
  // 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(SERVO0PIN);
  myServo2.attach(SERVO1PIN);   // Attach the servo to pin 0 on Trinket   
}
 
void loop(){
  if(BTSerial.available()) 
    w = BTSerial.read();
    if(w == '1001')
      myServo2.write(90);
      delay(20);
      myServo1.write(servoPos);                 
      servoPos = servoPos + moveAmount;
      if (servoPos == 0 || servoPos == 180){
      moveAmount = -moveAmount;
    }
      delay(15);                              // Slot 1
      if(w == '7007') 
        myServo2.write(90);
        delay(20);
    if(w == '2002')
      myServo2.write(102);
      delay(20);
      myServo1.write(servoPos);                 
      servoPos = servoPos + moveAmount;
      if (servoPos == 0 || servoPos == 180){
      moveAmount = -moveAmount;
      }
      delay(15);                              // Slot 2
      if(w == '7007') 
        myServo2.write(90);
        delay(20);   
    if(w == '3003')
      myServo2.write(114);
      delay(20);
      myServo1.write(servoPos);                 
      servoPos = servoPos + moveAmount;
      if (servoPos == 0 || servoPos == 180){
      moveAmount = -moveAmount;
      }
      delay(15);                              // Slot 3
      if(w == '7007') 
        myServo2.write(90);
        delay(20);
    if(w == '4004')
      myServo2.write(126);
      delay(20);
      myServo1.write(servoPos);                 
      servoPos = servoPos + moveAmount;
      if (servoPos == 0 || servoPos == 180){
      moveAmount = -moveAmount;
      }
      delay(15);                              // Slot 4
      if(w == '7007') 
        myServo2.write(90);
        delay(20);
    if(w == '5005')
      myServo2.write(138);
      delay(20);
      myServo1.write(servoPos);                 
      servoPos = servoPos + moveAmount;
      if (servoPos == 0 || servoPos == 180){
      moveAmount = -moveAmount;
      }
      delay(15);                              // Slot 5
      if(w == '7007') 
        myServo2.write(90);
        delay(20);
    if(w == '6006')
      myServo2.write(150);
      delay(20);
      myServo1.write(servoPos);                 
      servoPos = servoPos + moveAmount;
      if (servoPos == 0 || servoPos == 180){
      moveAmount = -moveAmount;
      }
      delay(15);                              // Slot 6
      if(w == '7007') 
        myServo2.write(90);
        delay(20);
  }
 
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();
  }
}

AWOL

#12
Aug 07, 2018, 09:28 pm Last Edit: Aug 07, 2018, 09:28 pm by AWOL
Code: [Select]
  if(w == '1001')Do you know what single quotes denote?

Clearly not.

Please take a look at Robin2's excellent serial handling basics tutorial.

And.... please remember to use code tags when posting code.

WallAppAlpha

would you recommend a different approach? and if so what would that approach be?

cattledog

Quote
would you recommend a different approach? and if so what would that approach be?
Send single character control commands.

Go Up