Here is the code again updated.
What I think I'm trying to do is to say that if the fbutton state is HIGH than the mode should be FREE, and if the fbutton state is low, the mode should be STOP.
And just to make my point clear: it works until I add the third instance - i.e the hbounce that is commented out here.
// working so far//
#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
//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;
}
//digitalWrite(fLED,fledValue);
}
}
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;
}
//digitalWrite(rLED,rledValue);
}
}
/*
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;
}
//digitalWrite(hLED,hledValue);
}
}
*/
switch (mode){
case FREE:
digitalWrite(fLED, 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:
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 (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);
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;
}
}
}