Touch Sensor and Servo

Hello..

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.

Thank you!

Touch_sensor.ino (767 Bytes)

Hello,

How can i write an if statement for my touch sensor?

Have you looked at the reference section on the web site?
http://arduino.cc/en/Reference/If

Ya i see that, but here is the code on that site but is it the same if i want it for servo?
if (x > 120) digitalWrite(LEDpin, HIGH);

if (x > 120)
digitalWrite(LEDpin, HIGH);

if (x > 120){ digitalWrite(LEDpin, HIGH); }

if (x > 120){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
} // all are correct

If statement is just a condition. The code you presented seems to be the same thing over and over.

Just different ways of saying it.

if the action after the statement is short it can work nicely if the rest is written on the same line, otherwise
the use of

if ()
{

}

is helpful to keep everything you want done as a result of the condition.

i would suggest doing

if (x > 120)
{ 
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH); 
}

it just looks clearer to me

:frowning: 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.

How can i do that?

You would just need another if statement

so if it goes to 180

next time you touch it

it would be at 180 and you want it to reverse

if(x==180)

{

//then write some code to make it go the other way

}

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?

#include <Servo.h>

Servo servoMotor;
int servoPin = 3;

void setup() {
Serial.begin(9600);
servoMotor.attach(servoPin);
}

void loop()
{
int analogValue = analogRead(A0);
Serial.println(analogValue);

int servoAngle = map(analogValue, 0, 1023, 0, 179);

servoMotor.write(servoAngle);
}

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.

I'd look into that kind of approach.....

You seem to have 3 or so threads going on this servo/switch thing.....

It is not doing anything? DOes this look right?

#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

void setup() {
pinMode(buttonPin, INPUT);
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
buttonState= digitalRead(buttonPin);

int analogValue = analogRead(A0);
Serial.println(analogValue);

int servoAngle = map(analogValue, 0, 1023, 0, 179);

if(buttonState != lastButtonState){
if(buttonState==180){

buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else{
Serial.print("0");
}
}

lastButtonState = buttonState;

if(buttonPushCounter % 2==0){
digitalWrite(servoPin, 180);
}
else{
digitalWrite(servoPin, 0);
}

servoMotor.write(servoAngle);
}

misrak:
It is not doing anything? DOes this look right?

if(buttonState==180)

How is the return value of a digitalRead ever going to equal 180?

Please use code tags when posting code.

So the dititalRead values have to be either 0 and 1?

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.

SO what do i have to do? SHould i take out the servoMotor,write()?

You can't move a servo with a digitalWrite, or for that matter, an analogWrite: to tell a servo where to go, you do need the servo.Write().

Like this..?

#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

void setup() {
pinMode(buttonPin, INPUT);
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
buttonState= digitalRead(buttonPin);

int analogValue = analogRead(A0);
Serial.println(analogValue);

int servoAngle = map(analogValue, 0, 1023, 0, 179);

if(buttonState != lastButtonState){
if(buttonState==180){

buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else{
Serial.print("0");
}
}

lastButtonState = buttonState;

if(buttonPushCounter % 2==0){
servo.Write(servoPin, 180);
}
else{
servo.Write(servoPin, 0);
}

I don't have time to check your whole logic (I'm at work), but yes those servo writes look ok.

BUT.... you're still asking this near the top....

 buttonState= digitalRead(buttonPin);

....

 if(buttonState==180){

... and as pointed out earlier, that can never be, and so your buttonPushCounter is not going to be incrementing properly

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

void setup() {
pinMode(buttonPin, INPUT);
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{
buttonState= digitalRead(buttonPin);

int analogValue = analogRead(A0);
Serial.println(analogValue);

int servoAngle = map(analogValue, 0, 1023, 0, 179);

if(buttonState != lastButtonState){

if(buttonState==0){

buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else{
Serial.print("0");
}
}

lastButtonState = buttonState;

if(buttonPushCounter % 2==0){

Servo.Write(servoPin, 1);
}
else{
** Servo.Write(servoPin, 0);**
}
servoMotor.write(servoAngle);
}

i am learning alot [sic]

But not, it seems, how to use code tags.