FADING ON A NON-PWM PIN OF ARDUINO

Hi guys!

Probably, we all have seen that fade program in the standard examples of arduino IDE that creates cool light fading effect .If you haven’t , I suggest you go watch it right now(Go here :File—> Examples—> Basic —> Fade), who knows it may strike some nice project to your mind like creating elegant colour effects with RGB,making a disco light or something new which your mind can come up with .But , here is the catch, you can’t fade an led on every digital I/O pin that arduino owns.There are some special pins called the PWM pins,which are meant to vary the voltage on the I/O pins and hence vary the intensity of light.

 

Image result for pwm pins on arduino uno pics

In this image above , do you see the little dash next to the pins 11, 10, 9, 6, 5 and 3? This dash is the symbol for pwm. So, we have 6 of these pins that give pwm output in uno.

 

PWM makes it possible to control the voltage output of the pin as per our wish . In a standard fade program we first decrease voltage output and then increase it ,all within the void loop.

For more on pwm , you can view my previous post.

What if,  you are up to creating some cool light fading effects but sadly all your dashed pins are booked up already.The natural Idea would be to get an arduino with greater number of pins.But , who would want that when we have a cheaper way .

int pwm=14;

int timer,interval;

int i=0;

void setup()

{

pinMode(13,OUTPUT);

Serial.begin(9600);

}

void loop()

{

  while(i<=pwm)

{

interval=millis()-timer;

digitalWrite(13,HIGH);

delay(i);

if(i==pwm)

{

 delay(100);

 }

digitalWrite(13,LOW);

delay(pwm);

if(interval>=40)

{

i++;

timer=millis();

}

}

while(i>=0)

 

{

interval=millis()-timer;

digitalWrite(13,HIGH);

delay(i);

digitalWrite(13,LOW);

delay(pwm);

if(i==0)

{

 delay(100);

 }

if(interval>=pwm)

{

i–;

timer=millis();

}

}

i=0;

}

 

So , this was the code optimized fading for every I/O pin.Unlike the normal pwm

 

 it just provide pwm value upto 14. So , better not increase the value more than 14 or else the trick will deceive you. Below ,is the link to the video showing the fading on pin 13.

https://youtu.be/7sfYAI6fRwQ

 

Feel free to comment out your queries and keep TINKERING!

One thought on “FADING ON A NON-PWM PIN OF ARDUINO

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: