Hi, I am very new to arduino and electronics, I have this issue with a project of mine.
I have a flash blob detection program that sends data to Processing which then passes to an Arduino mega 2560. The Arduino then gives out orders to 36 Futaba S3001 servos. The servos will need to point at specific degrees based on the data received, which is within 0-180.
For 18 of the servos, it just needs to swing back and forth, I just need to send it an on/off, but the other 18 servos I need to send 3-digit numbers to give them a specific angle to point at. Which means I need to send around 18x3 (3-digit number each) plus 18x1 (1-digit for on/off as 'T' or 'F') which is 72 characters each time.
I tried it out with 10 servos, but to prevent data drop, I had to give it a pretty large delay for it to pack up the characters before it does anything, so the feedback of the servos were really choppy. It even just stopped working after a while.
The servos have an external DC power source of 6V.
Would really appreciate any kind of help.
Here is the code
#include <Servo.h>
//_______________________________
//---- store data from processing
char inData[5];
char inChar = -1;
byte index = 0;
char temp[3];
//===============================
//_______________________________
//---------------------- cat data
const int catNum = 5;
int currCat;
int maxHandPos = 170;
int minHandPos = 10;
boolean realData;
//===============================
//_______________________________
//-------------------- servo data
Servo assServo[catNum];
int assServoPin[catNum];
int assPosition[catNum];
// - - - - - - - - - - - - - - -
boolean handShakeBool[catNum];
Servo handServo[catNum];
int handServoPin[catNum];
int handPosition[catNum];
int handSpeed = 1;
//===============================
//_______________________________
//----------------------- testing
int ledPin[catNum];
//===============================
// FUNCTION ___________________________________________
//-----------------------------------------------------
void setup(){
Serial.begin(57600);
currCat = -1;
realData = false;
// - - - - - - - - - - - - - - - - - - - - -
/*for(int i=0; i<catNum; i++){
assServoPin[i] = i+13;
assServo[i].attach(assServoPin[i]);
assPosition[i] = 90;
// - - - - - - - - - - - - - - - -
handShakeBool[i] = false;
handServoPin[i] = i+1;
handServo[i].attach(handServoPin[i]);
handPosition[i] = 0;
}
// - - - - - - - - - - - - - - - - - - - - -
shakeHandFunc();*/
for(int i=0; i<catNum; i++){
ledPin[i] = i+3;
pinMode(ledPin[i], OUTPUT);
digitalWrite(ledPin[i], LOW);
}
}
void loop(){
readData();
if(strlen(inData) == 5){
if(inData[4] == 'T' || inData[4] == 'F'){
// - - - - - - - - - - - - - - - - - - - - -
switch(inData[0]){
case 'A':
currCat = 0;
realData = true;
break;
// - - - - - - - - -
case 'B':
currCat = 1;
realData = true;
break;
// - - - - - - - - -
case 'C':
currCat = 2;
realData = true;
break;
// - - - - - - - - -
case 'D':
currCat = 3;
realData = true;
break;
// - - - - - - - - -
case 'E':
currCat = 4;
realData = true;
break;
}
// - - - - - - - - - - - - - - - - - - - - -
if(realData && currCat != -1){
realData = false;
assPosition[currCat] = setPos(inData);
if(inData[4] == 'T'){
handShakeBool[currCat] = true;
}else{
handShakeBool[currCat] = false;
}
assServo[currCat].write(assPosition[currCat]);
currCat = -1;
}
// - - - - - - - - - - - - - - - - - - - - -
}
}
clearChar(inData);
delay(100);
}
void shakeHandFunc(){
for(int i=0; i<catNum; i++){
if(handShakeBool[i]){
if(handPosition[i] > maxHandPos){
handPosition[i]-=handSpeed;
}
if(handPosition[i] < minHandPos){
handPosition[i]+=handSpeed;
}
}else{
handPosition[i] = 90;
}
handServo[i].write(handPosition[i]);
}
delay(100);
}
void readData(){
while(Serial.available() > 0)
{
if(index < 5)
{
inChar = Serial.read();
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
int setPos(char* tempChar){
for(int i=0; i<3; i++){
temp[i] = tempChar[i+1];
}
return (atoi(temp));
clearChar(temp);
}
void clearChar(char* charTemp){
for(int i=0; i<strlen(charTemp); i++){
charTemp[i] = 0;
}
}