servo and cap sensor

Hello everyone.
I have a bit of a problem with a piece of code i working with. I have a qt113 sensor which turns on and off when some touches it. I'm also moving a servo. I would like the both the touch and servos to run without interruption. The servo moves to 3 positions, and an led turns on when the touch sensor is touch.

here is what i have so far:

int potPin = 1;    
int ledPin4 = 8;   
byte val = 0;       
int THRESHOLD = 5;
int statePin = LOW;  

int ledPin6 = 8; 
int ledPin3 = 5; 
int ledPin = 2;
    
int servoPin = 14;   // servo connected to digital pin 2

int myAngle;        // angle of the servo roughly 0-180
int pulseWidth;     // servoPulse function variable
void setup()
{   
  pinMode(servoPin, OUTPUT);   // sets pin 2 as output
  pinMode(ledPin, OUTPUT); 
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin6, OUTPUT); 
  pinMode(ledPin4, OUTPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(9600);

}
void servoPulse(int servoPin, int myAngle)    
{      
  pulseWidth = (myAngle * 10) + 600;  // determines delay  
  digitalWrite(servoPin, HIGH);       // set servo high
  delayMicroseconds(pulseWidth);      // micro pause
  digitalWrite(servoPin, LOW);              // set servo low
  delay(20);
                                    // refresh cycle
}

void loop()
{ val = analogRead(potPin); 
  if (val >= THRESHOLD) {
  statePin = !statePin;           
  digitalWrite(ledPin4, statePin); 
  }
  delay(150);  // we have to make a delay to avoid overloading the serial port
  digitalWrite(ledPin, HIGH);  
  myAngle = 10;                          // starts at 10º
  for(int i=0; i<300; i++)             // loops 50 times
  {
    servoPulse(servoPin, myAngle);
  }
  digitalWrite(ledPin, LOW);    // sets the LED off

                               // waits for a second
// wk_2
  digitalWrite(ledPin3, HIGH);   // sets the LED on
  myAngle = 90;                  // moves to 90º
  for(int i=0; i<300; i++)       // loops 50 times
  {
    servoPulse(servoPin, myAngle);
  }
  digitalWrite(ledPin3, LOW);      // sets the LED off


// wk_3
  digitalWrite(ledPin6, HIGH);     // sets the LED on
  myAngle = 170;              // then moves to 170º
  for(int i=0; i<300; i++)       // loops 50 times
  {
    servoPulse(servoPin, myAngle);
  }
  digitalWrite(ledPin6, LOW);      // sets the LED off

}

You could do something like this:

int touchPin = 3;  // set this to the pin connected to the qt113
int potPin = 1;    
int ledPin4 = 8;   
byte val = 0;       
int THRESHOLD = 5;
int DELAY = 6;           // number of seconds between servo movements
int statePin = LOW;  

int ledPin6 = 8;    
int ledPin3 = 5; 
int ledPin = 2;
      
int servoPin = 14;   // servo connected to analog pin 0
int myAngle;        // angle of the servo roughly 0-180
int pulseWidth;     // servoPulse function variable
void setup()
{   
  pinMode(servoPin, OUTPUT);   // sets pin 2 as output
  pinMode(ledPin, OUTPUT); 
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin6, OUTPUT); 
  pinMode(ledPin4, OUTPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(9600);

}

void processTouch(){
   // do something here when qt113 sensor touched 
}

void servoPulse(int servoPin, int myAngle)    
{      
  pulseWidth = (myAngle * 10) + 600;  // determines delay  
  digitalWrite(servoPin, HIGH);       // set servo high
  delayMicroseconds(pulseWidth);      // micro pause
  digitalWrite(servoPin, LOW);              // set servo low
  delay(20);                        // refresh cycle
}

void servoMove(int servo, int angle, int duration){
 // move servo to given angle, wait for given duration in seconds
  digitalWrite(ledPin, HIGH);  
  for(int i=0; i < duration * 50; i++)       // loops 50 times number of seconds
  {
    servoPulse(servo, angle);
    if(digitalRead(touchPin) == HIGH)  //change to LOW depending on qt113 interface
        processTouch();
  }
  digitalWrite(ledPin, LOW);    // sets the LED off  
}

void loop(){
  val = analogRead(potPin); 
  if (val >= THRESHOLD) {
   statePin = !statePin;            
  digitalWrite(ledPin4, statePin); 
  }
 
  servoMove(servoPin,10,DELAY);  // wk_1: 10 degrees for DELAY seconds
  servoMove(servoPin,90,DELAY);  // wk_2: 90 degrees 
  servoMove(servoPin,170,DELAY);  // wk_3: 170 degrees 
}

thank you for your help...
The servo doesn't seem to want to move. It only pulses forward. I would like for it to move to 170(left) 90(middle), right(10). I need this go continuously, which the cap sensor is on. For example, while the cap sensor is working, the servo is moving every minute to a designated position. It could be at 170 one minute and the next minute it moves to 10.

The servo doesn't seem to want to move. It only pulses forward.

It doesn't move, or it only moves in one direction?

Have you tested your servo and wiring with one of the simple playground sketch's?

Servo works...the initial code i posted moves the servo to the designated position. it is moving to either 170(left), 10(right), 90(middle)...There is a delay of 1 minute between each position, and movement to one of these 3 position. All i need is to have the cap sensor which by the way works with the last code you posted, working which the servo is active. hope this make sense.

Could you post the latest code you are trying and say what the servo does when you run the sketch (still not clear if it moves in one direction and stops, or doesn't move at all)

here's what i have so far: It includes the cap sensor, i am using a blinkM which fades when you touch the sensor. I also have an rgb led which changes color when the servo moves to a specific position. so if the servo moves to 90, the led turns blue, if it moves 170, it turns red, if it moves 10 it turns green.

#include "Wire.h"  
#include "BlinkM_funcs.h" 
int blinkm_addr = 0x10;
#define CALMED_HUE 200  
int potPin = 1;     // set this to the pin connected to the qt113
int ledPin9 = 13;   
byte val = 0;       
int THRESHOLD = 5;
int statePin = LOW;  

//blink m imports
long red = 0;
long green = 0;
long blue = 0;

//LED Imports
int ledPin2 = 4; 
int ledPin1 = 3; 
int ledPin = 2;
    
int servoPin = 14;   // servo connected to digital pin 2
int myAngle;          // angle of the servo roughly 0-180
int pulseWidth;     // servoPulse function variable
void setup()
{ BlinkM_begin();  
  BlinkM_setAddress(blinkm_addr);  
  byte rc = BlinkM_checkAddress(blinkm_addr);  
  if (rc == -1) {  
    Serial.println("rnno response");  
  }  
  else if (rc == 1) {  
    Serial.println("rnaddr mismatch");  
  }
    
  BlinkM_stopScript(blinkm_addr);  
  BlinkM_fadeToHSB(blinkm_addr, CALMED_HUE, 0xff, 0xff);  
  BlinkM_setFadeSpeed(blinkm_addr, 0x10);  
  val = 0;    
  pinMode(servoPin, OUTPUT);   // sets pin 2 as output
  
  pinMode(ledPin2, OUTPUT);   // blue
  pinMode(ledPin1, OUTPUT);   // green
  pinMode(ledPin, OUTPUT);    // red
  Serial.begin(9600);  

}
void processTouch(){
  //blink process
}
  delay(150);  // we have to make a delay to avoid overloading the serial port
}

void servoPulse(int servoPin, int myAngle)    
{      
  pulseWidth = (myAngle * 10) + 600;  // determines delay  
  digitalWrite(servoPin, HIGH);       // set servo high
  delayMicroseconds(pulseWidth);      // micro pause
  digitalWrite(servoPin, LOW);              // set servo low
  delay(20);
                                    // refresh cycle
}
void loop()
{  val = analogRead(potPin); 
// wk_0
  digitalWrite(ledPin6, HIGH);   // sets the red LED on
  digitalWrite(ledPin3, HIGH);   // sets the red LED on
  digitalWrite(ledPin1, HIGH);    // sets the red LED on

  myAngle = 90;                   // starts at 10º
  for(int i=0; i<300; i++)       //  loops 50 times
  {
    servoPulse(servoPin, myAngle);    
    if(digitalRead(potPin) == HIGH)  //change to LOW depending on qt113 interface
      processTouch();   
  }
  digitalWrite(ledPin6, LOW);    // sets the red LED off
  digitalWrite(ledPin3, LOW);    // sets the red LED off
  digitalWrite(ledPin1, LOW);     // sets the red LED off
                                 // waits for a second
// wk_1
  digitalWrite(ledPin6, HIGH);   // sets the blue LED on
  digitalWrite(ledPin4, HIGH);   // sets the blue LED on
  digitalWrite(ledPin1, HIGH);   // sets the blue LED on

  myAngle = 10;                   // moves to 90º
  for(int i=0; i<300; i++)       // loops 50 times
  {
    servoPulse(servoPin, myAngle);    
    if(digitalRead(potPin) == HIGH)  //change to LOW depending on qt113 interface
      processTouch();

  }
  digitalWrite(ledPin6, LOW);      // sets the blue LED off
  digitalWrite(ledPin4, LOW);      // sets the blue LED off
  digitalWrite(ledPin1, LOW);      // sets the blue LED off

// wk_2
  digitalWrite(ledPin6, HIGH);     // sets the green LED on
  digitalWrite(ledPin3, HIGH);     // sets the green LED on
  digitalWrite(ledPin, HIGH);     // sets the green LED on
  myAngle = 10;                   // moves to 90º
  for(int i=0; i<300; i++)       // loops 50 times
  {
    servoPulse(servoPin, myAngle);    
    if(digitalRead(potPin) == HIGH)  //change to LOW depending on qt113 interface
      processTouch();
  digitalWrite(ledPin6, LOW);     // sets the green LED on
  digitalWrite(ledPin3, LOW);     // sets the green LED on
  digitalWrite(ledPin, LOW);     // sets the green LED on

  }

The servo doesn't seem to want to move. It only pulses forward.

Did the comment above apply to the code I posted yesterday or the code you just posted? Did you run the code as posted yesterday and did it move the servos?

The code you just posted looks like it will have problems because your touch processing delays 150ms. That is much to long between servo pulses.

If you are only using one or two servos, you may want to look at some of the code in the playground that drives the servos using an interrupt. I think the library is called ServoTimer1.