element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 Help with Arduino 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
  • State Not Answered
  • Replies 3 replies
  • Subscribers 392 subscribers
  • Views 349 views
  • Users 0 members are here
Related

Help with Arduino sketch

gub11
gub11 over 10 years ago

In the testing phase of developing a clap on clap off light switch with an attiny.

First step, test out some code.

[code]

const int buttonPin = 2;

const int ledPin = 0;

 

 

int buttonstate = 0;

int ledstate = 0;

 

void setup() {

 

pinMode(ledPin, OUTPUT);

 

pinMode(buttonPin, INPUT);

}

 

void loop(){

 

buttonstate = digitalRead(buttonPin);

ledstate = digitalRead(ledPin);

 

if (ledstate == HIGH && buttonstate == LOW) {

delay(250);

digitalWrite(ledPin, LOW);

}

if (ledstate == LOW && buttonstate == LOW) {

delay(250);

digitalWrite(ledPin, HIGH);

}

}

[/code]

The above code i have received for the arduino to receive a clap on pin 2, then change the ledstate on pin 0.  I don't really understand how it works though, which is odd considering my strong knowledge of c++, c#, and objective-C.  are HIGH and LOW boolean values like true and false?  Won't this either just constantly loop because it is constantly changing the values?  or will it just never loop because both states start at 0 which is neither HIGH or LOW.  if it does work and somebody understands it, could you explain it to me, and then explain how i would get it to work with two claps, because I wan't to be able to clap twice and the lights go on.  Also, if somebody is willing to work through this with me till the end, i have the circuit all planned out, i'm just very new to the arduino and the namespace, that would be very helpful.

THanks

  • Sign in to reply
  • Cancel
  • Former Member
    0 Former Member over 10 years ago

    I'll try to answer each question separately:

     

    1. Microcontrollers (a.k.a "uC") typically use voltage ranges for digital High and Low.  The defaults for the Arduino (these are rough parameters); 0-2.5VDC is "Low", and 3.4-5.0 is "High".  Yes there's a "dead zone" in there that's used for things like debouncing a switch, for example.  You can also define these parameters programmatically.  Digital High and Low are Boolean operators (otherwise they wouldn't be digital).

    2. Your supposition that the code will infinitely loop through void loop() is correct.  Since there's no breakpoint or exit it will simply continue as long as the uC has power.  This is the type of behavior you would want (using this particular method), as you're constantly polling for a clap (the external input).

    3. This isn't the type of code you'd want to use for something like claps, generally (and I'm certain others have different ideas).  I'd likely use a simple electret microphone that requires a sound >x in dB to generate a digital High that then turns on your lighting circuit with a different pin.  You would still have to constantly poll your input (the microphone) for a digital High.  The Arduino runs about 20ns for a clock cycle, so the resolution would be plenty fine to detect two distinct loud noises.  That brings up another potential problem with a non-specific sound input: Any two sounds at the correct intervals, with enough sound pressure, would trigger most "clappers".  The delay that's used in the example (250ms) is pretty long for this type of detection.  You generally want some delay between inputs or state change to allow the component to debounce; but that's too long.

     

    I would refer you to the Arduino site that has LOTS of code examples.  There's also the Arduino Playground with thousands of posts, questions, and answers on just about any Arduino aspect.

    For an electret microphone (or other analog sensor) here's a good starting place: https://www.arduino.cc/en/Tutorial/AnalogInput

    For digital inputs this page also points out some things that need to be in the code for it to work properly: https://www.arduino.cc/en/Reference/DigitalRead

    There are also many books available that can help you work with Arduino, sensors, and code.  I don't know if Element 14 carries any, but "Getting Started with Arduino" (Massimo Banzi, et al.), "The Arduino Cookbook, 2nd Edition" (Michael Margolis), "Exploring Arduino" (Jeremy Bloom), and anything by Simon Monk are good references for hardware properties and techniques, software syntax and techniques (the Arduino IDE is a subset of C, that's based on the Processing language), and general electronics.

     

    Dealing with uC's is more than just code.  You have to use the properties and attributes of whatever physical electronics you're dealing with.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gub11
    0 gub11 over 10 years ago in reply to Former Member

    Thank you, that was very helpful and i'd love to use analog input,  i just don't know how to, other than i need to use the A# inputs.  any ideas?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to gub11

    Gabriel;

     

    One of the easiest ways is to use an electret microphone breakout board (or BB).  You can find all manner of sensors, outputs, etc. on breakout boards.  These have the main component of interest mounted on a small PCB with supporting sub-circuitry to make them match characteristics of uC boards.  They're not as comprehensive as an Arduino "Shield" (which will use the Arduino Uno R3 mounting footprint, and more comprehensive circuitry), but exceedingly useful for prototyping.  Most of the more well-established companies that make or market them have datasheets for their BB's.

     

    Element 14 has a pretty good selection of Arduino's and Shields; and a huge selection of components.  They will also make custom PCB's for you.  I don't know what country you're in, but "Sparkfun" is a company in the US that has a great selection of all manner of boards, components, and uC's, that they mostly design and manufacture in-house.  I don't believe they market through Element 14 (at least in the US), unfortunately.  Another US company is "Adafruit" (for which Element 14 does market).  They have a great selection of in-house and third-party materials.

     

    Here is a link to the Sparkfun "Sound Detector" BB.  They have a page with use and code examples for almost all of their products:

    https://www.sparkfun.com/products/12642

    Look toward the bottom and click on the "Hookup Guide" for information on how to use the BB.

     

    I don't know how you're going to control the light.  The "usual" way is via a relay to control mains power.  There's a great device that works with Arduino (and Raspberry Pi, BeagleBone Black, etc.) called a "PowerSwitch Tail" (currently on Version 2).  You send it current from power and I/O pins on the uC (3-12VDC@3-50mA) to turn 120VAC/15A on or off.  It's obviously only for US NEMA 5-15 plugs and power.  I don't know if they have a 220VAC version.

     

    And, just for the record, I don't have any financial interest in any of these companies.

     

    A couple of notes from an old Electronics Engineer about prototyping, packaging, and making a production version:

    1. If this is going to be a retail product then work with a company that does PCB design and packaging (like Element 14).  You'll save a LOT of headaches.

    2. If this is going to be something for your own use then be certain to put it in some kind of enclosure.  You can get generic ones and Dremel the heck out of them.

    3. Be aware that uC's have different voltage and power minimums and maximums for the actual uC and components (especially I/O pins).  Make certain to match the usable ranges of components to what the uC uses.  Even within the Arduino family the Uno, Mega, and Pro use a 5VDC uC.  The Due and Zero are 3.3VDC (also written as 3V3).  Most other uC's (PIC, the Broadcom in a Raspberry Pi, the Sitara in a BeagleBone Black, etc.) are 3V3.

     

    I hope this is enough to get you started.

     

    P.S.: If you don't already have basic electronics tools, like a digital multimeter and soldering iron, you'll want to get them.  Most BB's require that you at least solder on the pins.

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