I have a code that is working with my project, I only needa toggle switch but I have one when you hold the button, the servo stops moving, but when I release, it continues to move. My code is listed here:
#include <Servo.h>
Servo servo;
int angle = 10;
int switchstate = 1;
void setup() {
servo.attach(8);
servo.write(angle);
}
void loop()
{
if (switchstate = 1);
// scan from 0 to 180 degrees
for(angle = 10; angle < 180; angle++)
{
servo.write(angle);
delay(15);
}
// now scan back from 180 to 0 degrees
for(angle = 180; angle > 10; angle–)
{
servo.write(angle);
delay(15);
}
}
if (switchstate = 1);
Two mistakes in 1 line
(1) = assigns values, == compares values
(2) the only code that depends for its execution on the result of the if is the semicolon at the end. Remove it
Quite apart from those look at your code in your post. Does it look like that in the IDE ?
It seems that you did not read this before posting a programming question
As to your question, have a look at the StateChangeDetection example in the IDE
Try this:
#include <Servo.h>
#define TIME_SERVO_STEP 15
#define TIME_READ_SWITCH 50
Servo servo;
//servo angle
int angle = 10;
//record of the last switch change state
byte lastSwitch;
//flag that is toggled each switch press; false = servo stopped, true = servo moves
bool bServoActive;
const byte pinServo = 8;
const byte pinSwitch = 2; //use whatever pin you've connected to the switch
void setup()
{
//setup the switch pinMode; pressing switch should ground the pin
pinMode( pinSwitch, INPUT_PULLUP );
//establish the current switch state as the "last" state
lastSwitch = digitalRead( pinSwitch );
//initialize the servo to "inactive"
bServoActive = false;
servo.attach( pinServo );
servo.write( angle );
}//setup
void ReadSwitch( void )
{
static unsigned long
timeSwitch = 0;
unsigned long
timeNow;
byte
currSwitch;
//read the switch every 50mS
timeNow = millis();
if( (timeNow - timeSwitch) < TIME_READ_SWITCH )
return;
timeSwitch = timeNow;
//read the current state of the switch
currSwitch = digitalRead( pinSwitch );
//has it changed from the last state? ...
if( currSwitch != lastSwitch )
{
//...yes; make the new state the last state...
lastSwitch = currSwitch;
//if the current reading is low we just recorded a button press
if( currSwitch == LOW )
{
//toggle the state of the servo-active flag
if( bServoActive )
bServoActive = false;
else
bServoActive = true;
}//if
}//if
}//ReadSwitch
void loop()
{
static bool
bServoDirection = false;
static unsigned long
timeServo = 0;
unsigned long
timeNow;
//execute this every pass; switch is actually read every 50mS
ReadSwitch();
//we'll move the servo once every 15mS
if( (timeNow - timeServo) >= TIME_SERVO_STEP )
{
timeServo = timeNow;
//if the servo flag is active...
if( bServoActive )
{
//move it according to the direction flag
if( bServoDirection )
{
//if the angle is less than 180, bump it; otherwise, toggle the flag to start
//it moving in the other direction
if( angle < 180 )
angle++;
else
bServoDirection = false;
}//if
else
{
//if the angle is greater than 10, decrement it; otherwise, toggle the flag
//to start it moving in the other direction
if( angle > 10 )
angle--;
else
bServoDirection = true;
}//else
servo.write(angle);
}//if
}//if
}//loop