I am working on a touch sensor that will control the servo motor. I have a code attached below but what it does is, when its touch it turns from 0 to 180 and when i let go it goes back from 180 to 0. But how can i make it go to 0 to 180 on the first touch and stay there . Then on the second touch go from 180 to 0 and stay there.
why i want to use if statement is that, i have a code that runs a servo from 0 to 180 when i touch a touch sensor but then when i let it go it goes from 180 to 0. I want to have an if statement that will do; when i touch the touch sensor it checks the state and go to either 0 to 180 or from 180 to 0. and pause but when i touch again, it should go reverse.
This is the code i have right now and all it does is when i touch it, it goes from 0 to 180 then when i let it go it goes from 180 to 0. SO where do i put the if statement here?
You half-answered your own question a few posts above, when you spoke of "state".... you should pursue that line of thought.
Have a look at this example which counts button presses. It does something every 4 clicks: in your case you want every "odd" press to do something and every "even" press to do something else.
Servo servoMotor;
int servoPin = 3;
int buttonPin= A0;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
misrak:
So the dititalRead values have to be either 0 and 1?
That's the whole basis of the digital world: your switch is either open or closed.
And while we're on the subject, seems you're trying to position the servo with a digitalWrite to 0 or 180 degrees. But you are also doing that with servoMotor.write() which is what you should be doing.
Servo servoMotor;
int servoPin = 3;
int buttonPin= A0;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
Sorry for all this and thank you so much for your time, i am learning alot. I just made some of the changes but it tells me it has "expected unqualified id before "." on line Servo.Write(servoPin, 1);
#include <Servo.h>
Servo servoMotor;
int servoPin = 3;
int buttonPin= A0;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button