Saturday, September 20, 2014

Third Arduino Project

This time it was influenced by my younger brother to write a code that will detect the distenation and if it is more than 100 cm to light up a green light, if it is less than 100 but more than or equal 50 to light up a blue LED and if it is less than 50 to light up a red LED. If the distance is critically small (less than or equal 10 cm) to start flashing the red LED and play a long alarming tone. Then we modified the code to play a smaller alarming tone on other distenations too.

Shown is the circuit and below is the code.









#include

#define TRIG_PIN 5
#define ECHO_PIN 6

#define RED_LED 8
#define GREEN_LED 9
#define BLUE_LED 10

#define SPEAKER_PIN 12

void setup ()
{
  Serial.begin (9600);
 
  PinMode (TRIG_PIN, OUTPUT);
  PinMode (ECHO_PIN, INPUT);
 
  PinMode (RED_LED, OUTPUT);
  PinMode (GREEN_LED, OUTPUT);
  PinMode (BLUE_LED, OUTPUT);

  PinMode (SPEAKER_PIN, OUTPUT);
}

void loop ()
{
  DigitalWrite (TRIG_PIN, HIGH);
  delayMicroseconds (100);
  DigitalWrite (TRIG_PIN, LOW);
  delayMicroseconds (100);
  DigitalWrite (TRIG_PIN, HIGH);

  int iPulseLength = pulseIn (ECHO_PIN, HIGH);
  float fDistance = iPulseLength / 29.387 / 2;

 Serial.print ("The distance in Centimeters is: ");
 Serial.println (fDistance);

 if (fDistance <= 10.0)
 {
    DigitalWrite (GREEN_LED, LOW);
    DigitalWrite (BLUE_LED, LOW);
   
    DigitalWrite (RED_LED, HIGH);
    PlayTone (SPEAKER_PIN, NOTE_B6, 2);
    delay (500);
    DigitalWrite (RED_LED, LOW);
    //PlayTone (SPEAKER_PIN, NOTE_G3, 4);
    //delay (500);
 }
 else if (fDistance > 10 && fDistance <= 50.0)
 {
   DigitalWrite (RED_LED, HIGH);
   DigitalWrite (GREEN_LED, LOW);
   DigitalWrite (BLUE_LED, LOW);

   PlayTone (SPEAKER_PIN, NOTE_B6, 4);
   delay (500);
 }
 else if (fDistance > 50.0 && fDistance <= 100.0)
 {
   DigitalWrite (RED_LED, LOW);
   DigitalWrite (GREEN_LED, LOW);
   DigitalWrite (BLUE_LED, HIGH);

   PlayTone (SPEAKER_PIN, NOTE_B6, 8);
   delay (500);
 }
 else //fDistance > 100
 {
   DigitalWrite (RED_LED, LOW);
   DigitalWrite (GREEN_LED, HIGH);
   DigitalWrite (BLUE_LED, LOW);
 }

 delay (1000);
}

Friday, September 19, 2014

Second Arduino Project

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:
  1. bool ValidAnalogPin (IN const int ciAnalogPin);
  2. bool ValidDigitalPin (IN const int ciDigitalPin);
  3. bool ValidPWMPin (IN const int ciPWMPin);
  4. bool ValidNote (IN const unsigned int cuiNote);
  5. float ReadTemperature (IN const int ciTempPin);
  6. int PlayTone    (IN const int ciSpeakerPin, IN const unsigned int cuiNote, IN const unsigned long culDuration); 
I also created an include file that includes different constants and definitions that I will be using in all my upcoming Arduino projects.


/*************************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);
}