This time I learned new thing, which is how to control the speaker, I also learned how to build my own llibrary and use it in Arduino. I have created an API that contains the following functions:
/*************************Arduino_api file******************************/
#ifndef __ARDUINO_API_H__
#define __ARDUINO_API_H__
#include "Arduino.h"
#include "consts.h"
/*
Description:
Validates that the AnalogPin used is in the valid range
Input:
ciAnalogPin: Analog pin number
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidAnalogPin (IN const int ciAnalogPin);
/*
Description:
Validates that the DigitalPin used is in the valid range
Input:
ciDigitalPin: Digital pin number
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidDigitalPin (IN const int ciDigitalPin);
/*
Description:
Validates that the PWMPin used is in the valid range
Input:
ciPWMPin: PWM pin number
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidPWMPin (IN const int ciPWMPin);
/*
Description:
Validates that the Note used is in the valid range
Input:
cuiNote: Passed note based on pitches.h
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidNote (IN const unsigned int cuiNote);
/*
Description:
Reads the temperature from the analog pin, then
converts it to celsius which based on it you can
take desicions
Input:
ciTempPin: The pin used to read the temperature
Output:
N/A
Returns:
Returns the temperature in celsius degrees
*/
float ReadTemperature (IN const int ciTempPin);
/*
Description:
Plays the tone based on the passed note and duration
Input:
ciSpeakerPin: The pin used to play the tone
cuiNote: The note passed to be played
culDuration: The duration in milliseconds of the note
Output:
N/A
Returns:
-1: if the pin is not valid digital pin
-2: if the duration equals 0 to avoid divide by zero
-3: if the note is not valid note as in pitches.h
0: if everything is OK
Notes:
To play quarter tone pass the duration = 4, to play half tone
pass the duration = 8 since internal calculation is done which
divides 1000 over the passed culDuration
*/
int PlayTone (
IN const int ciSpeakerPin,
IN const unsigned int cuiNote,
IN const unsigned long culDuration //milliseconds
);
#endif //__ARDUINO_API_H__
/*************************consts.h file**********************************/
#ifndef __CONSTS_H__
#define __CONSTS_H__
// Constants and definitions
#define IN
#define OUT
#define INOUT
#include "pitches.h"
// Valid Analog Pins
const int ciaAnalogPins[] = {A0,A1,A2,A3,A4,A5};
// Valid Digital Pins
const int ciaDigitalPins[] = {2,3,4,5,6,7,8,9,10,11,12,13};
// Valid PWM Pins
const int ciaPWMPins[] = {3,5,6,9,10,11};
// Valid Notes
const unsigned int cuiaValidNotes[] = {
NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1, NOTE_E1, NOTE_F1, NOTE_FS1, NOTE_
G1, NOTE_GS1, NOTE_A1, NOTE_AS1, NOTE_B1, NOTE_C2, NOTE_CS2, NOTE_D2,
NOTE_DS2, NOTE_E2, NOTE_F2, NOTE_FS2, NOTE_G2, NOTE_GS2, NOTE_A2, NOTE_AS2, NOTE
_B2, NOTE_C3, NOTE_CS3, NOTE_D3, NOTE_DS3, NOTE_E3, NOTE_F3, NOTE_FS3,
NOTE_G3, NOTE_GS3, NOTE_A3, NOTE_AS3, NOTE_B3, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE
_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4
, NOTE_B4, NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOT
E_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6
, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NO
TE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS
7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7, NOTE_C8, NOTE_CS8, NOTE_D8, NO
TE_DS8, NOTE_BLANK};
// Forbidden Pins
const int ciRxPin = 0;
const int ciTxPin = 1;
#endif // __CONSTS_H__
/************************ Sketch file ****************************/
/*
Project Name: Temperature Sensor with Sound Alert
Description: This is going to work indefinitely checking for temperature
and if the temperature is critical high it will sound alarm and the red
LED will start flashing.
If the temperature is normal the green LED lights and if the temperature
is LOW then the blue LED lights.
Learned Stuff: 1- Making my own API and include (library) and calling it fr
om
outside.
2- Making the API defensive and created consts.h file that contains
valid inputs (pins and notes).
*/
#include
#define COLD_ALARM_LED 6
#define HOT_ALARM_LED 7
#define NORMAL_LED 8
#define SPEAKER_PIN 10
#define HIGH_TEMP 25.0
#define CRITICAL_HIGH_TEMP 26.0
#define LOW_TEMP 24.0
#define TEMPERATURE_PIN A0
void setup ()
{
Serial.begin (9600);
pinMode (HOT_ALARM_LED, OUTPUT);
pinMode (NORMAL_LED, OUTPUT);
pinMode (COLD_ALARM_LED, OUTPUT);
}
void loop ()
{
float fTemp = ReadTemperature(TEMPERATURE_PIN);
if (fTemp == -100.0)
{
Serial.println ("An Error Occured while reading temperature");
}
if (fTemp >= HIGH_TEMP)
{
digitalWrite (COLD_ALARM_LED, LOW);
digitalWrite (HOT_ALARM_LED, HIGH);
digitalWrite (NORMAL_LED, LOW);
while (fTemp >= CRITICAL_HIGH_TEMP)
{
digitalWrite (HOT_ALARM_LED, LOW);
delay (1000);
digitalWrite (HOT_ALARM_LED, HIGH);
if (PlayTone (SPEAKER_PIN, NOTE_B6, 8) == 0)
if (PlayTone (SPEAKER_PIN, NOTE_A7, 8) == 0)
delay (1000);
fTemp = ReadTemperature(TEMPERATURE_PIN);
if (fTemp == -100.0)
{
Serial.println ("An Error Occured while reading temperature");
}
}
}
else if (fTemp <= LOW_TEMP)
{
digitalWrite (HOT_ALARM_LED, LOW);
digitalWrite (COLD_ALARM_LED, HIGH);
digitalWrite (NORMAL_LED, LOW);
}
else
{
digitalWrite (HOT_ALARM_LED, LOW);
digitalWrite (COLD_ALARM_LED, LOW);
digitalWrite (NORMAL_LED, HIGH);
}
delay (1000);
}
- bool ValidAnalogPin (IN const int ciAnalogPin);
- bool ValidDigitalPin (IN const int ciDigitalPin);
- bool ValidPWMPin (IN const int ciPWMPin);
- bool ValidNote (IN const unsigned int cuiNote);
- float ReadTemperature (IN const int ciTempPin);
- int PlayTone (IN const int ciSpeakerPin, IN const unsigned int cuiNote, IN const unsigned long culDuration);
/*************************Arduino_api file******************************/
#ifndef __ARDUINO_API_H__
#define __ARDUINO_API_H__
#include "Arduino.h"
#include "consts.h"
/*
Description:
Validates that the AnalogPin used is in the valid range
Input:
ciAnalogPin: Analog pin number
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidAnalogPin (IN const int ciAnalogPin);
/*
Description:
Validates that the DigitalPin used is in the valid range
Input:
ciDigitalPin: Digital pin number
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidDigitalPin (IN const int ciDigitalPin);
/*
Description:
Validates that the PWMPin used is in the valid range
Input:
ciPWMPin: PWM pin number
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidPWMPin (IN const int ciPWMPin);
/*
Description:
Validates that the Note used is in the valid range
Input:
cuiNote: Passed note based on pitches.h
Output:
N/A
Returns:
true if valid and false otherwise
*/
bool ValidNote (IN const unsigned int cuiNote);
/*
Description:
Reads the temperature from the analog pin, then
converts it to celsius which based on it you can
take desicions
Input:
ciTempPin: The pin used to read the temperature
Output:
N/A
Returns:
Returns the temperature in celsius degrees
*/
float ReadTemperature (IN const int ciTempPin);
/*
Description:
Plays the tone based on the passed note and duration
Input:
ciSpeakerPin: The pin used to play the tone
cuiNote: The note passed to be played
culDuration: The duration in milliseconds of the note
Output:
N/A
Returns:
-1: if the pin is not valid digital pin
-2: if the duration equals 0 to avoid divide by zero
-3: if the note is not valid note as in pitches.h
0: if everything is OK
Notes:
To play quarter tone pass the duration = 4, to play half tone
pass the duration = 8 since internal calculation is done which
divides 1000 over the passed culDuration
*/
int PlayTone (
IN const int ciSpeakerPin,
IN const unsigned int cuiNote,
IN const unsigned long culDuration //milliseconds
);
#endif //__ARDUINO_API_H__
/*************************consts.h file**********************************/
#ifndef __CONSTS_H__
#define __CONSTS_H__
// Constants and definitions
#define IN
#define OUT
#define INOUT
#include "pitches.h"
// Valid Analog Pins
const int ciaAnalogPins[] = {A0,A1,A2,A3,A4,A5};
// Valid Digital Pins
const int ciaDigitalPins[] = {2,3,4,5,6,7,8,9,10,11,12,13};
// Valid PWM Pins
const int ciaPWMPins[] = {3,5,6,9,10,11};
// Valid Notes
const unsigned int cuiaValidNotes[] = {
NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1, NOTE_E1, NOTE_F1, NOTE_FS1, NOTE_
G1, NOTE_GS1, NOTE_A1, NOTE_AS1, NOTE_B1, NOTE_C2, NOTE_CS2, NOTE_D2,
NOTE_DS2, NOTE_E2, NOTE_F2, NOTE_FS2, NOTE_G2, NOTE_GS2, NOTE_A2, NOTE_AS2, NOTE
_B2, NOTE_C3, NOTE_CS3, NOTE_D3, NOTE_DS3, NOTE_E3, NOTE_F3, NOTE_FS3,
NOTE_G3, NOTE_GS3, NOTE_A3, NOTE_AS3, NOTE_B3, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE
_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4
, NOTE_B4, NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOT
E_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6
, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NO
TE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS
7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7, NOTE_C8, NOTE_CS8, NOTE_D8, NO
TE_DS8, NOTE_BLANK};
// Forbidden Pins
const int ciRxPin = 0;
const int ciTxPin = 1;
#endif // __CONSTS_H__
/************************ Sketch file ****************************/
/*
Project Name: Temperature Sensor with Sound Alert
Description: This is going to work indefinitely checking for temperature
and if the temperature is critical high it will sound alarm and the red
LED will start flashing.
If the temperature is normal the green LED lights and if the temperature
is LOW then the blue LED lights.
Learned Stuff: 1- Making my own API and include (library) and calling it fr
om
outside.
2- Making the API defensive and created consts.h file that contains
valid inputs (pins and notes).
*/
#include
#define COLD_ALARM_LED 6
#define HOT_ALARM_LED 7
#define NORMAL_LED 8
#define SPEAKER_PIN 10
#define HIGH_TEMP 25.0
#define CRITICAL_HIGH_TEMP 26.0
#define LOW_TEMP 24.0
#define TEMPERATURE_PIN A0
void setup ()
{
Serial.begin (9600);
pinMode (HOT_ALARM_LED, OUTPUT);
pinMode (NORMAL_LED, OUTPUT);
pinMode (COLD_ALARM_LED, OUTPUT);
}
void loop ()
{
float fTemp = ReadTemperature(TEMPERATURE_PIN);
if (fTemp == -100.0)
{
Serial.println ("An Error Occured while reading temperature");
}
if (fTemp >= HIGH_TEMP)
{
digitalWrite (COLD_ALARM_LED, LOW);
digitalWrite (HOT_ALARM_LED, HIGH);
digitalWrite (NORMAL_LED, LOW);
while (fTemp >= CRITICAL_HIGH_TEMP)
{
digitalWrite (HOT_ALARM_LED, LOW);
delay (1000);
digitalWrite (HOT_ALARM_LED, HIGH);
if (PlayTone (SPEAKER_PIN, NOTE_B6, 8) == 0)
if (PlayTone (SPEAKER_PIN, NOTE_A7, 8) == 0)
delay (1000);
fTemp = ReadTemperature(TEMPERATURE_PIN);
if (fTemp == -100.0)
{
Serial.println ("An Error Occured while reading temperature");
}
}
}
else if (fTemp <= LOW_TEMP)
{
digitalWrite (HOT_ALARM_LED, LOW);
digitalWrite (COLD_ALARM_LED, HIGH);
digitalWrite (NORMAL_LED, LOW);
}
else
{
digitalWrite (HOT_ALARM_LED, LOW);
digitalWrite (COLD_ALARM_LED, LOW);
digitalWrite (NORMAL_LED, HIGH);
}
delay (1000);
}
No comments:
Post a Comment