I'm super new to this and really need help as the assignment is due tomorrow and the part I needed only just arrived! I've figured out how to code the two separately and have the servo work and have the touch sensor work, but not how to work it together. I'm begging for help, please! :o
Servo code
#include <Servo.h>
int servoPin = 9;
Servo servo;
int servoAngle = 0; // position in degress
void setup()
{
Serial.begin(9600);
servo.attach(servoPin);
}
void loop()
{
servo.write(45); // Turn SG90 servo Left to 45 degrees
delay(1000); // Wait 1 second
servo.write(90); // Turn SG90 servo back to 90 degrees (center position)
delay(1000); // Wait 1 second
servo.write(135); // Turn SG90 servo Right to 135 degrees
delay(1000); // Wait 1 second
servo.write(90); // Turn SG90 servo back to 90 degrees (center position)
delay(1000);
//end control the servo's direction and the position of the motor
//control the servo's speed
//if you change the delay value (from example change 50 to 10), the speed of the servo changes
for(servoAngle = 0; servoAngle < 180; servoAngle++) //move the micro servo from 0 degrees to 180 degrees
{
servo.write(servoAngle);
delay(50);
}
for(servoAngle = 180; servoAngle > 0; servoAngle--) //now move back the micro servo from 0 degrees to 180 degrees
{
servo.write(servoAngle);
delay(10);
}
//end control the servo's speed
}
Sensor Code
// When Sig Output is high, touch sensor is being pressed
#define ctsPin 2 // Pin for capactitive touch sensor
int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
}
void loop() {
int ctsValue = digitalRead(ctsPin);
if (ctsValue == HIGH){
digitalWrite(ledPin, HIGH);
Serial.println("TOUCHED");
}
else{
digitalWrite(ledPin,LOW);
Serial.println("not touched");
}
delay(500);
}