user20473: Why does this code not function in a similar way to the example fade sketch?
I have had to resort to using bit-banging as a way to implement PWM onto non PWM pins due to  a lack of available free pins on my Arduino Pro Mini. I am using a sketch that supposedly should fade an LED ranging from maximum brightness to minimum brightness. My code however does not work. Can anyone figure out why my code does not operate in the same way the fade sketch does?
My current code is:
int brightness = 0;
int fadeAmount = 50;  
void setup()
{
  pinMode(12, OUTPUT);
}
void loop()
{
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  digitalWrite(13, HIGH);
  delayMicroseconds(brightness); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(13, LOW);
  delayMicroseconds(1000 - 100);
}

And I want it to work in the same way as:
int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup() {
  pinMode(led, OUTPUT);
}
void loop() {
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  delay(30);
}

If possible could anyone provide a correct version to help me with my project.

user7856: You seem to have a typo on the pin number: you are configuring pin 12 and driving pin 13.
In other words: pinMode(12, OUTPUT); and digitalWrite(13, HIGH); do not match.

user4143: Your code here is not quite right:
digitalWrite(13, HIGH);
delayMicroseconds(brightness);
digitalWrite(13, LOW);
delayMicroseconds(1000 - 100);

Your two delays should add up to the total period (wavelength), or the reciprocal of the frequency (scaled to microseconds).
Hence the - 100 in the second delay - that is subtracting the "on" time from the total time to give the "off" time.   It should be replaced with - brightness as such:
digitalWrite(13, HIGH);
delayMicroseconds(brightness);
digitalWrite(13, LOW);
delayMicroseconds(1000 - brightness);

And of course the brightness is in the range 0 - 1000 not 0 - 255 like analogWrite().  To make it 0-255 you should replace the 1000 with 255, which of course makes it all run at a much higher frequency. (1 / 0.000255 = 3922Hz).
digitalWrite(13, HIGH);
delayMicroseconds(brightness);
digitalWrite(13, LOW);
delayMicroseconds(255 - brightness);

Mind you, all this is not good regardless of your calculations. This is a blocking system in which you are unable to do anything else at all in your program whilst generating PWM.  Instead you should be using a timer (like the tone() command does) and switch between two different time periods each interrupt for "on" and "off" time periods. That way it can function asynchronously ("in the background") like analogWrite() does.

user37948: It's not strongly related to your question but i suggest you to use external library as SoftPWM.h 
You can find the whole documentation on https://github.com/bhagman/SoftPWM

20473: <p>Why does this code not function in a similar way to the example fade sketch?
I have had to resort to using bit-banging as a way to implement PWM onto non PWM pins due to  a lack of available free pins on my Arduino Pro Mini. I am using a sketch that supposedly should fade an LED ranging from maximum brightness to minimum brightness. My code however does not work. Can anyone figure out why my code does not operate in the same way the fade sketch does?</p>
<p>My current code is:</p>
<pre><code>int brightness = 0;
int fadeAmount = 50;  
void setup()
{
  pinMode(12, OUTPUT);
}
void loop()
{
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  digitalWrite(13, HIGH);
  delayMicroseconds(brightness); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(13, LOW);
  delayMicroseconds(1000 - 100);
}
</code></pre>
<p>And I want it to work in the same way as:</p>
<pre><code>int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup() {
  pinMode(led, OUTPUT);
}
void loop() {
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  delay(30);
}
</code></pre>
<p>If possible could anyone provide a correct version to help me with my project.</p>
 7856: <p>You seem to have a typo on the pin number: you are configuring pin 12 and driving pin 13.</p>

<p>In other words: <code>pinMode(12, OUTPUT);</code> and <code>digitalWrite(13, HIGH);</code> do not match.</p>
 4143: <p>Your code here is not quite right:</p>

<pre><code>digitalWrite(13, HIGH);
delayMicroseconds(brightness);
digitalWrite(13, LOW);
delayMicroseconds(1000 - 100);
</code></pre>

<p>Your two delays should add up to the total period (wavelength), or the reciprocal of the frequency (scaled to microseconds).</p>

<p>Hence the <code>- 100</code> in the second delay - that is subtracting the "on" time from the total time to give the "off" time.   It should be replaced with <code>- brightness</code> as such:</p>

<pre><code>digitalWrite(13, HIGH);
delayMicroseconds(brightness);
digitalWrite(13, LOW);
delayMicroseconds(1000 - brightness);
</code></pre>

<p>And of course the brightness is in the range 0 - 1000 not 0 - 255 like <code>analogWrite()</code>.  To make it 0-255 you should replace the 1000 with 255, which of course makes it all run at a much higher frequency. (1 / 0.000255 = 3922Hz).</p>

<pre><code>digitalWrite(13, HIGH);
delayMicroseconds(brightness);
digitalWrite(13, LOW);
delayMicroseconds(255 - brightness);
</code></pre>

<p>Mind you, all this is not good regardless of your calculations. This is a <em>blocking</em> system in which you are unable to do anything else at all in your program whilst generating PWM.  Instead you should be using a timer (like the <code>tone()</code> command does) and switch between two different time periods each interrupt for "on" and "off" time periods. That way it can function asynchronously ("in the background") like <code>analogWrite()</code> does.</p>
 37948: <p>It's not strongly related to your question but i suggest you to use external library as SoftPWM.h </p>

<p>You can find the whole documentation on <a href="https://github.com/bhagman/SoftPWM" rel="nofollow noreferrer">https://github.com/bhagman/SoftPWM</a></p>
