//test program #include Servo Pinky; Servo Ring; Servo Index; Servo Pointer; Servo Thumb; int val; int val1; int val2; int val3; int val4; float filterVal; // this determines smoothness - .0001 is max 1 is off (no smoothing) float smoothedVal; // this holds the last loop value just use a unique variable for every different sensor that needs smoothing float smoothedVal1; float smoothedVal2; float smoothedVal3; float smoothedVal4; void setup() { Serial.begin(9600); Pinky.attach(7); Ring.attach(6); Index.attach(5); Pointer.attach(4); Thumb.attach(3); } void loop() { val = analogRead(0); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 179); val1 = analogRead(1); // reads the value of the potentiometer (value between 0 and 1023) val1 = map(val1, 0, 1023, 0, 179); val2 = analogRead(2); // reads the value of the potentiometer (value between 0 and 1023) val2 = map(val2, 0, 1023, 0, 179); val3 = analogRead(3); // reads the value of the potentiometer (value between 0 and 1023) val3 = map(val3, 0, 1023, 0, 179); val4 = analogRead(4); // reads the value of the potentiometer (value between 0 and 1023) val4 = map(val4, 0, 1023, 0, 179); filterVal = 0.05; smoothedVal = smooth(val, filterVal, smoothedVal); // second parameter determines smoothness - 0 is off, .9999 is max smooth smoothedVal1 = smooth(val1, filterVal, smoothedVal1); // second parameter determines smoothness - 0 is off, .9999 is max smooth smoothedVal2 = smooth(val2, filterVal, smoothedVal2); // second parameter determines smoothness - 0 is off, .9999 is max smooth smoothedVal3 = smooth(val3, filterVal, smoothedVal3); // second parameter determines smoothness - 0 is off, .9999 is max smooth smoothedVal4 = smooth(val4, filterVal, smoothedVal4); // second parameter determines smoothness - 0 is off, .9999 is max smooth if (smoothedVal > 120) { Pinky.write(60); Serial.print("smoothed val: "); Serial.print(smoothedVal); Serial.print(" "); Serial.print(val); Serial.print(" "); Serial.println("60"); } else if(smoothedVal<60) { Pinky.write(180); Serial.print("smoothed val: "); Serial.print(smoothedVal); Serial.print(" "); Serial.print(val); Serial.print(" "); Serial.println("180"); } delay(15); if (smoothedVal1 > 120) { Ring.write(60); } else if(smoothedVal1<60) { Ring.write(180); } delay(15); if (smoothedVal2 > 120) { Index.write(60); } else if(smoothedVal2<60) { Index.write(180); } delay(15); if (smoothedVal3 > 120) { Pointer.write(60); } else if(smoothedVal3<60) { Pointer.write(180); } delay(15); if (smoothedVal4 > 120) { Thumb.write(60); } else if(smoothedVal4<60) { Thumb.write(180); } delay(15); } int smooth(int data, float filterVal, float smoothedVal){ if (filterVal > 1){ // check to make sure param's are within range filterVal = .99; } else if (filterVal <= 0){ filterVal = 0; } smoothedVal = (data * (1 - filterVal)) + (smoothedVal * filterVal); return (int)smoothedVal; } int smooth1(int data, float filterVal, float smoothedVal1){ if (filterVal > 1){ // check to make sure param's are within range filterVal = .99; } else if (filterVal <= 0){ filterVal = 0; } smoothedVal1 = (data * (1 - filterVal)) + (smoothedVal1 * filterVal); return (int)smoothedVal1; } int smooth2(int data, float filterVal, float smoothedVal2){ if (filterVal > 1){ // check to make sure param's are within range filterVal = .99; } else if (filterVal <= 0){ filterVal = 0; } smoothedVal2 = (data * (1 - filterVal)) + (smoothedVal2 * filterVal); return (int)smoothedVal2; } int smooth3(int data, float filterVal, float smoothedVal3){ if (filterVal > 1){ // check to make sure param's are within range filterVal = .99; } else if (filterVal <= 0){ filterVal = 0; } smoothedVal3 = (data * (1 - filterVal)) + (smoothedVal3 * filterVal); return (int)smoothedVal3; } int smooth4(int data, float filterVal, float smoothedVal4){ if (filterVal > 1){ // check to make sure param's are within range filterVal = .99; } else if (filterVal <= 0){ filterVal = 0; } smoothedVal4 = (data * (1 - filterVal)) + (smoothedVal4 * filterVal); return (int)smoothedVal4; }