0

I recently got into programing and I'm making an alarm. I cant figure out how to make a buzzer loop. I have two blinking LED's and two fading ones. Now I plan to have sound and it won't work. It only blinks once and stops. I would also like the two fading LED's to fade at different times. I can't figure that out. Here's the code. 8 is the buzzer and red and blue are the fading LED's.

int blue = 6;
int red = 9;
int brightness = 0;
int fadeAmount = 500;

void setup() {
  pinMode(11,OUTPUT);
   pinMode(12,OUTPUT);
    pinMode(red, OUTPUT);
     pinMode(blue, OUTPUT);
      pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(11, HIGH);
  digitalWrite(12, LOW);
  delay(200);
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);
  delay(200);
  digitalWrite(8, LOW);
  digitalWrite(8, HIGH);
  delay(10);

 analogWrite(red, brightness);
  analogWrite(blue, brightness);
   brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
  fadeAmount = -fadeAmount;
}
 millis();
}
1

Have a look here, on how you can use different peripherals in a non-blocking manner. The following code is from the link:

// These variables store the flash pattern
// and the current state of the LED

int ledPin1 =  12;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long previousMillis1 = 0;        // will store last time LED was updated
long OnTime1 = 250;           // milliseconds of on-time
long OffTime1 = 750;          // milliseconds of off-time

int ledPin2 =  13;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previousMillis2 = 0;        // will store last time LED was updated
long OnTime2 = 330;           // milliseconds of on-time
byte brightness = 0;
byte brigthnessInterval = 10;

void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);      
  pinMode(ledPin2, OUTPUT);      
}

void loop()
{
  // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();

  if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(ledPin1, ledState1);  // Update the actual LED
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(ledPin1, ledState1);     // Update the actual LED
  }

  if(currentMillis - previousMillis2 >= OnTime2)
  {
    previousMillis2 = currentMillis;  // Remember the time
    if (brigthness <= 254) brigthness += brigthnessInterval;
    if (brigthness == 255) brigthness = 0;
    analogWrite(ledPin2, brigthness);
  }
}

Here, two LEDs are controlled with different intervals, I changed LED number 2 to fade in with an interval specified with OnTime2; watch out your fade amount though, because analogWrite has 255 levels, and you immediately increased it to 500. You can add as many intervals as you want in the same manner, and perform tasks in pseudoparallel.

0

try and analyze this alarm sound sketch

const byte TONE_PIN = 4;
const int ALARM_BEEP_1 = 4186;
const int ALARM_BEEP_2 = 4699;

const int ALARM_TONE_LENGTH = 200;
const int ALARM_TONE_PAUSE = 800;
const int ALARM_TONE_REPEAT = 6;

void setup()
{
}

void loop()
{
  alarmSound();
}

void alarmSound() {
  static unsigned long next = millis();
  static byte count = 0;
  if (millis() > next) {
    next += ALARM_TONE_LENGTH;
    count++;
    if (count == ALARM_TONE_REPEAT) {
      next += ALARM_TONE_PAUSE;
      count = 0;
    }
    tone(TONE_PIN, (count % 2) ? ALARM_BEEP_1 : ALARM_BEEP_2, ALARM_TONE_LENGTH);
  }
}
  • i tried this it sayed alarm sound not declared in scope and i cant figure out how to fix it – jace Sep 26 '18 at 18:06
  • you can't compile it in Arduino IDE? what is "alarm sound"? the function is called alarmSound – Juraj Sep 26 '18 at 18:11
0

I suggested the basics in my answer to your previous question - "To fade them out of phase, you'll need a second brightness-variable (and possibly a second fade-amount), and an increment/decrement and limit test for each LED."

To put that more concretely, instead of brightness, you'll need a redBrightness and a blueBrightness, and initialize them to different values.

   brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
  fadeAmount = -fadeAmount;
}

For instance, initializing them to '0' and '128', respectively, would drive the LEDs 180 degrees out of phase, or one fading up while the other fades down, assuming you use the same fadeAmount variable for both. If you use a redFadeAmount and blueFadeAmount, you can make them fade at different rates, as well.

-1

"I recently got into programing" So start here : https://en.wikipedia.org/wiki/Divide_and_conquer_algorithm

To apply such principle start by finding suitable "blink (SINGLE) LED" in Arduino IDE examples. There is no real need to reinvent the wheel / code when you just starting to learn programming as long as you do not just "cut and paste".

You can blink LED attached to pin 13 without using any additional hardware. Then find an example doing same without using "delay".

Most newcomers have issues with Arduino hidden main function and navigating "loop".

Once you have hang of the loop function you can add multiple "instances" of blinking LED's, and later consolidate these instances into suitable function.

Then use same approach to your buzzer or any other device. Good luck.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.