Hello,
I've got a program that uses three push buttons, every push button does a different thing to control a servo motor.
When the first button is pressed, the servo motor moves from position 0 to 180 and goes back to 0 every two seconds.
When the second button is pressed, I can control the servo with a potentiometer and when the third button is pressed, I control it with a force sensitive resistor.
Everything works pretty much ok, but when I'm on the first mode, I need to keep pressing another button until it changes to another mode,I know that's because there's a delay of 2 seconds. However is there anyway to keep always checking the state of the buttons to change the mode at any moment?
I'm going to show you just the code of the servo motor going from 0 to 180 since everything else works ok.
/*
simple debounce using delay, will block for 5mSec when state changes
*/
#include <Servo.h>
Servo myservo1; //Servomotor
const uint32_t debounceTime = 20; // 5 mSec, enough for most switches
const uint8_t switchCPM = 5; // with n.o. momentary pb switch to ground
const uint8_t switchPot = 6; // with n.o. momentary pb switch to ground
const uint8_t switchFSR = 7; // with n.o. momentary pb switch to ground
const uint8_t ledCPM = 2; // led
const uint8_t ledPot = 3; // led
const uint8_t ledFSR = 4; // led
const uint8_t FSR = 1; // Force Sensitive Resistor
const uint8_t Pot = 0; // Potentiometer
int force; // the analog reading from the FSR resistor divider
const bool switchOn = false; // using INPUT_PULLUP
const bool switchOff = true;
bool lastStateCPM = switchOff;
bool newStateCPM = switchOff;
bool lastStatePot = switchOff;
bool newStatePot = switchOff;
bool lastStateFSR = switchOff;
bool newStateFSR = switchOff;
bool toggleStateCPM = false;
bool toggleStatePot = false;
bool toggleStateFSR = false;
void setup()
{
Serial.begin ( 9600 );
myservo1.attach(10); // pin the servo is connected to
myservo1.write(0);
pinMode ( switchCPM, INPUT_PULLUP );
pinMode ( ledCPM, OUTPUT );
pinMode ( switchPot, INPUT_PULLUP );
pinMode ( ledPot, OUTPUT );
pinMode ( switchFSR, INPUT_PULLUP );
pinMode ( ledFSR, OUTPUT );
} // setup
void loop ()
{
newStateCPM = digitalRead( switchCPM );
newStatePot = digitalRead( switchPot);
newStateFSR = digitalRead( switchFSR);
if ( lastStateCPM != newStateCPM ) // state changed
{
delay( debounceTime );
lastStateCPM = newStateCPM;
// push on, push off
if ( newStateCPM == switchOn && toggleStateCPM == false )
{
toggleStateCPM = true;
digitalWrite( ledCPM, HIGH );
Serial.println( F ( "Switched ON CPM" ) );
toggleStatePot = false;
digitalWrite( ledPot, LOW );
Serial.println( F ( "Switched OFF Pot" ) );
toggleStateFSR = false;
digitalWrite( ledFSR, LOW );
Serial.println( F ( "Switched OFF FSR" ) );
while (toggleStateCPM == true)
{
myservo1.write(0);
newStatePot = digitalRead( switchPot);
newStateFSR = digitalRead( switchFSR);
newStateCPM = digitalRead( switchCPM );
if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
toggleStateCPM = false;
digitalWrite( ledCPM, LOW );
Serial.println( F ( "Switched OFF CPM" ) );
}
delay(2000);
newStatePot = digitalRead( switchPot);
newStateFSR = digitalRead( switchFSR);
newStateCPM = digitalRead( switchCPM );
if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
toggleStateCPM = false;
digitalWrite( ledCPM, LOW );
Serial.println( F ( "Switched OFF CPM" ) );
}
myservo1.write(179);
newStatePot = digitalRead( switchPot);
newStateFSR = digitalRead( switchFSR);
newStateCPM = digitalRead( switchCPM );
if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
toggleStateCPM = false;
digitalWrite( ledCPM, LOW );
Serial.println( F ( "Switched OFF CPM" ) );
}
delay(2000);
newStatePot = digitalRead( switchPot);
newStateFSR = digitalRead( switchFSR);
newStateCPM = digitalRead( switchCPM );
if ( newStateFSR == switchOn && toggleStateFSR == false || newStatePot == switchOn && toggleStatePot == false ) {
toggleStateCPM = false;
digitalWrite( ledCPM, LOW );
Serial.println( F ( "Switched OFF CPM" ) );
}
}
}
else if ( newStateCPM == switchOn && toggleStateCPM == true )
{
toggleStateCPM = false;
digitalWrite( ledCPM, LOW );
Serial.println( F ( "Switched OFF CPM" ) );
}
} // if state CPM changed