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.
#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: </mark> <mark>[code]</mark> <mark>
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.
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
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.
#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: </mark> <mark>[code]</mark> <mark>
SoftwareSerial BTSerial(x,y); //RX|TX
const int x=0;
const int y=1;
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.
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
}
I just tried it seems like I still have no luck could it be the code? and if it is what could it be? I'm new to Arduino so this is a process for me but thank you for the help
As mentioned earlier, take a big step backwards and write a simple program to turn an led on and off. This will simplify the issues with the phone/bluetooth , trinket and basic serial communications. Introduce the servos and the timer after the basics are solid.