So I need help programming a project I am currently working on. I feel like this may be fairly simple, but this is one of my first projects using an Arduino and I need lots of help.
I would like to have 4 leds blink at a given rate. Then with the press of a momentary push button, I would like the leds to blink much faster and cause a buzzer to beep. This will act as a rescue alarm for a project I am currently working on.
If possible, I would also like the rapid blinking and buzzer sounding to only last for about 8 seconds before stopping automatically. Thus when the button is pressed again the blinking and buzzing will start again.
I feel like I understand the basics of wiring all of this up, but I am clueless on how to even start with the code. Any help/guidance is greatly appreciated!
I currently have just tried mashing up the codes from a bunch of different example and the code currently wont even compile correctly.
/*
Melody
Plays a melody
circuit:
* 8-ohm speaker on digital pin 8
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"
// notes in the melody:
const int led = 13;
const int led2 = 12;
const int led3 = 11;
int ledState= LOW;
long previousMillis= 0;
long interval = 1500;
const int buttonPin = 2;
int buttonState = 0;
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
2, 2, 2, 2, 2, 2, 2, 2
};
void setup() {
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode (buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = curretMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(led, ledState);
digitalWrite(led2, ledState);
digitalWrite(led3, ledState);
}}
}
else{
digitalWrite(buttonPin, LOW);
do {// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.80;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);} } while;
unsigned long currentMillis = millis ();
if(currentMillis - previousMillis > interval) {
previousMillis = curretMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(led, ledState);
digitalWrite(led2, ledState);
digitalWrite(led3, ledState);
}
}
This is really my first attempt at programming anything so any guidance or reference to previously similar projects would be extremely helpful.
unsigned long lastMillisLED1;
unsigned long flashRate = 1000UL;
unsigned long buzzerMillis;
boolean flag = false;
const byte mySwitch = 2;
const byte LED1 = 5;
const byte buzzerPin = 6;
void setup()
{
Serial.begin(9600);
pinMode(mySwitch, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(buzzerPin, OUTPUT);
} // E N D o f s e t u p ( )
void loop()
{
if (digitalRead(mySwitch) == LOW) //Switch pushed?
{
digitalWrite(buzzerPin, LOW); //Buzzer on
buzzerMillis = millis();
// fast rate
flashRate = 250UL;
flag = true;
}
if(millis() - lastMillisLED1 >= flashRate)
{
digitalWrite(LED1, !digitalRead(LED1));
lastMillisLED1 = lastMillisLED1 + flashRate;
}
if(millis() - buzzerMillis < 8000UL && flag == true)
{
//buzzer on
digitalWrite(buzzerPin,LOW);
}
else
{
//buzzer off
digitalWrite(buzzerPin,HIGH);
//slow rate
flashRate = 1000UL;
}
} // E N D o f l o o p ( )