touch sensor needs to move servo, need help with code.

My Project: Robot Dog from a old christmas decoration that when you pet his head he wags his tail. He will also turn "on" (light up body, collar (EL tape) and OLED eyes) when he senses motion. I need help with the sensor turning on a servo part first.

I'm using Arduino Uno. and Adafruit 16 channel , 12 bit servo driver. and one standard size, high torque, Metal Gear Servo.

I was able to make the servo work through the tutorial online via adafruit. But what I lack is how do I connect that to a touch sensor like the example, starter kit, touch sensor lamp code on Arduino. I bread boarded up that project and got a piece of foil to turn on a LED, but how do i make that turn on a Servo. I lack the ability to code. I can understand some of it. Maybe the Servo Driver is not needed or overkill? I just want to be able to pet the dog head and have it drive the servo. I need to program the capacitance sensor to be pretty sensitive so that when your hand leaves the head momentarily to lift and pet again I don't want him to stop wagging. I'd like the capacitance to sense the hand about a 1/2 inch or so off the head, so as the kids pet the dog it keeps wagging. I could only program the capacitor to turn on the LED when I touched the tin foil, I tried to tweek the code like in the beginning Arduino book showed but I couldn't get it to turn on unless I was touching it, so I need to know where in the code to adjust its sensitivity better. I googled my problem and found a few videos on youtube, but no directions on how to wire or code it. Also no luck on the project hub by using the words sensor and servo. Hope someone could help, I'd like my robot dog to be the hit of the Halloween party and Christmas at the Fire Station. Thank you.

Capacitive sensors are hard to control, in detail from a 1/2" distance. Try two foils, with the second one connected to Gnd.

Eventually a PIR sensor will work, with some tweaking to a low range, or an active IR or ultrasonic distance sensor.

To enable other effects than a LED, replace the code piece that turns the LED on by the activation of whatever outputs you need, and equivalent for the turn off section. Eventually show your code, enclosed in code tags </>.

Ok, I'll try that, I can't work on it again for a few days, but I'll post my code, I'm sure it will need tweaking. Thanks for the help.

I have no idea how to add these two sets of code together, I looked at the example code, "demonstration code for several things at once" and i cannot figure out how to edit that to work. I am using a Adafruit 16 channel 12 servo driver to work one servo, and there example code is at the top when i add my code, then the touch sensor code. If i can add them together do i run a wire from the + side of where the LED would be to the (5V) coming from the servo driver? i know this is kinds complicated. thanks for any and all help.

here's the code:

/*************************************************** 
  This is an example for our Adafruit 16-channel PWM & Servo driver
  Servo test - this will drive 16 servos, one after the other

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/815

  These displays use I2C to communicate, 2 pins are required to  
  interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4

  Adafruit invests time and resources providing this open source code, 
  please support Adafruit and open-source hardware by purchasing 
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.  
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// you can also call it with a different address you want
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);

// Depending on your servo make, the pulse width min and max may vary, you 
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
  
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  yield();
}

// you can use this function if you'd like to set the pulse length in seconds
// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
void setServoPulse(uint8_t n, double pulse) {
  double pulselength;
  
  pulselength = 1000000;   // 1,000,000 us per second
  pulselength /= 60;   // 60 Hz
  Serial.print(pulselength); Serial.println(" us per period"); 
  pulselength /= 4096;  // 12 bits of resolution
  Serial.print(pulselength); Serial.println(" us per bit"); 
  pulse *= 1000;
  pulse /= pulselength;
  Serial.println(pulse);
  pwm.setPWM(n, 0, pulse);
}

void loop() {
  // Drive each servo one at a time
  Serial.println(servonum);
  for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);
  for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) {
    pwm.setPWM(servonum, 0, pulselen);
  }

  delay(500);

  servonum ++;
  if (servonum > 7) servonum = 0;
}
/*
  Arduino Starter Kit example
 Project 13  - Touch Sensor Lamp

 This sketch is written to accompany Project 13 in the
 Arduino Starter Kit

 Parts required:
 1 Megohm resistor
 metal foil or copper mesh
 220 ohm resistor
 LED

 Software required :
 CapacitiveSensor library by Paul Badger
 http://www.arduino.cc/playground/Main/CapacitiveSensor

 Created 18 September 2012
 by Scott Fitzgerald

 http://www.arduino.cc/starterKit

 This example code is part of the public domain
 */

// import the library (must be located in the
// Arduino/libraries directory)
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;


void setup() {
  // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value
  Serial.println(sensorValue);

  // if the value is greater than the threshold
  if (sensorValue > threshold) {
    // turn the LED on
    digitalWrite(ledPin, HIGH);
  }
  // if it's lower than the threshold
  else {
    // turn the LED off
    digitalWrite(ledPin, LOW);
  }

  delay(10);
}

I managed to get the touch sensor tweaked the way I want it. It comes on when my hand hovers above the tin foil, and I used the (5v) power from the Adafruit servo driver and put it in line with the (+) on the LED and when I "touch" the foil the green light on the servo driver comes on and goes off when I move my hand away, so I'm successfully sending it power, but the servo won't work till I get the code right, and there's a second power source going into the servo driver (5v) which isn't controlled by the touch sensor....so I don't know where to go from here, how to add those two sets of code together......I tried cut and pasting sections, and got nothing. I got a C++ book on order, but I'm unsure if this will teach me how to write code for Arduino.

As a start, you should try simply bolting the two sketches together by using one as the source for the other, or use both as source for a third, by putting all the stuff from above setup() above setup() in the resulting sketch, all the stuff from both setup()s into the resulting setup and ditto into a new loop().

Then check for conflicts like say both sketches might use the same name for a variable, and give one of them a new name if that's the case, and check they don't both try use the same pins, all that sort of thing.

Get that to compile, but of course it won't actually do what you want. Not yet anyway... That's the next step.

I can't test anything since I have no hardware like yours, but it seems to me you could just then move the servo move stuff (from loop() of the servo sketch) inside the if of the sensor where the led comes on. (Assuming you want the servo to run when the led is on; if not, stick it in the other half of the if where the led's off.) Then when the led's on that servo move code should run the servo: the delay()s in that code will probably make the response to your sensor laggy.

But I do think the servo driver board approach is overkill and complicating things. You can easily control one servo with just the standard servo library, as in the servo sweep sketch in the IDE at File > Examples > Servo. Just put the part doing the sweeping inside the if where the led comes on, joining the sweep sketch with your sensor sketch as outlined above.

This code here illustrates what I was getting at above, where I use a button to mimic your sensor. The two parts of my "if" are where the button is pressed or not, same as your sensor above or below the threshold.

It's a bit unresponsive due to the nature of the "for" blocking it from looking at the switch until it gets a gap. That can be fixed with a millis() timing approach but that's for another day.

//************************************
//modded to sweep or not sweep with a button
//***********************************

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

  modified 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Sweep
*/


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

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

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

void loop() {
  if (digitalRead(theButton) == LOW) //button pushed
  { //sweep
    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 >= 0; 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
    }
  }
  else //button not pushed
  {
    //don't sweep
  }
}

Don't bother with the power of the servo driver, it must stay powered continuously!

Instead extend your code, at the two places where it turns the LED on and off. As a test, add another LED that blinks in reverse of the first LED.

 // if the value is greater than the threshold
 if (sensorValue > threshold) {
   // turn the LED on
   digitalWrite(ledPin, HIGH);
   digitalWrite(ledPin2, LOW); //turn second LED off
   //later add code for other outputs, like servo motor
   //pwm.setPWM(servonum, 0, SERVOMIN);
 }
 // if it's lower than the threshold
 else {
   // turn the LED off
   digitalWrite(ledPin, LOW);
   digitalWrite(ledPin2, HIGH); //turn second LED on
   //later add code for other outputs, like servo motor
   //pwm.setPWM(servonum, 0, SERVOMAX);
 }

Do you know already how to make that code compile and blink the second LED, too?

Thank you very much Ardy_guy, and Dr.Diettrich for your help, I feel like I'm almost there. Ardy I think my servo driver is overkill too, I will just hook directly to my Arduino. I still am a bit confused guys on how to add the two together. If I use the sensor sketch do I add the whole servo sketch to the (IF) statement? Or just take each part and add it to the appropriate part of the sketch. I'm still fuzzy about do I hook the servo to (pin9)?? And how do I make the servo code go like (0,45, then 45,0) continuously while the sensor is being touched simulating the dog tail wag? I'll attach the code I'm trying when I have some time tomorrow to mess with it again and remove the servo driver out of equation. I will have to add the extra power (5v) to the Arduino too. Thanks for your continued help guys.

You combine sketches by adding the similar parts to similar parts, so to speak.

So the resulting sketch has 2 lots of stuff from above setup, above setup. Two lots of stuff from 2x setup()s in the resulting setup(), ditto for loop(). (Only 1x setup() and 1x loop() in the result, of course.)

Check to make sure no duplicate pins from both sketches, and ditto variable names.

THEN you move stuff around, so for example the servo movement commands go inside the if.

The servo yellow or orange wire goes in say pin 9. The red and black go to an external 5V supply, and the black from the servo and its power also goes to the ground on the Arduino.

(Most servos handle 4.8-6V so you can probably use 4x1.5V AA for the servo.)

Ok i got it working kind of. Instead of the servo turning on when i touch the sensor it turns off, so it runs until i touch the sensor then turns off. So i assume i copied and pasted the code wrong. can you guys take a look at it and tell me whats wrong? i will also include a few pics of how i wired it to my arduino. I'll add them from my phone on a second post its easier.

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

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;

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

   // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);

}

void loop() {
   // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value
  Serial.println(sensorValue);

  // if the value is greater than the threshold
  if (sensorValue > threshold) {
    // turn the LED on
    digitalWrite(ledPin, HIGH);
  }
  // if it's lower than the threshold
  else {
    // turn the LED off
    digitalWrite(ledPin, LOW);
  }

  delay(10);
  for (pos = 45; 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 >= 45; 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
  }
}

In the code you posted there, the servo code is actually outside the "if" and its "else" so according to that it will run always.

Perhaps you posted an older version?

You need to put that into either where the led goes on or off, in the "if" or its "else".

If you have problems in implementing concurrent actions, like wagging the tail and blinking LEDs, you can try to use task macros. Every task function runs (quasi) concurrently, without disturbing other tasks. The same effect can be achieved with the frequently recommended procedures, the task macros only hide nasty things like a state machine and their local variables.

In your case the task (let's call it wagTail) will move the tail (servo), just as you already coded. Move that code into a function wagTail, add taskBegin() and taskEnd(), and replace each delay() by taskDelay(). Finally add conditions to start wagging when the sensor is touched, and stop it when it's not touched any more. This may look like this:

void wagTail(bool active) {
taskBegin();
//wait for sensor touched
  taskWaitFor(active);

//activated, do wagging
  taskDelay(10);
  for (pos = 45; 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'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 45; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }

//task starts over, checking "active"
taskEnd();
}

Then make the task execute, by adding this to loop(), instead of the moved code:

  wagTail(sensorValue > threshold);

Of course you can add further tasks, like a LED light show, using the above pattern.

Ok i kinda understand some of what your telling me...its still kinda like reading Greek or Latin. I tried to add your code in to the one i had, but i kept getting errors no matter where i added it. It keep saying "task begin was not declared in this scope. I am sorry if i totally screwed it up like i know i did....but Im way to new at this to understand I had to get out my "Arduino projects book out just to try and understand where the "if" statement went. Here's how i pasted it together, but i cant even get it to verify now. Sorry for wasting your guys time. I really appreciate the help. I'm just kinda lost.

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

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;

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

 // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}
void wagTail(bool active) {
taskBegin();
//wait for sensor touched
  taskWaitFor(active);

//activated, do wagging
  taskDelay(10);
  for (pos = 45; 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'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 45; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }

//task starts over, checking "active"
taskEnd();
}

void loop() {
  // put your main code here, to run repeatedly:
  wagTail(sensorValue > threshold);
}

Like any other library, you have to install ALib0 and #include <ALib0.h>.

Also keep your sensor code in loop(), else sensorValue will never change.

Ok i included the Library, and moved the code down to the loop, was i supposed to keep that in void setup as well as in the loop? I'm getting the following errors; 'sensor value' was not defined in this scope. whatever that means I don't know. Here's a copy of what i currently have. and thanks again....I will have to show you the project when its all done in a few months.

#include <ALib0.h>

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

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;

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

 // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}
void wagTail(bool active) {
taskBegin();
//wait for sensor touched
  taskWaitFor(active);

//activated, do wagging
  taskDelay(10);
  for (pos = 45; 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'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 45; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }

//task starts over, checking "active"
taskEnd();
}

void loop() {
  // put your main code here, to run repeatedly:
  wagTail(sensorValue > threshold);
void wagTail(bool active) {
taskBegin();
//wait for sensor touched
  taskWaitFor(active);

//activated, do wagging
  taskDelay(10);
  for (pos = 45; pos <= 180; pos += 1) { // goes from 0 degre "es to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 45; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }

//task starts over, checking "active"
taskEnd();
}

Please look into your initial code, what was included in loop(), and what global variables you had there.

Don't duplicate the wagTail() function, remove it from loop() and keep the copy before loop().

Ok here's the latest copy paste of code, It almost compiled this time, the errors are below, and I wondered if in your code Dr.Diettrich and I have no idea if I'm right, but was is supposed to be Bool? or boot? and does that matter? here's the part I'm talking about, "void Tailwag (bool active) }" so here's the errors it states:

Arduino: 1.8.1 (Windows 10), TD: 1.35, Board: "Arduino/Genuino Uno"

C:\Users\Nancy\AppData\Local\Temp\ccdNH4K0.ltrans0.ltrans.o: In function `main':

C:\Program Files (x86)\arduino-1.8.1\hardware\arduino\avr\cores\arduino/main.cpp:43: undefined reference to `setup'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

#include <ALib0.h>

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

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;
 
void wagTail(bool active) {
taskBegin();
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object

   // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);


//wait for sensor touched
  taskWaitFor(active);

//activated, do wagging
  taskDelay(10);
  for (pos = 45; 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'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 45; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    taskDelay(15);                       // waits 15ms for the servo to reach the position
  }

//task starts over, checking "active"
taskEnd();
}


void loop() {
   // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value
  Serial.println(sensorValue);

  // if the value is greater than the threshold
  if (sensorValue > threshold) {
    // turn the LED on
    digitalWrite(ledPin, HIGH);
  }
  // if it's lower than the threshold
  else {
    // turn the LED off
    digitalWrite(ledPin, LOW);
  }

  delay(10);
  for (pos = 45; 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 >= 45; 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
  }

    wagTail(sensorValue > threshold);
}

Why don't you follow the given instructions? Nobody told you to replace setup() by wagTail().

Every Arduino sketch consists of functions setup() and loop(), and more functions can be added. Have a look at the ALib0 examples, how sketches are constructed properly.

I'm sorry, I'm trying, I'm getting so frustrated with the project that I'm about to quit and just make it have power all the time.....would be easier. I tried looking at your example code, but I still just don't understand it. I got an Arduino for dummies book, and a Arduino projects for kids book to try and understand the code part of it. So perhaps I'll come back to it when I have the patience and understanding, I would learn this better if someone could sit over my shoulder and point to things and explain them, I'm a hands on visual learner (Art degree) this stuff is past my minds ability. I took out the tail wag code and put it back the way it was with your Alib0 added, but It still just runs the servo constantly until i touch the touch sensor and it stops. so basically doing the opposite of what I want. I think I need to learn more before asking questions on this Forum. It's possible I wired it wrong, but I can't add pictures like I can on the Adafruit site. Here it is anyways.

#include <ALib0.h>

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

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
#include <CapacitiveSensor.h>

// create an instance of the library
// pin 4 sends electrical energy
// pin 2 senses senses a change
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);

// threshold for turning the lamp on
int threshold = 1000;

// pin the LED is connected to
const int ledPin = 12;
 
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

   // open a serial connection
  Serial.begin(9600);
  // set the LED pin as an output
  pinMode(ledPin, OUTPUT);

}

void loop() {
   // store the value reported by the sensor in a variable
  long sensorValue = capSensor.capacitiveSensor(30);

  // print out the sensor value
  Serial.println(sensorValue);

  // if the value is greater than the threshold
  if (sensorValue > threshold) {
    // turn the LED on
    digitalWrite(ledPin, HIGH);
  }
  // if it's lower than the threshold
  else {
    // turn the LED off
    digitalWrite(ledPin, LOW);
  }

  delay(10);
  for (pos = 45; 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 >= 45; 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
  }

   
}