Friday, 20 July 2012

Simple siren generator for Arduino.

This is a simple siren generator, as mentioned.
We will only improve on this...
Graphics and schematic by Fritzing (http://fritzing.org/ )


/*
 * Siren Generator with adjustable rate
 * Version 0.1
 * Author: Ras B / Keith Gray 
 * Created: 3/5/12
 */
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int SPKR_PIN = 10; //assigning pin 10 to the speaker
const int MAX_FREQ = 10000; //max frequency of the siren wail
const int MIN_FREQ = 3700; //min frequency of the siren wail

int potRead = 0; //potentiometer read in pin Analog 0
int potValue ; //value for potentiometer read 
int FREQ_SKIP ; //frequency skip variable
int DELAY = potValue; // variable for delay linking it to the potValue variable

void setup()
{
  pinMode(SPKR_PIN, OUTPUT);
  pinMode (potRead, INPUT);

  updateDisplay();
  updatefreqSkip();
}
void updateDisplay() //creating a function we can call that updates the LCD
{
  lcd.clear();
  // adding some name to the values we want to display:
  lcd.print("Rate=");
  // calling the values we want to display:
  lcd.print(potValue);
  lcd.print(" Freq=");
  // Print the value of the frequency skip
  lcd.print(FREQ_SKIP);
}

void updatefreqSkip() //creating a function we can call that updates the FREQ_SKIP
{
  if (potValue >= 8)
  { 
    FREQ_SKIP = 10 ;
  }
  else
  {
    FREQ_SKIP = 30 ;
  }
}


void loop()
{
  updatefreqSkip();

  potValue = analogRead(potRead);
  potValue = map(analogRead(potRead), 0, 1023, 0, 10);

  updateDisplay();

  unsigned long siren_start_time = millis();
  int freq;
  updatefreqSkip();
  updateDisplay();
  do
  {
    for (freq=MIN_FREQ; freq<=MAX_FREQ; freq+=FREQ_SKIP)
    {
      analogRead(potRead);
      DELAY = potValue;
      tone(SPKR_PIN, freq);
      delay(DELAY);
    }
    updateDisplay();
    updatefreqSkip();

    for (freq=MAX_FREQ; freq>=MIN_FREQ; freq-=FREQ_SKIP)
    {
      analogRead(potRead);
      DELAY = potValue;
      tone(SPKR_PIN, freq);
      delay(DELAY);
    }
    updateDisplay();
    updatefreqSkip();
  }
  while (millis() - siren_start_time < 5000L);
}


No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless