code running backward

Hi all,

I'm following a YT personality that teaches Arduino programming, Paul McWhorter. I have a "homework lesson." 4 LED's are supposed to flash the binary equivalent of 0 through 15. Funny thing is that the sketch seems to run in reverse by starting with 15. Why would this happen? Something in my code obviously but I can't see it.

Best,

Tony

Happy Mother's Day to any mothers out there.

//
// Paul McWhorter "homework"
// Write to LEDs, 0 - 15 in binary
//
//


void setup() {

pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
pinMode(9, OUTPUT);
pinMode(11, OUTPUT);

}

void loop() {

int pinFive = 5;
int pinSeven = 7;
int pinNine = 9;
int pinEleven = 11;

int delayTime = 1000;

// 0
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 1
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 2
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 3
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 4
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 5
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 6
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 7 
digitalWrite(pinFive, LOW);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 8
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 9
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 10
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 11
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, LOW);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 12
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 13
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, LOW);
digitalWrite(pinEleven, HIGH);

delay(delayTime);


// 14
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, LOW);

delay(delayTime);


// 15
digitalWrite(pinFive, HIGH);
digitalWrite(pinSeven, HIGH);
digitalWrite(pinNine, HIGH);
digitalWrite(pinEleven, HIGH);

delay(delayTime);



}

Wiring, I assume. Remove all but the code for zero - do all the LEDs come on?

wild,

they do come on for zero. let me take another look at what i have on the breadboard.

t

wild,

you were right. thank you. have a great day.

best,

t

When you start seeing the same lines of code over and over again, it's time for a function, and possibly some loops

(uncompiled, untested)

//
// Write to LEDs, 0 - 15 in binary
//
// "one" "two" etc, are the binary weights, not the pin number
const byte onePin   = 11;
const byte twoPin   = 9;
const byte fourPin  = 7;
const byte eightPin = 5;

const int delayTime = 1000;

const byte ON  = HIGH; // may need to be swapped over
const byte OFF = LOW;  // depending on how LEDs are wired

void setup() 
{
  pinMode(onePin, OUTPUT);
  pinMode(twoPin, OUTPUT);
  pinMode(fourPin, OUTPUT);
  pinMode(eightPin, OUTPUT);
}

void lightDigit (byte val)
{
  digitalWrite (onePin,  val & 1 ? ON : OFF);
  digitalWrite (twoPin,  val & 2 ? ON : OFF);
  digitalWrite (fourPin, val & 4 ? ON : OFF);
  digitalWrite (eightPin,val & 8 ? ON : OFF);
}

void loop() 
{
  for (byte val = 0; val < 16; val++) {
    lightDigit (val);
    delay (delayTime);
  }
}

or

//
// Write to LEDs, 0 - 15 in binary
//

const int N_BITS = 4;
const byte binaryDigitPin [N_BITS] = {11, 9, 7, 5}; //Note, LSB first
const int delayTime = 1000;
const byte ON  = HIGH; // may need to be swapped over
const byte OFF = LOW;  // depending on how LEDs are wired

void setup() 
{
  for (int i = 0; i < N_BITS; i++) {
    pinMode(binaryDigitPin [i], OUTPUT);
    digitalWrite (binaryDigitPin [i], OFF);
  }
}

void lightDigit (byte val)
{
  for (int i = 0; i < N_BITS; i++) {
    digitalWrite (binaryDigitPin [i],  val & (1 << i) ? ON : OFF);
  }
}

void loop() 
{
  for (byte val = 0; val < 16; val++) {
    lightDigit (val);
    delay (delayTime);
  }
}

TheMemberFormerlyKnownAsAWOL:
When you start seeing the same lines of code over and over again, it's time for a function, and possibly some loops

Hopefully, that will be the next homework assignment.

Ha!

Thanks, all.

Best,

Tony