Here is all the documentation for assignment 4!
Above is a picture of the circuit and the capacitative sensor and Servo motor itself! I used a serving spoon as the sensor.
#include // allows the use of the CapacitiveSensor library, which acts as the input
#include // allows the use of the Servo library, which acts as the output
CapacitiveSensor cs_4_2 = CapacitiveSensor(7,5); // 1 megohm resistor between pins 7 & 5, pin 5 is sensor pin, add wire, metal thing
Servo myServo; // create a Servo object
const int pwm = 11; // the output PWM that the Servo is attached to
//runs once at setup
void setup() {
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
myServo.attach(pwm); // links the Servo to the PWM pin
Serial.begin(9600); // begins the Serial monitor
}
// runs repeatedly
void loop()
{
long start = millis(); // returns the number of milliseconds since the device begins running the program
long total1 = cs_4_2.capacitiveSensor(30); // runs a sensor check 30 times to avoid errors and filters for bad data
Serial.print(millis() - start); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug window spacing
Serial.println(total1); // print sensor output 1
delay(10); // arbitrary delay to limit data to serial port
if (total1 > 200) { //checks if the spoon is being touched
myServo.write(180); // sets an angle of 180 to the Servo
delay(100); // allows a delay for the Servo to move a bit
myServo.write(0); // sets an angle of 0 to the Servo
delay(100); // allows a delay for the Servo to move a bit. The Servo does not actually move the entire 180 degrees, but it jiggles.
}
}
void setup(){
gathered data = test for 5 seconds //collects over 100 data points within 5 seconds
check gathered data
get rid of beginning 5% and end 5% of the data
newMin = new beginning of data
newMax = new end of data
}
void loop(){
sensorValue = activated sensor
if sensorValue > newMax {
throw it away!
} elseif sensorValue < newMin {
throw it away!
} else {
activate output
}
}
// goal: get as close to the true measurement as possible
void setup(){
gathered data = test for 10 seconds //collects over 200 data points
average the gathered data //it should average near the true measurement!
}
void loop(){
add new values to gathered data
average the values
update average value //this should be closer to the true measurement as it loops
}