1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

i need a circuit and code to generate pwm

Discussion in 'Arduino' started by formerX, Nov 4, 2008.

  1. formerX

    formerX New Member

    Joined:
    Nov 4, 2008
    Messages:
    1
    Likes:
    0
    i need a circuit and code to generate pwm.
    i have a code to generate pwm.

    Arduino - Fading

    i get fading of led's and its shows pwm.
    its showing dimmming light and some point it shut off.
    but i need to make it as an on and off.
    this is my code to make it on and off through pwm.

    Code (text):
    int value = 0; // variable to keep the actual value
    int ledpin = 9; // light connected to digital pin 9

    void setup()
    {
    // nothing for setup
    }

    void loop()
    {
    for(value = 0 ; value <= 174; value+=5) // fade in (from min to max)
    {
    analogWrite(ledpin, value); // sets the value (range from 0 to 255)
    delay(30); // waits for 30 milli seconds to see the dimming effect
    }
    for(value = 174; value >=255; value-=5) // fade out (from max to min)
    {
    analogWrite(ledpin, value);
    delay(30);
    }
    but stilll it is not working.
    can u help me out.
     
    Last edited by a moderator: May 29, 2010
  2. magnatro

    magnatro New Member

    Joined:
    Mar 23, 2010
    Messages:
    295
    Likes:
    0
    Location:
    India
    your application?
     
  3. Manube

    Manube New Member

    Joined:
    May 29, 2010
    Messages:
    1
    Likes:
    0
    Location:
    Lincoln, NE
    If you want your LED to fade all the way in and out you should change 174's to 255. Also in your 2nd for satment you need to change 255 to 0 so your logic makes sence. Since you initalize your value in each for ststment you don't need to initalize it in your variable declaration but that's just a clean code issue. One last thing I'm not sure if you copied the code incompletely but there should be one more } at the end.

    -Wayne
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Edd

    Edd New Member

    Joined:
    Oct 24, 2007
    Messages:
    41
    Likes:
    0
    Location:
    Australia

    void setup() needs another line:

    Code (text):
    pinMode(ledPin, OUTPUT);
     
    Last edited: Jun 4, 2010
  6. emc2

    emc2 New Member

    Joined:
    Jan 28, 2010
    Messages:
    21
    Likes:
    0
    This code looks incorrect:

    Code (text):
    for(value = 174; value >=255; value-=5) // fade out (from max to min)
    change to:

    Code (text):
    for(value = 174; value >= 0; value-=5) // fade out (from max to min)
     
    Last edited: Jun 5, 2010

Share This Page