I am trying to combine a servo with a touch sensor/ infrared sensor so they will turn the servo on and cause it to rotate 100 degrees or so and come back. I am using an Arduino Uno. My code for the sensors is
// Henry's Bench
// Capacitive Touch Sensor Tutorial
// When Sig Output is high, touch sensor is being pressed
#define ctsPin 2 // Pin for capactitive touch sensor
int LDR = 0;
int ledPin = 13; // pin for the LED
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
pinMode(LDR, 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);
int v = analogRead(LDR);
Serial.println(v);
}
and my code so far for the servo to move is
/* 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
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'
}
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'
}
}
/* 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
*/
// Henry's Bench
// Capacitive Touch Sensor Tutorial
// When Sig Output is high, touch sensor is being pressed
#define ctsPin 2 // Pin for capactitive touch sensor
int LDR = 0;
int ledPin = 13; // pin for the LED
#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
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
int ctsValue = digitalRead(ctsPin);
if (ctsValue == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("TOUCHED");
}
else {
digitalWrite(ledPin, LOW);
Serial.println("not touched");
}
delay(500);
int v = analogRead(LDR);
Serial.println(v);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
pinMode(LDR, INPUT);
}
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'
}
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'
}
}
No. You have a bunch of code hanging outside of any function (look at the } that closes setup(). In that code are pinMode statements that belong in setup() and code that belongs in loop().
This is as corrected as I can get it without the hardware. Compiles with ver 1.6.5, Win10.
/* 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
*/
// Henry's Bench
// Capacitive Touch Sensor Tutorial
// When Sig Output is high, touch sensor is being pressed
#define ctsPin 2 // Pin for capactitive touch sensor
int LDR = 0;
int ledPin = 13; // pin for the LED
#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
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
pinMode(LDR, 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);
int v = analogRead(LDR);
Serial.println(v);
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'
// you might want a delay here to slow the servo?
}
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'
// you might want a delay here to slow the servo?
}
}
Leave "void setup()" behind, move all the junk to the destination's setup.
Do the same with "void loop()".
Check for pin conflicts and order the program sensibly, such as putting a button reading before whatever would happen as a result of the button being used.
okay i believe this is what you are trying to achieve.
#define ctsPin 2 // Pin for capactitive touch sensor
#include <Servo.h>
Servo myservo;
int LDR = 0;
int ledPin = 13; // pin for the LED
int pos = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ctsPin, INPUT);
pinMode(LDR, INPUT);
myservo.attach(9);
}
void loop() {
int ctsValue = digitalRead(ctsPin);
if (ctsValue == HIGH) {
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'
}
}
else {
digitalWrite(ledPin, LOW);
Serial.println("not touched");
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(500);
int v = analogRead(LDR);
Serial.println(v);
}
this should allow the servo to spin to one position when the sensor is pressed and then spin back when it is no longer pressed. i understand my commenting is not the best but just ask questions if you dont understand why i have done it this way.