!!!FAULTY WIRE!!!
![]()
Now it finally works!
I'm attaching the final code (although I have a few revision thoughts) for the benefit of all.
This is a Focus Pulling mechanism that attaches to a lens and allows one to record the focus change and repeat it each time
a shot is being taken.
Thank you very much for your kind help and patience!
This is a great forum!
![]()
// WORKING SKETCH//
/*
Focus Pulling/Recordin/Playing Back
Adi Soffer
4.4.12
*/
#include <Servo.h>
#include <Bounce.h>
Servo myservo;
unsigned int potpin = 0;
unsigned int val;
uint8_t waitForServo = 5; //delay time:let servo get to position
uint8_t lastPosition;
//Defining Modes
int mode;
#define FREE 1
#define RECORD 2
#define PLAYBACK 3
#define STOP 4
#define REWIND 5
#define RECORDCONT 6
//Variables for Record and Playback
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
//Defining Switches
#define fBUTTON 7 //Free Mode Button
#define rBUTTON 8 //Record Button
#define hBUTTON 9 //Home Button
#define pBUTTON 10 //Play Button
//Defining LEDs
#define rLED 3 //Record LED
#define hLED 4 // Home LED
#define pLED 5 //Play LED
#define fLED 6 // Free Mode LED
//initial button states
int fbuttonValue = LOW; //Free button Value
int rbuttonValue = LOW; //Record button value
int hbuttonValue = LOW; //Home button value
int pbuttonValue = LOW; //Play button value
//Defining Bounce objects
Bounce fbouncer = Bounce(fBUTTON, 10); //Free Mode Bouncer
Bounce rbouncer = Bounce(rBUTTON, 10); //Record Bouncer
Bounce hbouncer = Bounce(hBUTTON, 10); //Home Bouncer
Bounce pbouncer = Bounce(pBUTTON, 10); //Play Bouncer
void setup() {
Serial.begin(9600);
Serial.flush();
myservo.attach (2);
pinMode(fBUTTON,INPUT);
pinMode(rBUTTON,INPUT);
pinMode (hBUTTON, INPUT);
pinMode (pBUTTON, INPUT);
pinMode (pLED, OUTPUT);
pinMode (hLED, OUTPUT);
pinMode(fLED,OUTPUT);
pinMode(rLED,OUTPUT);
}
void loop() {
//Record and Play Calculated
deltaPos = abs (outPos - inPos);
deltaTime = (outTime - inTime);
servoTime = (deltaTime/deltaPos);
if ( fbouncer.update() ) { //if FREE Button pressed
if ( fbouncer.read() == HIGH) {
if ( fbuttonValue == LOW ) {
fbuttonValue = HIGH;
Serial.println ("Free In");
mode = FREE;
}
else {
fbuttonValue = LOW;
Serial.println ("Free Out");
mode = STOP;
}
}
}
else if ( rbouncer.update() ) { //if REC Button pressed
if ( rbouncer.read() == HIGH) {
if ( rbuttonValue == LOW ) {
rbuttonValue = HIGH;
Serial.println ("Record In");
mode = RECORD;
}
else {
rbuttonValue = LOW;
Serial.println ("Record Out");
mode = STOP;
}
}
}
else if ( hbouncer.update() ) { //if REVERSE Button pressed
if ( hbouncer.read() == HIGH) {
if ( hbuttonValue == LOW ) {
hbuttonValue = HIGH;
Serial.println ("Rewinding");
mode = REWIND;
}
else {
hbuttonValue = LOW;
Serial.println ("Rewinding done");
mode = STOP;
}
}
}
else if ( pbouncer.update() ) { //if PLAY Button pressed
if ( pbouncer.read() == HIGH) {
if ( pbuttonValue == LOW ) {
pbuttonValue = HIGH;
Serial.println ("Play IN");
mode = PLAYBACK;
}
else {
pbuttonValue = LOW;
Serial.println ("Play done");
mode = STOP;
}
}
}
/*
DEFINING CASES
*/
switch (mode){
case FREE: //<<<FREE Focus Pulling>>>//
digitalWrite(fLED, HIGH);
val = analogRead (potpin);
val = map(val, 0, 1023, 0, 179);
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: //<<<RECORD Focus Pulling>>>//
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: //<<<RECORDING CONTINUES>>>//
digitalWrite (rLED, HIGH);
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 STOP:
digitalWrite (rLED, LOW);
digitalWrite (fLED, LOW);
digitalWrite (pLED, LOW);
digitalWrite (hLED, LOW);
//check up
Serial.print ("IN POSITION: ");
Serial.println (inPos);
Serial.print ("OUT POSITION: ");
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 ("TOTAL REC TIME: ");
Serial.println (deltaTime);
break;
case REWIND: //still need to build rewind from lastPosition in free mode
digitalWrite (hLED, HIGH);
if (outPos > inPos)
{
for (int i=outPos;i>inPos;i--)
{
myservo.write (i);
delay (20);
Serial.println (i); // print values for checking
}
hbuttonValue = LOW;
digitalWrite (hLED, LOW);
mode = STOP;
break;
}
else if (outPos<inPos)
{
for (int i=outPos;i<inPos;i++)
{
myservo.write (i);
delay (20);
Serial.println (i); // print values for checking
}
hbuttonValue = LOW;
digitalWrite (hLED, LOW);
mode = STOP;
break;
}
case PLAYBACK:
digitalWrite (pLED, HIGH);
if (outPos>inPos)
{
for (int i=inPos; i< outPos; i++)
{
myservo.write (i);
lastPosition = i;
delay (servoTime);
Serial.println (i); //print val for checking
}
pbuttonValue = LOW;
digitalWrite (pLED, LOW);
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
}
pbuttonValue = LOW;
digitalWrite (pLED, LOW);
mode = STOP;
break;
}
}
}