Buzzeri kasutamine “Väike Alarm Süsteem”

Funktsioonid

Keskkonnanäitajate mõõtmine:

  • Valgustase – fototakisti (LDR)

Reaalajas kuvamine LCD-ekraanil:

  • Ekraanil kuvatakse valguse tase (nt Valgus: 320)
  • Väärtused uuenevad pidevalt

Automaatne häirefunktsioon:

  • Kui valgus langeb alla teatud taseme (nt pimedus):
    • Ekraanile ilmub tekst „ALARM“
    • Buzzer annab märku (alarmheli või vilkumine)
  • Kui valgus normaliseerub:
    • Häire lõppeb
    • Süsteem taastub

Süsteemi sisse-/väljalülitamine:

  • Potentsiomeetri või lüliti abil
  • Lülitab süsteemi aktiivseks või passiivseks

Taustamuusika (valikuline):

  • Kui kõik on korras, võib mängida rahulik meloodia
  • Kasutatakse tone() funktsiooni

Kasutatud komponendid

  • LDR – valguse mõõtmiseks
  • LCD-ekraan – info kuvamiseks
  • Buzzer – heliliseks häireks
  • Lüliti / potentsiomeeter – süsteemi juhtimiseks
  • Arduino – süsteemi juhtimine

Näide tööloogikast

  1. Süsteem on sisselülitatud
  2. Ekraanil kuvatakse valguse tase
  3. Kui tuvastatakse pimedus (valgus < nt 200):
    • Kuulub alarmheli
    • Ekraanile ilmub „ALARM“
  4. Kui valgus taastub:
    • Alarm vaigistub
    • Ekraanile naaseb tavaline info
#include <LiquidCrystal.h>
 
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);
 
 
const int buzzerPin = 9;
const int alarmSwitchPin = A0;
const int ldrPin = A1;
const int sensorPin = A5;
 
int alarmMode = 0;
int lightThreshold = 600;
 
const char* myStrings[] = {"mistydawn", "skyloop", "emberpath"};

const int songLengthMistyDawn = 8;
const int songLengthSkyLoop = 16;
const int songLengthEmberPath = 16;

const char notesMistyDawn[] = "a e g f d a c e ";
int beatsMistyDawn[] = {1, 1, 2, 1, 2, 1, 1, 2};

const char notesSkyLoop[] = "d e f g a g f e d c b a g f e d";
int beatsSkyLoop[] = {1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1};

const char notesEmberPath[] = "g c d f e d c g f e d c b a g e";
int beatsEmberPath[] = {2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 2};



const char alarmNotes[] = "c c c c ";
int alarmBeats[] = {4, 4, 4, 4};
const int alarmLength = 4;
 
int tempoStillGrey = 60;
int tempoBack2U = 90;
int tempoNorthWinds = 75;
int tempoAlarm = 100;
 
int currentTempo = 75;
int currentOctaveOffset = 0;
 
unsigned long noteStartTime = 0;
int currentNoteIndex = 0;
int currentSongLength = 0;
const char* currentNotes = nullptr;
int* currentBeats = nullptr;
bool isPlaying = false;
 
void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(alarmSwitchPin, INPUT);
  pinMode(ldrPin, INPUT);
  pinMode(sensorPin, INPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  
  
}
 
void loop() {
  int ldrValue = analogRead(ldrPin);
  int alarmSwitchValue = analogRead(alarmSwitchPin);
  int sensorValue = analogRead(sensorPin);
  

 alarmMode = (alarmSwitchValue > 512) ? 1 : 0;
  if (ldrValue < lightThreshold) {
    alarmMode = 1;
  } else {
    alarmMode = 0;
  }
 
  Serial.print("LDR: ");
  Serial.print(ldrValue);
  Serial.print("  Potentiometer: ");
  Serial.print(alarmSwitchValue);
  Serial.print("  Sensor: ");
  Serial.println(sensorValue);

  lcd.setCursor(0, 1);
  lcd.print("Light: ");
  lcd.print(ldrValue);
  lcd.print("    ");


 

if (alarmMode == 1) {
  lcd.setCursor(0, 0);
  lcd.print("ALARM!          ");
  if (!isPlaying || currentNotes != alarmNotes) {
    currentTempo = tempoAlarm;
    currentOctaveOffset = 0;
    startPlaying(alarmNotes, alarmBeats, alarmLength);
  }
} else if (sensorValue < 682) {
  lcd.setCursor(0, 0);
  lcd.print("skyloop         ");
  if (!isPlaying || currentNotes != notesSkyLoop) {
    currentTempo = tempoBack2U;
    currentOctaveOffset = 1;
    startPlaying(notesSkyLoop, beatsSkyLoop, songLengthSkyLoop);
  }
} else {
  lcd.setCursor(0, 0);
  lcd.print("emberpath       ");
  if (!isPlaying || currentNotes != notesEmberPath) {
    currentTempo = tempoNorthWinds;
    currentOctaveOffset = -1;
    startPlaying(notesEmberPath, beatsEmberPath, songLengthEmberPath);
  }
}


 
  updateMelody();
}
 
void startPlaying(const char* notes, int* beats, int length) {
  currentNotes = notes;
  currentBeats = beats;
  currentSongLength = length;
  currentNoteIndex = 0;
  noteStartTime = millis();
  isPlaying = true;
  playNote(currentNotes[currentNoteIndex], currentBeats[currentNoteIndex]);
}
 
void updateMelody() {
  if (!isPlaying) return;
 
  unsigned long currentTime = millis();
  int noteDuration = currentBeats[currentNoteIndex] * currentTempo;
 
  if (currentTime - noteStartTime >= (unsigned long)noteDuration) {
    currentNoteIndex++;
    if (currentNoteIndex >= currentSongLength) {
      currentNoteIndex = 0;
    }
    noteStartTime = currentTime;
    playNote(currentNotes[currentNoteIndex], currentBeats[currentNoteIndex]);
  }
}
 
void playNote(char note, int beat) {
  if (note == ' ') {
    noTone(buzzerPin);
  } else {
    tone(buzzerPin, frequency(note, currentOctaveOffset), beat * currentTempo);
  }
}
 
int frequency(char note, int octaveOffset) {
  const int numNotes = 8;
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int baseFrequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  for (int i = 0; i < numNotes; i++) {
    if (names[i] == note) {
      int freq = baseFrequencies[i];
      if (octaveOffset > 0) return freq << octaveOffset;
      if (octaveOffset < 0) return freq >> -octaveOffset;
      return freq;
    }
  }
  return 0;
}