Hello again!
I'm in the process of adapting my code to work with switches.
I'm using the Bounce library and building on its "change" sketch to help me work out the different "cases".
If you'll be kind enough to check out my code, you'll notice that BOTH REWIND sections commented out.
That is because the code works great until I add the "else if" section referring to the rewind mode (hbouncer).
Even with the REWIND case commented out and just with the "else if" section active (to check the flow), things get erratic.
I push the fButton and things go weird.
I'd be thankful for someone's practiced eye.
// Servo Recording - Switch Version//
#include <Servo.h>
#include <Bounce.h>
Servo myservo;
unsigned int potpin = 0;
unsigned int val;
uint8_t waitForServo = 5; //delay time to 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
int fledValue = LOW; //Free led Value
int rledValue = LOW; //Record led value
int hledValue = LOW; //Home led value
int pledValue = LOW; //Play led 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 ( fledValue == LOW ) {
fledValue = HIGH;
Serial.println ("Free In");
mode = FREE;
}
else {
fledValue = LOW;
Serial.println ("Free Out");
mode = STOP;
}
digitalWrite(fLED,fledValue);
}
}
else if ( rbouncer.update() ) { //if REC Button pressed
if ( rbouncer.read() == HIGH) {
if ( rledValue == LOW ) {
rledValue = HIGH;
Serial.println ("Record In");
mode = RECORD;
}
else {
rledValue = LOW;
Serial.println ("Record Out");
mode = STOP;
}
digitalWrite(rLED,rledValue);
}
}
/*
<<<<<<<<<<<<<<<<<<<<<This is the part that messes everything up>>>>>>>>>>>>>>>
else if ( hbouncer.update() ) { //if Reverse Button pressed
if ( hbouncer.read() == HIGH) {
if ( hledValue == LOW ) {
hledValue = HIGH;
Serial.println ("Rewinding");
mode = REWIND;
}
else {
hledValue = LOW;
Serial.println ("Rewinding done");
mode = STOP;
}
digitalWrite(hLED,hledValue);
}
}
*/
switch (mode){
case FREE:
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:
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:
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:
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: //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 (15);
Serial.println (i); // print values for checking
}
hledValue = LOW;
digitalWrite (hLED, LOW);
mode = STOP;
break;
}
else if (outPos<inPos)
{
for (int i=outPos;i<inPos;i++)
{
myservo.write (i);
delay (15);
Serial.println (i); // print values for checking
}
hledValue = LOW;
digitalWrite (hLED, LOW);
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
}
mode = STOP;
break;
}
}
}