In addition to doing so more capacitive sensor testing with walnuts, I also wanted to branch out a use a sensor that is unrelated to my project. I feel like all the sensors we have talked about in class go about their sensoring magical powers in different ways. So, I just selected thermistor pin and see if I can make it work.
I used some starter code from the thermistor pin section in class. The goal for this code is that when it hits a certain temperature (100 degrees Fahrenheit), which the sensor will be detecting, the Arduino will then tell the servo motor to move. Finding a way to reach the temperature quickly was a bit difficult. I didn't have someone who could go to lab with me and be my buddy, so I brought my stuff home, where there a lot fewer resources to work with. I did have a lighter though, and in hindsight, this is probably such a hazardous idea of using open flame in a middle of a chaos of wires and a breadboard, but alas, it got the job done to reach the temperature of 100 degrees F. I learned from Bobby afterwards that in lab, we have a heat gun, which made me realize there was definitely a safer way to test my thermistor pin. What I did involved burning up a q-tip and putting that flame right on top of the thermistor pin.
I will also try to no longer use delay and instead incorporate what we learned in lab regarding millis. To do that, I need to initial an unsigned long mills. Then, I also have to create an interval of 500 milliseconds. I also wanted to see what my sensor was detecting, so I did some coding to make my serial monitor be more clear as to what temperature the thermistor pin was detecting.
For the code, I had to make one class for my sweeper. I did this in class, but now that I have to create my own class, I actually understand it much better. I gave it a start position and an end position, but that it will only do this once the temperature is 100 degrees F. So, there is an if condition. I explain more about the code in the next section in the comments.
Code for Thermistor Pin:
#include <CapacitiveSensor.h>
//starter code from : https://nathanmelenbrink.github.io/ps70/06_input/index.html
int ThermistorPin = A0; //I put my thermistor pin into analog 0
int Vo;
float R1 = 100000;
float R2, T, C, F; // Lol... so, I don't really vibe with Celsius,
//so I am going to convert the results from kelvin to celsius to fahrenheit
float A = 3.354e-03; // Set vlaues part of the thermistor pin datasheet
float B = 2.5698e-4; // Set vlaues part of the thermistor pin datasheet
//INVOKING MILLIS
//lab and this wonderful website: https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing
//shows how to use millis
//Part of the assignment is to no longer use delay as a function, so I will be invoking millis here:
unsigned long previousMillis = 0; // using long because this is time that we are going by
//Previously, I had a delay of 0.5 seconds or 500 milliseconds. Millis works in intervals, so now instead of
//making 500 milliseconds my delay, I will be using that time as my interval with the line below:
long interval = 500;
//I will be using a sweeper server as my output device:
#include <Servo.h>
class Sweeper{
Servo servo;
int servoPin;
int startPos;
int endPos;
int pos; //varying position
int increment; //how much to change per time
int updateInterval;
unsigned long previousMillis;
public:
Sweeper(int interval){
updateInterval = interval;
startPos = 25;
pos = startPos;
endPos = 170;
increment = 1;
}
void Attach(int pin){
servo.attach(pin);
}
void Detach(){
servo.detach();
}
void Open(){
if(millis() - previousMillis > updateInterval){
previousMillis = millis();
pos += increment;
servo.write(pos);
if((pos >= endPos || pos <= startPos)){
increment = -increment;
}
}
}
};
Sweeper boxServo(10); //I am going to be very honest here, I don't understand what this line does,
// but we used in the escape room box lab, and my code won't run without it.
void setup() {
Serial.begin(9600);
boxServo.Attach(9);
}
void loop() {
Vo = analogRead(ThermistorPin);
R2 = R1 * 1/(1023.0 / (float)Vo - 1.0);
//Calculate resistance of thermistor from voltage divider math.
T = (1.0 / (A + B*log(R2/R1) )); // Calculate temperature using datasheet formula.
C = T - 273.15; //Convert from Kelvin to Celcius.
F = ((C)*1.8)+32; //Convert from Celcius to Fahrenheit
//MILLIS IN MY LOOP
//Before, we used a 500 milliseconds delay. Now, we are going to tell arduino to get readings every 500 millisecond interval.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
Serial.print("Resistance : ");
Serial.print(R2);
Serial.print(" Ohms\ \ ");
Serial.print("Temperature in Celsius: ");
Serial.print(C);
Serial.print("C\ \ ");
Serial.print("Temperature in Fahrenheit: ");
Serial.print(F); //Added this column because I understand Fahrenheit better than Celsius
Serial.println("F\ \ ");
if (F > 100) {
boxServo.Open();
}
}
}