Simple (not for me) Servo on/off switch using pressure sensors

Hi All,

This may be a silly question, but for some reason my servo (parallax continuous) is activated when the current is connected and off when the current is disconnected.

I have my servo motors moving back and forth (similar to a sweep) and I've made a home made pressure sensor (two pieces of foil separated by foam). Essentially, I just want my servo to turn on (move back and forth) when the current is flowing (pressure pushed down and foil is touching) and stop (or turn off) when the current is not flowing (foil apart).

SET UP: I have my servo connected to pin 9, button to pin 2, ground (top foil) and power (bottom foil) - when they touch, my servo is MOVING. when they aren't touching, my servo IS moving. ---Is there an easier solution to this. Is seems like I could just interrupt the ground and power current from the servo (turning it on and off, not even needing a separate analog pin).

Please help me with suggestions. Thank you so very much in advance. :cold_sweat:

Please help me with suggestions

First suggestion is posting your code. (and maybe a photo of your foam.

If you exchange your foam for a real switch , can you get it to work then?

ground (top foil) and power (bottom foil) - when they touch, my servo is MOVING. when they aren't touching

That is because when they are touching you are shorting out the power supply and so getting no voltage to drive your motor.

Hey Guys! Thanks so much for replying!! I really appreciate this!!

@ robtillaart: No, if I change it to a real switch, it still doesn't work. Is this because I have a continuous rotation servo (this is parallax, not hacked)?

@Grumpy_Mike: I figure that may be the reason. I bought a hex inverter chip (SN7404N) and relay (r 70-5d1-6). I tried the inverter chip, but it started heating up.

I feel totally lost and apologies for my newbie vocabulary or approach to this. Thanks again for helping me...!!

Here is my code:

#include <Servo.h>

Servo myservo;
int buttonPin = 0;

int buttonState = 0;

int old_buttonState = 0;
int pos = 0;
int delayVal = 8;

void setup() {
pinMode(buttonPin, INPUT);
myservo.attach(9);

myservo.write(0);

}

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

if ((buttonState == HIGH) && (old_buttonState == LOW)) {

for(pos = 0; pos < 360; pos += 1)
{
myservo.write(pos);

delay(delayVal);
}
for(pos = 0; pos>=-360; pos-=1)
{
myservo.write(pos);

delay(delayVal);
}
}
old_buttonState = buttonState;
}

Attached are photos of my foam and set up:

I think you need to post a schematic of what you are trying to do. Using an inverter makes no sense, you can easily invert a signal in software.
That foam doesn't look like it is conducting foam, that's normally black. You will need a pull up resistor not to connect it to +5V.

It's very similar to the basic sweep schematic provided for us in the arduino example, instead I want the servo to sweep back and forth as the pressure mat is stepped on and off when the pressure mat is idle (with no weight).

I want the servo to behave like this video when you hold the button down:

Thanks!!!

// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

It's very similar to the basic sweep schematic provided for us in the arduino example

No not good enough we need to know exactly what you have done before we can possibly say what you are doing wrong. Can you not see that?
It is like sending your brother to the doctors because he has a pain in his leg just like yours.

Grumpy Mike, I've read your tutorial on pull up/pull down resistors and it's help make sense of all this a bit better.
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

I've adjusted the code to activate the internal pull up resistor in the arduino AND added a 10k resistor between 5v and the switch (Please see schematic). When I play around with the code and the placement of the pin and/or resistors, the only behavior I get is either it is servo is stopping when the button is pushed down (no longer short circuiting) and servo runs when the button is idle, or the servo has stopped and when the button is pushed down, it does nothing.

Thanks again for helping.

Updated Code:

int servoPin = 9;

int buttonPin = 0;
int buttonState = 0;
int old_buttonState = 0;

int fullCounter = 1250; // left
int stopped = 1500; // stop
int fullClock = 1750; // right

int dirInt = 1500; //stopped to start
int dir = 1; //add 1 ms per loop untill hitting 1800, then -1 until 1200

void setup()
{
pinMode(servoPin,OUTPUT);
pinMode(buttonPin, INPUT);

digitalWrite(buttonPin, HIGH); // turn on pullup resistors

}
void loop()
{
buttonState = digitalRead(buttonPin);
if ((buttonState == HIGH) && (old_buttonState == LOW)) {
//if (digitalRead(0)==HIGH) {
//digitalWrite(servoPin,LOW);
delayMicroseconds(dirInt);
//digitalWrite(servoPin,HIGH);
delay(20); // 20ms

dirInt += dir;
if(dirInt >= fullClock)
{
dir = -1;
} else if(dirInt <= fullCounter) {
dir = 1;
}

}
}

servoschematic.jpg

int buttonPin = 0;

Is the problem, pins 0 & 1 are used by the serial port, they are best avoided unless you know how to cope with that.