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
  • 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 Make LEDs blink in rhythm with music code
  • 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
  • State Suggested Answer
  • Replies 9 replies
  • Answers 4 answers
  • Subscribers 404 subscribers
  • Views 2446 views
  • Users 0 members are here
Related

Make LEDs blink in rhythm with music code

Former Member
Former Member over 13 years ago

Hello fellow Arduino users,

 

I have a code that plays the Popcorn song, feel free to use it or change it for yourself:

 

#include "pitches.h"

//The Pin for 8ohm speaker is 8....

int melody[] = {

  NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,

  NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,

  NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_D6, NOTE_DS6, NOTE_C6, NOTE_D6, NOTE_C6,

  NOTE_D6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_C6, 0,

  NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,

  NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_DS5, NOTE_G5, NOTE_C5, 0,

  NOTE_C6, NOTE_D6, NOTE_DS6, NOTE_D6, NOTE_DS6, NOTE_C6, NOTE_D6, NOTE_C6,

  NOTE_D6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_G5, NOTE_C6,

};

 

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {

  4,4,4,4,4,4,4,4,

  4,4,4,4,4,4,4,4,

  4,4,4,4,4,4,4,4,

  4,4,4,4,4,4,4,4,

  4,4,4,

  4,4,4,4,4,4,4,4,

  4,4,4,4,4,4,4,4,

  4,4,4,4,4,4,4,4,

  4,4,4,4,4,4

  };

void setup() {

  for (int thisNote = 0; thisNote < 64; thisNote++) {

    int noteDuration = 900/noteDurations[thisNote];

    tone(8, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1;

    delay(pauseBetweenNotes);

    noTone(8);

  }

 

}

void loop() {

}

 

I want to make the LEDs blink in rhythm with the beeps of my tiny Arduino "player"

 

I have an Arduino Mega 2560 R3

 

What can I add to my code to make one or more LEDs blink in rhythm with the music?

  • Sign in to reply
  • Cancel

Top Replies

  • windsr
    windsr over 13 years ago in reply to Former Member +1
    I like your idea too. I haven't done any programming for about 20yrs so I'm learning again, these little projects are a great place to learn. Thanks for sharing.
  • billabott
    billabott over 13 years ago in reply to ntewinkel +1
    Hi Nico. With regards to the stripboard layout method shown in the digikey project referenced above. What would you recommend for a smaller size board of 20 x 46 hole pattern They say to "Mark the top…
Parents
  • ntewinkel
    0 ntewinkel over 13 years ago

    Could you not just add a few functions to handle the lights simultaneously to the sounds?

     

    I'm thinking like this, which turns on the LED whenever a sound is being made:

     

        tone(8, melody[thisNote]); // don't specify duration here

         light(13); // this is the built-in LED

        delay(noteDuration);

        noTone(8);

        noLight(13);

     

        int pauseBetweenNotes = noteDuration * 1;

        delay(pauseBetweenNotes);

     

    and then the funtion named light would just set the pin high to turn on the LED and noLight does the opposite:

     

    void light(int led) {

       digitalWrite(led, HIGH);

    }

    void noLight(int led) {

      digitalWrite(led, LOW);

    }

     

    In setup you would also need to set the LED pins as output, maybe like this:

     

    void setup() {

      initLight(13); // initialize the LED to be ready for use    

    }

     

    and then add the function for init:

     

    void initLight(int led) {

      pinMode(led, OUTPUT);

    }

     

    Hope that helps!

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to ntewinkel

    Hi Nico. 

    With regards to the stripboard layout method shown in the digikey project referenced above.

    What would you recommend for a smaller size board of 20 x 46  hole pattern

    They say to "Mark the top side of the proto  board with the interleaved PCB bus pattern to aid in assembly".

    But do not specify where and when to cut the traces.  Can you shed some light on this, please?

    Adapting their layout directly, I got this result:

    image

    It appears that the maximum capacity is four 20 pin sockets plus connection points for a limited number of supporting components.

    What is your opinion?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to billabott

    Hi Billabott,

     

    I have to admit that I'm more of a software guy myself, and have not done much in terms of layout. I've been mostly using breadboards for my projects, and every so often I will solder a project onto a little board, and that's the extent of my hardware knowledge! So any advice I could give here would be not much more than theoretical.

     

    Hopefully someone else in the group will be an expert in this and can give you some advice image

     

    Sorry I can't be of more help there.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to billabott

    Hi Billabott,

     

    I have to admit that I'm more of a software guy myself, and have not done much in terms of layout. I've been mostly using breadboards for my projects, and every so often I will solder a project onto a little board, and that's the extent of my hardware knowledge! So any advice I could give here would be not much more than theoretical.

     

    Hopefully someone else in the group will be an expert in this and can give you some advice image

     

    Sorry I can't be of more help there.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • 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 © 2025 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.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube