Ivy's Assignment 4!

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.

Schematic

Code

#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.
}
}

Additional Questions


Question 1: Say you are using a servo motor you attach to pin 9. In your loop() you have the following code: for (int i=0, i<180, i++){servo.write(i); delay(100);} Draw a graph with the X axis in seconds, for two seconds, and the y-axis the voltage at pin 9 with respect to ground.

Question 2: Your input device is slightly broken, leading it to give us an erroneous reading 1% of the time. How can we address this? Answer in (pseudo)code.

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
}
}

Question 3: Your input device is slightly noisy, leading the measurement to randomly deviate from the true measurement up or down by 10%. How can we address this? Answer in (pseudo)code.

// 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
}