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

Saturday, September 13, 2014

My First Arduino Project

I had this Basic Arduino Kit from the US (and got it to pass the Jordanian customs) and started learning it.

The first tutorial was about the thermal detector or temperature sensor.  It was a great achievement for me to get it to read the temperature and get it to reflect it on the serial monitor. I also read the first digital lesson regarding lightining up the LED. So now I had to use my basic knowledge about both in a project and thats what i did.

I combined what I learned about the thermal detection along with LED control to create my first project "The Temperature Detection With Basic Alarm".

The idea basically is simple; it will detect the temperature and if it is greater than or equal 22 it will light up the Red Alarm LED and if it is less than or equal 20 it will light up the Blue Alarm LED, otherwise both LEDs are off (which means that I would like to keep the temperature between 20 and 22 exclusive). When I finished, I thought why not adding an LCD to the combination to show up the temperature and that's what I did. Of course this could be connected to an AC controller where you can control the temperature as desired.

Below is the code along with pictures for the circuit.

// Detecting temperture and based on it will light up the red alarm LED
// if the temp grt or equal 22 and will light up the blue alarm LED
// if the is less or equal 20 while at the same time will show the temp on the LCD

#include <LiquidCrystal.h>
const int HOT_ALARM_LED = 7;
const int COLD_ALARM_LED = 6;
const int TEMPERATURE_PIN = A0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup ()
{
  Serial.begin (9600);
  
  pinMode (HOT_ALARM_LED, OUTPUT);
  pinMode (COLD_ALARM_LED, OUTPUT);
  
  lcd.begin(16, 2);
}

void loop ()
{
  while (float fTemp = ReadTemperature())
  {
    if (fTemp >= 22.0)
    {
      digitalWrite (COLD_ALARM_LED, LOW);
      digitalWrite (HOT_ALARM_LED, HIGH);
    }
    else if (fTemp <=20.0)
    {
      digitalWrite (HOT_ALARM_LED, LOW);
      digitalWrite (COLD_ALARM_LED, HIGH);
    }
    else
    {
      digitalWrite (HOT_ALARM_LED, LOW);
      digitalWrite (COLD_ALARM_LED, LOW);
    }
    delay (1000);
  }
}

float ReadTemperature ()
{
  lcd.clear();
  
  float fTemp = 0.0;
  
  fTemp = analogRead (TEMPERATURE_PIN);
  
  fTemp = (fTemp / 1023) * 500;
  
  Serial.print ("Temperature is: ");
  Serial.print (fTemp);
  Serial.println (" C");
  
  lcd.print (fTemp);
  
  return fTemp;
}