Hey SirNickity - I'm using your idea and something weird is happening...
It does indeed smooth the values, but on "playback mode" I get weird values at the start...
I'm attaching the code and would appreciate your observance...
//
// FILE: servoRecorder.pde
// AUTHOR: Adi Soffer
// DATE: 28-3-2012
//
// PUPROSE: servo recorder
//
#define IDLE 0
#define FREE 1
#define RECORD 2
#define PLAYBACK 3
#define STOP 4
#define REWIND 5
#define RECORDCONT 6
int mode;
int idx, lastidx, firstidx, newidx;
int maxidx = 800;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
char buffer [2]; //variable to hold keystrokes
int potpin = 0; // analog pin used to connect the potentiometer
unsigned int val; // variable to read the value from the analog pin
const unsigned int valNumber = 800; //variable to contain number of array members
int incomingByte; //declare variable to hold incoming letter
unsigned int servoPos[valNumber]; //create array of values for servo position
const int waitForServo = 15; //delay time to let servo get to position
const byte recordModeLed = 3; // defining pins for ui leds
const byte homeModeLed = 4;
const byte playModeLed = 5;
const byte freeModeLed = 6;
const byte freeSwitch = 7; //defining pins for switches
const byte recSwitch = 8;
const byte homeSwitch = 9;
const byte playSwitch = 10;
void setup()
{
Serial.begin(9600);
Serial.flush();
myservo.attach(2); // attaches the servo on pin 2 to the servo object
pinMode (freeModeLed, OUTPUT); //defining LED pins as output
pinMode (recordModeLed, OUTPUT);
pinMode (homeModeLed, OUTPUT);
pinMode (playModeLed, OUTPUT);
pinMode (freeSwitch, INPUT); //defining switch pins as input
pinMode (recSwitch, INPUT);
pinMode (homeSwitch, INPUT);
pinMode (playSwitch, INPUT);
}
void loop ()
{
digitalWrite (freeModeLed, LOW);
digitalWrite (recordModeLed, LOW);
digitalWrite (homeModeLed, LOW);
digitalWrite (playModeLed, LOW);
// HANDLE IO
if (Serial.available( ) > 0)
{
incomingByte = Serial.read ( );
switch(incomingByte)
{
case 'f':
mode = FREE;
Serial.println ("Free mode");
break;
case 'r':
mode = RECORD;
for (int c=6;c>3;c --) // three sec LED countdown to recording
{
digitalWrite (recordModeLed, HIGH);
digitalWrite (c, HIGH);
delay (700);
digitalWrite (recordModeLed, LOW);
digitalWrite (c, LOW);
delay(300);
}
Serial.println ("Record");
break;
case 's' :
mode = STOP;
Serial.println ("STOP");
break;
case 'w' :
mode = REWIND;
digitalWrite (homeModeLed, HIGH); //turn on rewind LED
Serial.println ("Rewind");
break;
case 'p':
mode = PLAYBACK;
digitalWrite (playModeLed, HIGH); //turn on playback LED
Serial.println ("Playback");
break;
}
}
switch(mode)
{
case STOP:
for (int x=3;x>6;x++)
{
digitalWrite (x, LOW); //turn off all LEDs
}
break;
case FREE:
digitalWrite (freeModeLed, HIGH);
val = analogRead (potpin);
val = map(val, 0, 1023, 0, 179); // val = val * 45/256; // <==> val * 180/1024
myservo.write (val);
Serial.println (val); //print val for checking
newidx == val;
delay (waitForServo); // should be refactored away -> like blink without delay
break;
case RECORD:
digitalWrite (recordModeLed, HIGH); //keeps LED on
Serial.println ("Record function"); // if so - prints record
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
Serial.println (val); // print values for checking
servoPos [idx]=val; // stores val in array "servoPos"
firstidx = idx;
mode = RECORDCONT;
break;
case RECORDCONT:
digitalWrite (recordModeLed, HIGH); //keeps LED on
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
servoPos [idx]=val; // stores val in array "servoPos
idx ++ ;
lastidx = idx;
if (idx == maxidx) mode = STOP;
delay(waitForServo); // waits for the servo to get there
Serial.println(val); // print values for checking
break;
case REWIND: //need to build rewind from new point in free mode
newidx = analogRead (potpin);
if (newidx = lastidx)
{
for (int i=lastidx;i>firstidx;i--)
{
myservo.write (servoPos [i]);
val = servoPos [i];
delay (waitForServo);
Serial.println (val); // print values for checking
}
}
else //need to build rewind from new point in free mode
{
}
mode = STOP;
for (int x=3;x>6;x++)
{
digitalWrite (x, LOW); //turn off all LEDs
}
break;
case PLAYBACK:
for (int x = 0;x<lastidx - 2; x++) // smoothing values
{
servoPos [x+1] = (servoPos [x] + servoPos [x+2] ) /2;
}
for (int i=firstidx; i< lastidx; i++)
{
myservo.write (servoPos[i]);
val = servoPos [i];
delay (waitForServo);
Serial.println (val); //print val for checking
}
mode = STOP;
for (int x=3;x>6;x++) //turn off all LEDs
{
digitalWrite (x, LOW);
}
break;
}
}