my "digitalWrite" is giving me PWM...

I’m trying to modify the example “for” loop in the ide so that I can use PWM from pins 3, 5, and 6. What I ended up with looks like this:

/*
PWM modified “for” loop iteration.
Utilizes pins 3, 5, and 6 to allow PWM dimmed LED’s along the loop
*/

int timer = 500; // timing is in milliseconds

void setup() {
// using a for loop to initialize each pin as an output:
for (int thisPin = 3; thisPin != 4 && thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// loop from the lowest pin to the highest, excluding 4:
for (int thisPin = 3; thisPin != 4 && thisPin < 7; thisPin++) {
// turn the pin fully on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}

// loop from the highest pin to the lowest, once again excluding 4:
for (int thisPin = 6; thisPin != 4 && thisPin >= 3; thisPin–) {
// turn the pin fully on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}

Instead of getting what I anticipate, a two-way directional flash sequence identical to the example sketch I based this off of, what I get is a sequenced flash from pin 6, then 5, then 3. It only flashes in that direction, restarting at pin 6 every time. Additionally, the output from pins 6 and 5 are either reduced voltage or PWM at something like a 50% duty cycle (I don’t have an oscilloscope to be able to tell for sure) while the output from 3 is full brightness.

I don’t get it. Something must be wrong with the first for loop in the main loop, but I don’t know what it is…

TexArduinoAs:
identical to the example sketch I based this off of,

Post that program so we can compare them.

And for BOTH programs please post them using the code button </> so it looks like this. See How to use the Forum

…R

This my code:

/*
  PWM modified "for" loop iteration.
  Utilizes pins 3, 5, and 6 to allow PWM dimmed LED's along the loop
  */

int timer = 500;           // timing is in milliseconds

void setup() {
  // using a for loop to initialize each pin as an output:
  for (int thisPin = 3; thisPin != 4 && thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest, excluding 4:
  for (int thisPin = 3; thisPin != 4 && thisPin < 7; thisPin++) {
    // turn the pin fully on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  // loop from the highest pin to the lowest, once again excluding 4:
  for (int thisPin = 6; thisPin != 4 && thisPin >= 3; thisPin--) {
    // turn the pin fully on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}

This is what I want it to do, but with pins 3, 5, and 6.

This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/ForLoop
 */

int timer = 100;           // The higher the number, the slower the timing.

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 2; thisPin < 8; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 8; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 7; thisPin >= 2; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
}

That is not generating PWM.

for (int thisPin = 3; thisPin != 4 && thisPin < 7; thisPin++) {
    // turn the pin fully on:
    digitalWrite(thisPin, HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

This will turn pin 3 on, pause for 500ms, then turn it off.
Then it gets around to the top of the loop again, does thisPin++. Now thisPin!=4 is no longer true, so it exits that loop.

Similarly, the second for loop will blink pin 6, then pin 5, then exit the for loop, and after that the loop() will start over again and the first for loop will blink pin 3.

It’s not clear what you want it to do, but it sounds like that isn’t it.

You can’t skip numbers in a for loop like that. Use an array to map the iteration number to the pin number, ie

//up top in the global scope:
byte pinList[]={3,5,6}; //byte is fine, and smaller than int

//and the for loops looking something like:

for (byte i=0;i<3;i++) { //again, for values from 0 to 3, byte is fine, don't need a whole int...
    // turn the pin fully on:
    digitalWrite(pinList[i], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(pinList[i], LOW);
  }

Ah, so a for loop bucks if you try to exclude a number because it exits the loop when that condition isn't met... Very well, I thought the && operator would save it from that, but I suppose that was my error.

Thanks! I'll use the array instead!

Or put:

 if(thisPin == 4)continue;

as the first statement in the for loops.

Pete

I’m still pretty new at this, so mixing an if into my for would probably through me for a loop.

Thanks to all of your help, I finally got to where I wanted to be, which was this:

/*
  PWM modified "for" loop iteration.
  Utilizes pins 3, 5, and 6 to allow PWM dimmed LED's along the loop
  The timing of the sequence is controlled by a potentiometer regulating
  a max of 5V into an analog input pin, rendering a value of 0 - 1023.
  */

void setup() {
  // using a for loop to initialize each pin as an output:
  for (int thisPin = 3; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
  //create and integer to hold the analogRead's number
  int sensorValue;
  //set it to equal the number generated by the potentiometer voltage, 0-1023
  sensorValue = analogRead(A1);
  
   //create an array to hold the pin locations of the led's 
  int ledPins[3] = {3,5,6};
            //index 0,1,2

  //set an arbitrary integer to identify the array indexes
  int i;
  
  /* Loop from the lowest pin to the highest by shuffling through the array.
   *  By starting the first loop at index one and ending it at index 1
   *  and starting the second loop at 2 ending at 0, we avoid a double blink
   *  at the terminal ends of the sequence.
   */
  for (i = 1; i <= 1; i++) {
    // turn the pin fully on:
    analogWrite(ledPins[i], 61);
    delay(sensorValue);
    // turn the pin off:
    digitalWrite(ledPins[i], LOW);
  }
//the above loop kicks the value out when it equals 2, and it arrives in the next loop
  for (i = 2; i >= 0; i--) {
    analogWrite(ledPins[i], 61);
    delay(sensorValue);
    // turn the pin off:
    digitalWrite(ledPins[i], LOW);
  }
 //this loop ends, kicking it back to the previous loop
}

A dim LED is often indicative of an I/O that has not been made an output correctly. Keep that in mind.