Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Source Code-234

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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: http://wahiduddin.net/calc/density_algorithms.htm
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: http://en.wikipedia.org/wiki/Dew_point
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()
{
/*Serial.begin(9600);
Serial.print("SampleTime\tSamples\tPeriod\tFrequency\tPeaks\n");
Serial.begin(115200);
Serial.println(DHT11LIB_VERSION);
Serial.println();*/
lcd.begin(16,2); //sets the number of rows and columns for the LCD display
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hello Tech 6100!");
delay(2000);
lcd.clear();
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 = DHT11.read(DHT11PIN);

//lcd.setCursor(0,0); //set the cursor on line 1


//lcd.print(frequency); //display the measured frequency
/****************************************************************
lcd commands writing the frequency and note played by the guitar
*****************************************************************/
if ((frequency > 74) && (frequency <90))
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print("E");
lcd.print(" f:");
lcd.print(frequency); //display the measured frequency
}
else if ((frequency > 102) && (frequency <118))
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print("A");
lcd.print(" f:");
lcd.print(frequency); //display the measured frequency
}
else if ((frequency > 138) && (frequency <154))
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print("D");
lcd.print(" f:");
lcd.print(frequency); //display the measured frequency
}
else if ((frequency > 375) && (frequency <395))
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print("G");
lcd.print(" f: 196.00 ");
lcd.print(frequency); //display the measured frequency
}
else if ((frequency > 238) && (frequency <254))
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print("B");
lcd.print(" f:");
lcd.print(frequency); //display the measured frequency
}
else if ((frequency > 322) && (frequency <338))
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print("E");
lcd.print(" f:");
lcd.print(frequency); //display the measured frequency
}
else
{
lcd.setCursor(0,0); //set the cursor on line 1
lcd.print(" ");
}

/*Serial.print(sampleTime);
Serial.print("\t\t800\t");
Serial.print(period);
Serial.print("\t");
Serial.print(frequency);
Serial.print("\t");
Serial.print("\t");
Serial.print(peaks);
Serial.print("\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
*************************************************************/

lcd.setCursor(0,1); //set cursor on line 2


lcd.print("RH%:");
lcd.print((float)DHT11.humidity, 1); //display humidity to 1 decimal place
lcd.setCursor(12,1); //set cursor
lcd.print((float)DHT11.temperature, 0); //display temperature in degrees celsius with
no decimals
lcd.print((char)223);
lcd.print("C");
}

/*Serial.print("Read sensor: ");


switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}*/

/*Serial.print("Humidity (%): ");


Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (oC): ");


Serial.println((float)DHT11.temperature, 2);

Serial.print("Temperature (oF): ");


Serial.println(Fahrenheit(DHT11.temperature), 2);

Serial.print("Temperature (K): ");


Serial.println(Kelvin(DHT11.temperature), 2);

Serial.print("Dew Point (oC): ");


Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

Serial.print("Dew PointFast (oC): ");


Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));

//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;
Serial.print(sampleTime);
Serial.print("\t\t800\t");
Serial.print(period);
Serial.print("\t");
Serial.print(frequency);
Serial.print("\t");
Serial.print("\t");
Serial.print(peaks);
Serial.print("\n");
peaks=0;*/

//
// END OF FILE
//

You might also like