Einfacher Sketch für ein Blaulicht mit Sirene.
const int Note_A = 440;
const int Note_D = 585;
Blink und Sirenendauer: Änderung (rote Zahl)
TinyTone(Note_A, 4, 600);
digitalWrite(3, HIGH);
TinyTone(Note_D, 4, 600);
digitalWrite(3, LOW);
Laufzeit: Änderung (rote Zahl)
if (lcount > 39) { digitalWrite(1, LOW); }
if (lcount > 39) { digitalWrite(3, LOW); }
if (lcount > 39) { digitalWrite(4, LOW); }
if (lcount > 39) { goToSleep(); }
Code:
#include <avr/sleep.h>
#include <avr/interrupt.h>
#define BODS 7
#define BODSE 2
uint8_t mcucr1, mcucr2;
const int Note_A = 440;
const int Note_D = 585;
int lcount = 0;
int Speaker = 1;
void goToSleep(void) {
ACSR |= _BV(ACD);
ADCSRA &= ~_BV(ADEN);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
cli();
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE);
mcucr2 = mcucr1 & ~_BV(BODSE);
MCUCR = mcucr1;
MCUCR = mcucr2;
sei();
sleep_cpu();
cli();
sleep_disable();
sei();
}
void setup()
{
pinMode(Speaker, OUTPUT);
pinMode(0, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(3, HIGH);
digitalWrite(0, LOW);
digitalWrite(2, LOW);
}
void loop()
{
playTune();
delay(1);
lcount += 1;
if (lcount > 39) { digitalWrite(1, LOW); }
if (lcount > 39) { digitalWrite(3, LOW); }
if (lcount > 39) { digitalWrite(4, LOW); }
if (lcount > 39) { goToSleep(); }
}
void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration)
{
TCCR1 = 0x90 | (8-octave);
OCR1C = divisor-1;
delay(duration);
TCCR1 = 0x90;
}
void playTune(void)
{
TinyTone(Note_C, 4, 650);
digitalWrite(4, HIGH);
TinyTone(Note_B, 4, 650);
digitalWrite(4, LOW);
}