element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Sound sketch
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 6 replies
  • Subscribers 418 subscribers
  • Views 673 views
  • Users 0 members are here
Related

Sound sketch

e14 Contributor
e14 Contributor over 13 years ago

This sketch has been edited a dozen different ways with the same result; sounds 1~5 fire as they should but the random buzzer emits only a single beep.  The sounds are salvaged from children't toys triggered by a photocell. In a separate sketch, the buzzer is functional but not here.  I assume this is a software error.  Appreciate any suggestions.

 

int drummer = 12;

int siren = A3;

long randOn =0;

long randOff =0;

 

 

int snd1 = 5;

int snd2 = 6;

int snd3 = 7;

int snd4 = 8;

int snd5 = 9;

 

 

int sensorPin = A0;

int sensorValue = 0;

 

 

void setup(){

  pinMode(drummer, OUTPUT);

  pinMode(siren, OUTPUT);

  randomSeed (analogRead (0));

 

 

  pinMode(snd1, OUTPUT);

  pinMode(snd2, OUTPUT);

  pinMode(snd3, OUTPUT);

  pinMode(snd4, OUTPUT);

  pinMode(snd5, OUTPUT);

  digitalWrite(snd1,LOW);

  digitalWrite(snd2,LOW);

  digitalWrite(snd3,LOW);

  digitalWrite(snd4,LOW);

  digitalWrite(snd5,LOW);

 

 

  Serial.begin(9600);

  delay(500); 

}

void loop()

{

  sensorValue = analogRead(sensorPin);

  Serial.println(sensorValue);

  delay(500);

 

 

  if (sensorValue > 15)

  {

   

    digitalWrite(snd1,HIGH);

    delay(5000);

    digitalWrite(snd1,LOW);

    //delay(3000);

 

 

    digitalWrite(snd2,HIGH);

    delay(5000);

    digitalWrite(snd2,LOW);

    // delay(3000);

 

 

    digitalWrite(snd3,HIGH);

    delay(5000);

    digitalWrite(snd3,LOW);

 

 

    digitalWrite(snd4,HIGH);

    delay(5000);

    digitalWrite(snd4,LOW);

 

 

    digitalWrite(snd5,HIGH);

    delay(5000);

    digitalWrite(snd5,LOW);

    delay(1000);

 

    randOn = random (50, 500); 

    randOff = random (1, 900);

    analogWrite(siren,50);

    delay(randOn);

    analogWrite(siren,200);

    delay(randOn);

 

 

    analogWrite (siren, LOW);

    delay(randOff);

  }

}

  • Sign in to reply
  • Cancel
Parents
  • mcb1
    mcb1 over 13 years ago

    Keith

     

    From your description the buzzer is an external sounder/speaker.

     

    If its a device you place volts across and it makes noise, then you need to treat it as a digital output with a random amount of ON time as you wish.

     

    If its more like a speaker then I think you need 'tone', which generates a square wave at a certain frequency.

    http://arduino.cc/en/Reference/Tone

     

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 13 years ago in reply to mcb1

    In my mind, there is a huge difference between a 'single beep' and a 'single click'.  I hope your understanding is correct, Mark B.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • e14 Contributor
    e14 Contributor over 13 years ago in reply to billabott

    Thanks. I switched pins to D3 and edited the analog to digital.  The sketch is working. Well, not exactly as it should. Firstly, there is a long, ten second delay after running through the sounds before starting once again.  And, when the sensor goes below the threshold, the sketch continues to the end before stopping.  To stop immediately, is an interrupt necessary? I have no experience with interrupts but will dive into it if advised this is the solution.  Thanks.

     

     

    int siren = 3;

    long randOn =0;

    long randOff =0;

     

     

    int snd1 = 5;

    int snd2 = 6;

    int snd3 = 7;

    int snd4 = 8;

    int snd5 = 9;

     

     

    int sensorPin = A0;

    int sensorValue = 0;

     

     

    void setup(){

      pinMode(drummer, OUTPUT);

      pinMode(siren, OUTPUT);

      randomSeed (analogRead (0));

     

     

      pinMode(snd1, OUTPUT);

      pinMode(snd2, OUTPUT);

      pinMode(snd3, OUTPUT);

      pinMode(snd4, OUTPUT);

      pinMode(snd5, OUTPUT);

      digitalWrite(snd1,LOW);

      digitalWrite(snd2,LOW);

      digitalWrite(snd3,LOW);

      digitalWrite(snd4,LOW);

      digitalWrite(snd5,LOW);

     

     

      Serial.begin(9600);

      delay(500); 

    }

    void loop()

    {

      sensorValue = analogRead(sensorPin);

      Serial.println(sensorValue);

      delay(500);

     

     

      if (sensorValue > 15)

      {

        randOn = random (50, 5000);

        randOff = random (1, 900);

        digitalWrite(siren,HIGH);

        delay(randOn);

     

     

        digitalWrite(siren,LOW);

        delay(randOff);

     

     

        digitalWrite(snd1,HIGH);

        delay(5000);

        digitalWrite(snd1,LOW);

        //delay(3000);

     

     

        digitalWrite(snd2,HIGH);

        delay(5000);

        digitalWrite(snd2,LOW);

        // delay(3000);

        digitalWrite(siren,HIGH);

        delay(randOn);

        digitalWrite(siren,LOW);

        delay(randOff);

       

     

     

        digitalWrite(snd3,HIGH);

        delay(5000);

        digitalWrite(snd3,LOW);

     

     

        digitalWrite(snd4,HIGH);

        delay(5000);

        digitalWrite(snd4,LOW);

     

     

        digitalWrite(snd5,HIGH);

        delay(5000);

        digitalWrite(snd5,LOW);

        //delay(1000);

      }  

      }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • mcb1
    mcb1 over 13 years ago in reply to e14 Contributor

    Keith

    And, when the sensor goes below the threshold, the sketch continues to the end before stopping

    A wee comparison for those who might be reading this.

    You know a courier/delivery is coming to drop off a parcel at your home, and you are at work.

    You call home to ask if it's there, before doing 'something' (4 or 5 tasks) at work that takes 1 hour.

    After each 'something' you ring home.

     

    How do you know if the parcel has arrived while you're doing 'something'  ?

     

    You could change your 'if' to 'while' (use Help-reference in the IDE) which effectively keeps 'phoning home' all the time.

    The alternative is to add sensorValue = analogRead(sensorPin); and if (sensorValue > 15) at the top of each different sound.

     

    there is a long, ten second delay after running through the sounds before starting once again

    I presume you hear the sounds stop and then 10 secs later you see the serial print on the Serial monitor.?

    I suggest adding a Serial.println("snd X"); statement before each sound is played to help sort out where it is delayed.

     

    Your last three (snd3,4,5) have 5 sec ON periods, however that doesn't explain the 10 sec delay before starting again.

     

    I would also suggest uncommenting the last delay and dropping the time to 100 or 200mS to let everything settle before it returns to read the analog input.

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • mcb1
    mcb1 over 13 years ago in reply to e14 Contributor

    Keith

    And, when the sensor goes below the threshold, the sketch continues to the end before stopping

    A wee comparison for those who might be reading this.

    You know a courier/delivery is coming to drop off a parcel at your home, and you are at work.

    You call home to ask if it's there, before doing 'something' (4 or 5 tasks) at work that takes 1 hour.

    After each 'something' you ring home.

     

    How do you know if the parcel has arrived while you're doing 'something'  ?

     

    You could change your 'if' to 'while' (use Help-reference in the IDE) which effectively keeps 'phoning home' all the time.

    The alternative is to add sensorValue = analogRead(sensorPin); and if (sensorValue > 15) at the top of each different sound.

     

    there is a long, ten second delay after running through the sounds before starting once again

    I presume you hear the sounds stop and then 10 secs later you see the serial print on the Serial monitor.?

    I suggest adding a Serial.println("snd X"); statement before each sound is played to help sort out where it is delayed.

     

    Your last three (snd3,4,5) have 5 sec ON periods, however that doesn't explain the 10 sec delay before starting again.

     

    I would also suggest uncommenting the last delay and dropping the time to 100 or 200mS to let everything settle before it returns to read the analog input.

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube