-






-
What I changed????
I changed the whole structure of the servo motor code. Instead of having slightly complicated code for motor to go through every position I just set a certain position and this is what I wanted. The axis spins fast straight to the position I set up.
Code for servo motor to spin in some certain degree is
myservo.write(100);delay(10);
-
Working code
#include <CapSense.h>
#include <Servo.h>
/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Slightly adapted by Bare Conductive 2011
* Uses a high value resistor e.g. 10 megohm between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of Bare Paint
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int motorPin1 = 5; // One motor wire connected to digital pin 5
int motorPin2 = 6; // One motor wire connected to digital pin 6
int motorPin3 = 7; // One motor wire connected to digital pin 5
int motorPin4 = 8; // One motor wire connected to digital pin 6
int pos = 0; // variable to store the servo position
CapSense cs_4_2 = CapSense(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add Bare Paint
// CapSense cs_4_5 = CapSense(4,5); // OPTIONAL: for sensor 2, 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add Bare Paint
// CapSense cs_4_8 = CapSense(4,8); // OPTIONAL: for sensor 3, 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add Bare Paint
void setup()
{
pinMode(motorPin1, OUTPUT); // initialize the digital pins as an output:
pinMode(motorPin2, OUTPUT); // initialize the digital pins as an output:
pinMode(motorPin3, OUTPUT); // initialize the digital pins as an output:
pinMode(motorPin4, OUTPUT); // initialize the digital pins as an output:
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
long start = millis();
long total1 = cs_4_2.capSense(30);
if( total1 > 30){
Serial.println(“xa”);
// in steps of 1 degree
myservo.write(100); // tell servo to go to position in variable ‘pos’
delay(10);
// waits 15ms for the servo to reach the position
Serial.println(“left”);
rotateLeft(2000, 500);
rotateRight(2000, 500);
}else{
myservo.write(-180); // tell servo to go to position in variable ‘pos’
delay(10);
// waits 15ms for the servo to reach the position
Serial.println(“right”);
}
delay(800); // arbitrary delay to limit data to serial port
}
void rotateLeft(int speedOfRotate, int length){
analogWrite(motorPin1, speedOfRotate); //rotates motor
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
delay(length); //waits
digitalWrite(motorPin1, LOW); // set the Pin motorPin1 LOW
}
void rotateRight(int speedOfRotate, int length){
analogWrite(motorPin3, speedOfRotate); //rotates motor
digitalWrite(motorPin4, LOW); // set the Pin motorPin1 LOW
delay(length); //waits
digitalWrite(motorPin3, LOW); // set the Pin motorPin2 LOW
}
-
[Flash 10 is required to watch video]
What I am trying to do here is to add motor and by using capacitive proximity sensor activate it. So when I hover my hand over the foil the motor should work. Apparently something was wrong with a code and in the serial monitor I got answers but the motor kept working even when my hand was of the foil.
The code I add was:
if(cs_4_2.capSense(30) > 30){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
}}
-
[Flash 10 is required to watch video]
#include <CapSense.h>
#include <Servo.h>
/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Slightly adapted by Bare Conductive 2011
* Uses a high value resistor e.g. 10 megohm between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of Bare Paint
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/
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
CapSense cs_4_2 = CapSense(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add Bare Paint
// CapSense cs_4_5 = CapSense(4,5); // OPTIONAL: for sensor 2, 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add Bare Paint
// CapSense cs_4_8 = CapSense(4,8); // OPTIONAL: for sensor 3, 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add Bare Paint
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
long start = millis();
long total1 = cs_4_2.capSense(30);
// long total2 = cs_4_5.capSense(30); // OPTIONAL for sensor 2
// long total3 = cs_4_8.capSense(30); // OPTIONAL for sensor 3
// Serial.print(millis() - start); // OPTIONAL: check on performance in milliseconds
// Serial.print(” “); // OPTIONAL: tab character for debug windown spacing
Serial.println(total1); // OPTIONAL: To use additional sensors,change Serial.println to Serial.print for proper window spacing
//Serial.print(” “); // OPTIONAL: tab character for window spacing for sensor output 2
//Serial.print(total2); // OPTIONAL: print sensor output 2
//Serial.print(” “); // OPTIONAL: tab character character for sensor output 3
//Serial.println(total3); // print sensor output 3
}
delay(800); // arbitrary delay to limit data to serial port
}
-
Getting started with a project
Today I am trying to build capacitive proximity sensor. I am using a few wires, foil and 1megOhms metal film resistor. By having this code I am able to detect wheter the hand is over the foil or if it is not. You will see the example above.