Hello,
I am working on a project with the Arduino Mega using three servos. These are actually special-order multi-turn servos that rotate up to 6 revolutions. Here is the link to the product on Hobby King's website:
Also copied here is my code. What I am trying to do is send a character over the serial monitor and have one of the servos move according to the rules I set up for each character. For the most part, the code works and each servo behaves as expected. However, every so often, the servos just start moving on their own without any decipherable reason or pattern. Usually when this starts to happens it seems that the board just starts ignoring the input from the serial monitor and the servos continue to do their own thing.
I am somewhat a newbie so hopefully I have described my problem adequately. Any comments or help is greatly appreciated. Thank you.
#include <Servo.h> //include servo function
#define BUFFERSIZE 127
uint8_t inBuffer[BUFFERSIZE];
int inLength; // length of data in the buffer
int numLoop = 0; // number of times we looped
char val;
Servo servo_1; //for servo function
Servo servo_2; //for servo function
Servo servo_3; //for servo function
void setup() {
Serial.begin(9600); //baud rate I set for bluetooth module is 38400
servo_1.attach(2); //servo 1 at pin 2?
servo_2.attach(3); //servo 2 at pin 3?
servo_3.attach(5); //servo 3 at pin 5?
// motor_reset(); // call function to reset the motor
}
void loop() {
// read string if available
if (Serial.available()) {
inLength = 0;
while (Serial.available()) {
val = Serial.read();
}
Serial.print("Arduino received: "); //send the text to the android device
Serial.write(val);
Serial.println();
}
if (val == 'a')
{
servo_1.write(2000);
}
if (val == 'b')
{
servo_1.write(1800);
}
if (val == 'c')
{
servo_1.write(1600);
}
if (val == 'd')
{
servo_1.write(1400);
}
if (val == 'e')
{
servo_1.write(1200);
}
if (val == 'f')
{
servo_1.write(1000);
}
if (val == 'g')
{
servo_1.write(0);
}
if (val == 'h')
{
servo_2.write(2000);
}
if (val == 'i')
{
servo_2.write(1800);
}
if (val == 'j')
{
servo_2.write(1600);
}
if (val == 'k')
{
servo_2.write(1400);
}
if (val == 'l')
{
servo_2.write(1200);
}
if (val == 'm')
{
servo_2.write(1000);
}
if (val == 'n')
{
servo_2.write(0);
}
if (val == 'o')
{
servo_3.write(2000);
}
if (val == 'p')
{
servo_3.write(1800);
}
if (val == 'q')
{
servo_3.write(1600);
}
if (val == 'r')
{
servo_3.write(1400);
}
if (val == 's')
{
servo_3.write(1200);
}
if (val == 't')
{
servo_3.write(1000);
}
if (val == 'u')
{
servo_3.write(0);
}
if (val == ' ')
{
servo_1.write(0);
servo_2.write(0);
servo_3.write(0);
}
}