@zoomKat,
thanks for the suggestion. but the arduino module i am using (ArduinoBT) has an in-built bluetooth component and so it is basically impossible to seperate the two physically(at least to the best of my knowledge).
@fliggygeek,
here are the best codes. pls note that the final aim of the code is to drive 8 servos...so you will see 8 variables set up to achieve this. enjoy
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
unsigned int FrontServo1 =0X41; //
unsigned int FrontServo2 =0X42;
unsigned int BackServo3 =0X43;
unsigned int BackServo4 =0X44;
unsigned int PanelServo5 =0X45;
unsigned int MainServo6 =0X46;
unsigned int MainServo7 =0X47;
unsigned int MainServo8 =0X48;
//these variables won't change:
const int Midpoint=732; //variable to determine angle midway of servo extremes
const int rate = 100; //Variable to determine the rate of turning of servo motors
const int panelPin = 2;
//these variables will change
int bufferIn =0; // Variable to store incoming byte
byte pinState = 0;
int position1 = Midpoint;
int position2 = Midpoint;
int position3 = Midpoint;
int position4 = Midpoint;
int position5 = Midpoint;
int position6 = Midpoint;
int position7 = Midpoint;
int position8 = Midpoint;
void setup()
{
Serial.begin (9600); //Set USART Baud Rate to 115200
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(9600);
pinMode (panelPin, INPUT); //set panel pin as input
send_cmd(FrontServo1, position1,0); /Set the Default position of all servos in order to avoid floating/
send_cmd(FrontServo2, position2,0);
send_cmd(BackServo3, position3,0);
send_cmd(BackServo4, position4,0);
send_cmd(PanelServo5, position5,0);
send_cmd(MainServo6, position6,0);
send_cmd(MainServo7, position7,0);
send_cmd(MainServo8, position8,0);
}
void loop()
{
if (Serial.available()) { // Look for char in serial que and process if found
bufferIn= Serial.read(); //store char in variable
switch (bufferIn)
{
case '1': //if char is '1'
main_Forward(); //move main body forward
break;
case '2': //if char is '2'
main_Reverse(); //move main body in reverse
break;
case '3': //if char is '3'
main_Right(); //rotate main body to the right
break;
case '4': //if char is '4'
main_Left(); //rotate main body to the left
break;
case '5': //if char is '5'
while (digitalRead(panelPin) == LOW) //check the state of the panel safety pin
{
main_PanelDown(); // safety pin not triggered, launch main panel
}
main_PanelUp();
break;
case 'w': //if char is 'w'
front_Up(); //move front flipper upwards
break;
case 's': //if char is 's'
front_Down(); //move front flipper downwards
break;
case 'd': //if char is 'd'
front_Right(); //move front flipper to the right
break;
case 'a': //if char is 'a'
front_Left(); //move front flipper to the left
break;
case 'i': //if char is 'i'
back_Up(); //move back flipper upwards
break;
case 'k': //if char is 'k'
back_Down(); //move back flipper downwards
break;
case 'j': //if char is 'j'
back_Left(); //move back flipper to the left
break;
case 'l': //if char is 'l'
back_Right(); //move back flipper to the right
break;
case 'x': //if char is 'x'
Stop();
break;
default:
Serial.println("invalid command");
}
}
}
void front_Up()
{
if (position1=1463) //check if servo is at max angle,
{
send_cmd(FrontServo1, position1,0); // yes, Servo stays at the same position
}
else
{
position1=position1 + rate; //No, increase angle according to rate
send_cmd(FrontServo1, position1,0);
delay(15); // waits 15ms for the servo to reach the position
}
}
void front_Down()
{
if (position1=0) //check if servo is at min angle,
{
send_cmd(FrontServo1, position1,0); // yes, Servo stays at the same position
}
else
{
position1=position1 - rate; //No, Decrease angle according to rate
send_cmd(FrontServo1, position1,0);
delay(15); // waits 15ms for the servo to reach the position
}
}
void front_Right()
{
if (position2=1463) //check if servo is at max angle,
{
send_cmd(FrontServo2, position2,0); // yes, Servo stays at the same position
}
else
{
position2=position2 + rate; //No, increase angle according to rate
send_cmd(FrontServo2, position2,0);
delay(15); // waits 15ms for the servo to reach the position
}
}
void front_Left()
{
if (position2=0) //check if servo is at min angle,
{
send_cmd(FrontServo2, position2,0); // yes, Servo stays at the same position
}
else
{
position2=position2 - rate; //No, Decrease angle according to rate
send_cmd(FrontServo2, position2,0);
delay(15); // waits 15ms for the servo to reach the position
}
}
.
.
.
. //same procedure repeated for the rest of the servos
.
.
.
void send_cmd(unsigned int num, unsigned int data, unsigned int ramp)
//UART transmit 4 bytes: servo number, higher byte position,
//lower byte position and speed
{
unsigned char higher_byte=0, lower_byte=0;
higher_byte=(data>>6)&0x003f;
lower_byte=data&0x003f;
mySerial.print(num); //First byte is the servo channel 0x41-0x60
delay(2000);
mySerial.print(higher_byte); //second byte is the higher byte of position
//0b00xxxxxx
delay(2000);
mySerial.print(lower_byte); //third byte is the lower byte of position 0b00xxxxxx
delay(2000);
mySerial.print(ramp); //fourth byte is the speed value from 0-63
delay(2000);
toggle(13);
}
void toggle(int pinNum) {
// set the LED pin using the pinState variable:
digitalWrite(pinNum, pinState);
// if pinState = 0, set it to 1, and vice versa:
pinState = !pinState;
}