Hey.
For the test I'm making sure outPos>inPos.
What I get back is delayTime = 0
and no playback - naturally, if the delayTime is 0...deltaPos=0...
I'm sure I'm doing something extremely stupid here... :~
// Simple servo position recoder
// FILE: servoRecorder.pde
// AUTHOR: Adi Soffer
// DATE: 1-4-2012 B
//
// PUPROSE: servo recorder
//
#define FREE 1
#define RECORD 2
#define PLAYBACK 3
#define STOP 4
#define REWIND 5
#define RECORDCONT 6
int mode;
unsigned int maxidx = 500;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
char buffer [2]; //variable to hold keystrokes
unsigned int potpin = 0; // analog pin used to connect the potentiometer
unsigned int val; // variable to read the value from the analog pin
int incomingByte; //variable to hold incoming letter
uint8_t waitForServo = 5; //delay time to let servo get to position
uint8_t lastPosition;
unsigned long inTime; //variable to hold time when recording starts
unsigned long outTime; //variable to hold time when recording ends
unsigned long deltaTime; //var holding actual recording time
uint8_t inPos; //var to hols "in" value
uint8_t outPos; //var to hols "out" value
uint8_t deltaPos; //var to claculate num of steps
unsigned long servoTime; //var to claculate time for step in playback
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);
deltaPos = (outPos - inPos);
servoTime = (deltaTime/deltaPos);
deltaTime = (outTime - inTime);
}
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:
{
Serial.print ("inPos: "); //check change in var
Serial.println (inPos);
Serial.print ("outPos:");
Serial.println (outPos);
Serial.print ("Steps: ");
Serial.println (deltaPos);
Serial.print ("in time: ");
Serial.println (inTime);
Serial.print ("out Time: ");
Serial.println (outTime);
Serial.print ("time: ");
Serial.println (deltaTime);
stopFunction ( );
}
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
lastPosition = val; //save last position for REWIND fixing
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
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo
myservo.write(val); // sets the servo position according to the scaled value
inPos = val; //save in pos
inTime = millis ( ); //save in time
Serial.println (val); // print values for checking
mode = RECORDCONT;
break;
case RECORDCONT:
digitalWrite (recordModeLed, HIGH); // keeps LED on
val = analogRead(potpin); // reads the value of the potentiometer
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo
myservo.write(val); // sets the servo position according to the scaled value
outPos = val; //save out pos
outTime = millis ( ); //save out time
lastPosition = val;
delay(waitForServo); // waits for the servo to get there
Serial.println(val); // print values for checking
break;
case REWIND: //need to build rewind from lastPosition in free mode
if (outPos > inPos)
{
for (int i=outPos;i>inPos;i--)
{
myservo.write (i);
delay (waitForServo);
Serial.println (i); // print values for checking
}
mode = STOP;
break;
}
else if (outPos<inPos)
{
for (int i=outPos;i>inPos;i++)
{
myservo.write (i);
delay (waitForServo);
Serial.println (i); // print values for checking
}
mode = STOP;
break;
}
case PLAYBACK:
if (outPos>inPos)
{
for (int i=inPos; i< outPos; i++)
{
myservo.write (i);
//lastPosition = i;
delay (servoTime);
Serial.println (i); //print val for checking
}
mode = STOP;
break;
}
else if (outPos<inPos)
{
for (int i=inPos; i<outPos; i--)
{
myservo.write (i);
//lastPosition = i;
delay (servoTime);
Serial.println (i); //print val for checking
}
}
}
}
void stopFunction ( )
{
for (int x=3;x<6;x++)
{
digitalWrite (x, LOW); //turn off all LEDs
}
}