hello guys!
I am working on controlling 2 servos from the serial monitor thorugh commands like these:
PP ----> returns the pan position
PP1500 ----> moves the servo to position 1500
I posted my code below
#include <Servo.h>
char buffer[16];
int bufferIndex = 0;
Servo myServo1;
Servo myServo2;
void setup()
{
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
myServo1.attach(9);
myServo2.attach(8);
Serial.begin(9600);
Serial.println("Welcome!");
}
void loop()
{
static int val = 0;
static int temp = 150;
if( Serial.available())
{
char ch = Serial.read();
if( ch == '\r') // is this the terminating carriage return
{
bufferIndex = 0;
if (strcmp (buffer,"a") == 0) {
Serial.println("waiting for 1 second");
delay(1000);
}
if (strcmp (buffer,"pp") == 0) {
if (Serial.available())
{
char temp = Serial.read();
switch (temp)
{
case '0'...'9':
val = val * 10 + ch - '0';
break;
case 'p':
myServo1.write(val);
Serial.print("horizontal servo is set to: ");
Serial.print(val, DEC);
Serial.println(" degrees");
val = 0;
break;
}
}
else
{
Serial.print("current pan position is ");
Serial.println(val, DEC);
}
}
if (strcmp (buffer,"tp") == 0) {
Serial.print("current tilt position is ");
Serial.println(val, DEC);
}
if (strcmp (buffer," ") == 0) {
val = 90;
myServo1.write(val);
myServo2.write(val);
Serial.println("both servos are reset");
val = 0;
}
for (int i=0; i<=20; i++)
{
buffer[i] = '\0';
}
}
else
buffer[ bufferIndex++ ] = ch; // add the character into the buffer
}
}
the first part works fine which is returning the pan position, but the second part where you tell the servo to move does not seem to work.
please help me and guide me to where i should make modifications in the code considering that i am new to Arduino.
thanks in advance.