Sorry for the vague response before, i have finally come back to this project, properly this time and i am still encountering some minor problems. I introduced the smoothing code, although i feel as though i’ve over-complicated it, and i am running into problems where Servo 3 is completely unresponsive when controlled by potentiometer 3 buy when pot 2 is being used it has a residual effect on servo 3 .I have tested the servo, it isn’t the problem, my wiring isn’t interfering anywhere and I have also tested the potentiometer with a multimeter. And servo 2 is very unreliable and has very lagged jolting movement.
Here is my code:
#include <SoftwareSerial.h>
const int pot0Readings =10;
const int pot1Readings =10;
const int pot2Readings =10;
const int pot3Readings =10;
const int pot4Readings =10;
const int pot5Readings =10;
const int DEFAULT_BAUD = 9600;
const int SERVO_CONTROLLER_RX_PIN = 0; // The SERVO CONTROLLER'S RX PIN.
const int SERVO_CONTROLLER_TX_PIN = 1; // The SERVO CONTROLLER'S TX PIN.
int pot0 = A0; //input pins
int pot1 = A1;
int pot2 = A4;
int pot3 = A6;
int pot4 = A8;
int pot5 = A9;
int led = 13;
int readings0[pot0Readings]; // the readings from the analog input
int readings1[pot1Readings];
int readings2[pot2Readings];
int readings3[pot3Readings];
int readings4[pot4Readings];
int readings5[pot5Readings];
int readIndex0 =0; //Current position in Array
int readIndex1 =0;
int readIndex2 =0;
int readIndex3 =0;
int readIndex4 =0;
int readIndex5 =0;
int total0 =0;
int total1 =0; //the running total
int total2 =0;
int total3 =0;
int total4 =0;
int total5 =0;
int average0 =0; //the running average
int average1 =0;
int average2 =0;
int average3 =0;
int average4 =0;
int average5 =0;
SoftwareSerial ServoController = SoftwareSerial(SERVO_CONTROLLER_RX_PIN, SERVO_CONTROLLER_TX_PIN);
void setup(){
ServoController.begin(DEFAULT_BAUD);
delay(500);
Serial.begin(9600);
memset(readings0, 0, sizeof(readings0));
memset(readings1, 0, sizeof(readings1));
memset(readings2, 0, sizeof(readings2));
memset(readings3, 0, sizeof(readings3));
memset(readings4, 0, sizeof(readings4));
memset(readings5, 0, sizeof(readings5));
}
void moveServo(int ServoChannel, int target){
//656ms PWM pulse represents a servo angle of 0 degrees.
//2000ms PWM pulse represents a servo angele of 180 degrees.
byte serialBytes[4]; //Create the byte array object that will hold the communication packet.
target = (map(target, 0, 180, 656, 2000) * 4); //Map the target angle to the corresponding PWM pulse.
serialBytes[0] = 0x84; // Command byte: Set Target.
serialBytes[1] = ServoChannel; // First byte holds channel number.
serialBytes[2] = target & 0x7F; // Second byte holds the lower 7 bits of target.
serialBytes[3] = (target >> 7) & 0x7F; // Third byte holds the bits 7-13 of target.
ServoController.write(serialBytes, sizeof(serialBytes)); //Write the byte array to the serial port.
}
void loop(){
//smoothing for servo 0
//subtract the last reading:
total0 = total0 - readings0[readIndex0];
// read from the sensor:
analogReset(); //function to clear residual analogue readings
readings0[readIndex0] = analogRead(pot0);
// add the reading to the total:
total0 = total0 + readings0[readIndex0];
// advance to the next position in the array:
readIndex0 = readIndex0 + 1;
// if we're at the end of the array...
if (readIndex0 >= pot0Readings) {
// ...wrap around to the beginning:
readIndex0 = 0;
}
// calculate the average:
average0 = total0 / pot0Readings;
Serial.println(average0);
average0 = map(average0, 0, 1023, 0, 179);
moveServo(0, (average0));
//smoothing for servo 1
total1 = total1 - readings1[readIndex1] ;
analogReset();
readings1[readIndex1] = analogRead(pot1);
total1 = total1+ readings1[readIndex1];
readIndex1 = readIndex1 + 1;
if (readIndex1 >= pot1Readings) {
readIndex1 = 0;
}
average1 = total1 / pot1Readings;
Serial.println(average1);
average1 = map(average1, 0, 1023, 0, 179);
moveServo(1, (average1));
//smoothing for servo 2
total2 = total2- readings2[readIndex2];
analogReset();
readings2[readIndex2] = analogRead(pot2);
total2 = total2 + readings2[readIndex2];
readIndex2 = readIndex2 + 1;
if (readIndex2 >= pot2Readings) {
readIndex2 = 0;
}
average2 = total2 / pot2Readings;
Serial.println(average2);
average2 = map(average2, 0, 1023, 0, 179);
moveServo(2, (average2));
//smoothing for servo 3
total3 = total3 - readings3[readIndex3];
analogReset();
readings3[readIndex3] = analogRead(pot3);
total3= total3 + readings3[readIndex3];
readIndex3 = readIndex3 + 1;
if (readIndex3 >= pot3Readings) {
readIndex3 = 0;
}
average3 = total3/ pot3Readings;
Serial.println(average3);
average3 = map(average3, 0, 1023, 0, 179);
moveServo(3, (average3));
//smoothing for servo 4
total4 = total4 - readings4[readIndex4];
analogReset();
readings4[readIndex4] = analogRead(pot4);
total4 = total4 + readings4[readIndex4];
readIndex4 = readIndex4 + 1;
if (readIndex4 >= pot4Readings) {
readIndex4 = 0;
}
// calculate the average:
average4 = total4 / pot4Readings;
Serial.println(average4);
average4 = map(average4, 0, 1023, 0, 179);
moveServo(4, (average4));
//smoothing for servo 5
total5 = total5 - readings5[readIndex5];
analogReset();
readings5[readIndex5] = analogRead(pot5);
total5 = total5 + readings5[readIndex5];
readIndex5 = readIndex5 + 1;
if (readIndex5 >= pot5Readings) {
readIndex5 = 0;
}
// calculate the average:
average5 = total5 / pot5Readings;
Serial.println(average5);
average5 = map(average5, 0, 1023, 0, 179);
moveServo(5, (average5));
digitalWrite(led, HIGH);
}
void analogReset(){
#if defined(ADMUX)
#if defined(ADCSRB) && defined(MUX5)
// Code for the ATmega2560 and ATmega32U4
ADCSRB |= (1 << MUX5);
#endif
ADMUX = 0x1F;
// Start the conversion and wait for it to finish.
ADCSRA |= (1 << ADSC);
loop_until_bit_is_clear(ADCSRA, ADSC);
#endif
}