Appendix A Project code
//
// FILE: dht11_test1.pde
// PURPOSE: DHT11 library test sketch for Arduino
//
#include <LiquidCrystal.h>
#include "notes.h"
//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(3, 4, 5, 6, 7, 8);
int ledLow=A3;
int noteGood=A2;
int ledHigh=A1;
int tempMode;
int GUITAR_IN=A0;
int peaks = 0;
int sample[660];
float period = 0;
float frequency = 0;
float sampleTime = 0;
float startTime = 0;
float stopTime = 0;
#include "dht11.h"
dht11 DHT11;
#define DHT11PIN 2
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
// fast integer version with rounding
//int Celcius2Fahrenheit(int celcius)
//{
// return (celsius * 18 + 5)/10 + 32;
//}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: [Link]
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: [Link]
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
void setup()
{
/*[Link](9600);
[Link]("SampleTime\tSamples\tPeriod\tFrequency\tPeaks\n");
[Link](115200);
[Link](DHT11LIB_VERSION);
[Link]();*/
[Link](16,2); //sets the number of rows and columns for the LCD display
[Link]();
[Link](0,0);
[Link]("Hello Tech 6100!");
delay(2000);
[Link]();
pinMode(GUITAR_IN, INPUT); //Set the PIN 28(A05) as and input
pinMode(noteGood, OUTPUT);
pinMode(ledLow, OUTPUT);
pinMode(ledHigh, OUTPUT);
}
void loop()
{
/*if(digitalRead(stringSelect==HIGH))
{
note++;
}*/
/******************************************************************
samples the input signal, finds it peak and finds the peaks of the
signal. This time determines period and then inverses that value to
determine frequency
*******************************************************************/
startTime=millis(); //start of sampling
for(int i=0; i<660; i++){ //takes 800 samples of the input
sample[i]=analogRead(GUITAR_IN); //read the guitar input
delay(1);
}
stopTime = millis(); //stop time in miliseconds
sampleTime = ((stopTime-startTime)); //the time it takes for all 800 samples is
equal to stop time-start time
for(int k=0; k<660; k++){
if ((sample[k]>sample[k-1]) & (sample[k]>sample[k+1])) //determines the number of
peaks after 800 samples
{
peaks++;
}
}
period = sampleTime/peaks;
frequency = 1000.0/period;
peaks=0;
int chk = [Link](DHT11PIN);
//[Link](0,0); //set the cursor on line 1
//[Link](frequency); //display the measured frequency
/****************************************************************
lcd commands writing the frequency and note played by the guitar
*****************************************************************/
if ((frequency > 74) && (frequency <90))
{
[Link](0,0); //set the cursor on line 1
[Link]("E");
[Link](" f:");
[Link](frequency); //display the measured frequency
}
else if ((frequency > 102) && (frequency <118))
{
[Link](0,0); //set the cursor on line 1
[Link]("A");
[Link](" f:");
[Link](frequency); //display the measured frequency
}
else if ((frequency > 138) && (frequency <154))
{
[Link](0,0); //set the cursor on line 1
[Link]("D");
[Link](" f:");
[Link](frequency); //display the measured frequency
}
else if ((frequency > 375) && (frequency <395))
{
[Link](0,0); //set the cursor on line 1
[Link]("G");
[Link](" f: 196.00 ");
[Link](frequency); //display the measured frequency
}
else if ((frequency > 238) && (frequency <254))
{
[Link](0,0); //set the cursor on line 1
[Link]("B");
[Link](" f:");
[Link](frequency); //display the measured frequency
}
else if ((frequency > 322) && (frequency <338))
{
[Link](0,0); //set the cursor on line 1
[Link]("E");
[Link](" f:");
[Link](frequency); //display the measured frequency
}
else
{
[Link](0,0); //set the cursor on line 1
[Link](" ");
}
/*[Link](sampleTime);
[Link]("\t\t800\t");
[Link](period);
[Link]("\t");
[Link](frequency);
[Link]("\t");
[Link]("\t");
[Link](peaks);
[Link]("\n");*/
//peaks=0;
delay(500);
/***********************************************************
If else controlling lcd output and note good light. When the
frequency equals that of notes E,A,D,G,B,or E the green
tuning good indicator turns on
***********************************************************/
if((frequency >= (LowE-3)) && (frequency <=(LowE+3))){
digitalWrite(noteGood, HIGH);
}
else if((frequency >= (A-3)) && (frequency <=(A+3)))
{
digitalWrite(noteGood, HIGH);
}
else if((frequency >= (D-3)) && (frequency <=(D+3)))
{
digitalWrite(noteGood, HIGH);
}
else if((frequency >= 382) && (frequency <=389))
{
digitalWrite(noteGood, HIGH);
}
else if((frequency >= (B-3)) && (frequency <=(B+3)))
{
digitalWrite(noteGood, HIGH);
}
else if((frequency >= (HighE-3)) && (frequency <=(HighE+3)))
{
digitalWrite(noteGood, HIGH);
}
else
{
digitalWrite(noteGood, LOW);
}
/***********************************************************
If else controlling the tuning low indicator when approaching a note
************************************************************/
if((frequency >= (LowE-8)) && (frequency <(LowE-3))){
digitalWrite(ledLow, HIGH);
}
else if((frequency >= (A-8)) && (frequency <(A-3))){
digitalWrite(ledLow, HIGH);
}
else if((frequency >= (D-8)) && (frequency <(D-3))){
digitalWrite(ledLow, HIGH);
}
else if((frequency >= 375) && (frequency <382)){
digitalWrite(ledLow, HIGH);
}
else if((frequency >= (B-8)) && (frequency <(B-3))){
digitalWrite(ledLow, HIGH);
}
else if((frequency >= (HighE-8)) && (frequency <(HighE-3))){
digitalWrite(ledLow, HIGH);
}
else
{
digitalWrite(ledLow, LOW);
}
/***********************************************************
If else controlling the tuning high indicator when leaving
a notes boundries
***********************************************************/
if((frequency > (LowE+3)) && (frequency <=(LowE+8)))
{
digitalWrite(ledHigh, HIGH);
}
else if((frequency > (A+3)) && (frequency <=(A+8)))
{
digitalWrite(ledHigh, HIGH);
}
else if((frequency > (D+3)) && (frequency <=(D+8)))
{
digitalWrite(ledHigh, HIGH);
}
else if((frequency > 389) && (frequency < 400))
{
digitalWrite(ledHigh, HIGH);
}
else if((frequency > (B+3)) && (frequency <=(B+8)))
{
digitalWrite(ledHigh, HIGH);
}
else if((frequency > (HighE+3)) && (frequency <=(HighE+8)))
{
digitalWrite(ledHigh, HIGH);
}
else
{
digitalWrite(ledHigh, LOW);
}
/************************************************************
Sets the cursor and displays the temperature and humidity values
on the lcd display
*************************************************************/
[Link](0,1); //set cursor on line 2
[Link]("RH%:");
[Link]((float)[Link], 1); //display humidity to 1 decimal place
[Link](12,1); //set cursor
[Link]((float)[Link], 0); //display temperature in degrees celsius with
no decimals
[Link]((char)223);
[Link]("C");
}
/*[Link]("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
[Link]("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
[Link]("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
[Link]("Time out error");
break;
default:
[Link]("Unknown error");
break;
}*/
/*[Link]("Humidity (%): ");
[Link]((float)[Link], 2);
[Link]("Temperature (oC): ");
[Link]((float)[Link], 2);
[Link]("Temperature (oF): ");
[Link](Fahrenheit([Link]), 2);
[Link]("Temperature (K): ");
[Link](Kelvin([Link]), 2);
[Link]("Dew Point (oC): ");
[Link](dewPoint([Link], [Link]));
[Link]("Dew PointFast (oC): ");
[Link](dewPointFast([Link], [Link]));
//delay(2000);*/
/*else{
startTime=millis();
for(int i=0; i<800; i++){
sample[i]=analogRead(GUITAR_IN);
delay(1);
}
stopTime = millis();
sampleTime = ((stopTime-startTime));
for(int k=0; k<800; k++){
if ((sample[k]>sample[k-1]) & (sample[k]>sample[k+1]))
{
peaks++;
}
}
period = sampleTime/peaks;
frequency = 1000.0/period;
[Link](sampleTime);
[Link]("\t\t800\t");
[Link](period);
[Link]("\t");
[Link](frequency);
[Link]("\t");
[Link]("\t");
[Link](peaks);
[Link]("\n");
peaks=0;*/
//
// END OF FILE
//