Controlling a 4017, is this the correct way?

I'm using a 4017 chip to control 5 LEDs. No real reason, just trying to learn by doing and I hadn't designed anything with an IC yet and I happened to have the 4017 sitting around.

My first attempt worked but seemed to have a lot of unnecessary stuff:

/*5 LED sequential flasher based on 4017 decade counter.*/

//Define pins to be used.
int clockPin = 9;
int enablePin = 10;

void setup()
{
  //Set pins 9 & 10 as output.
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop()
{
  //Each cycle of enablePin/clockPin advances 1 LED.
  digitalWrite (9, HIGH);
  digitalWrite (10, LOW);
  delay(1000);
  digitalWrite (9, LOW);
  digitalWrite (10, HIGH);
  delay(1000);
  digitalWrite (9, HIGH);
  digitalWrite (10, LOW);
  delay(1000);
  digitalWrite (9, LOW);
  digitalWrite (10, HIGH);
  delay(1000);
  digitalWrite (9, HIGH);
  digitalWrite (10, LOW);
  delay(1000);
 /*The last cycle inputs from pin 1 (on 4017) to pin 10 (on 4017).
   This resets the decade counter back to the first LED.*/
  digitalWrite (9, LOW);
  digitalWrite (10, HIGH);
  delay(1000);  
}

The next version I cleaned it up a little;

/*5 LED sequential flasher based on 4017 decade counter.*/

//Define pins to be used.
int clockPin = 9;
int enablePin = 10;

void setup()
{
  //Set pins 9 & 10 as output.
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
}

void loop()
{
  //Each cycle of enablePin/clockPin advances 1 LED.
  digitalWrite (9, HIGH);
  digitalWrite (10, LOW);
  delay(1000);
  digitalWrite (9, LOW);
  digitalWrite (10, HIGH);
  delay(1000);
}

Then I wanted to try using if;

/*5 LED sequential flasher based on 4017 decade counter.*/

//Define pins to be used.
int clockPin = 9;
int enablePin = 10;

void setup()
{
  //Set pins 9 & 10 as output.
  pinMode(clockPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
  //Set starting state for pins
  digitalWrite (clockPin, HIGH);
  digitalWrite (enablePin, LOW);
}

void loop()
/*Check current state of pins, change to oposite of 
  current value. */
{
  delay(1000);
  if (clockPin == HIGH);
  {
    digitalWrite(clockPin, LOW);
    digitalWrite(enablePin, HIGH);
  }
  if (clockPin == LOW);
  {
    digitalWrite(clockPin, HIGH);
    digitalWrite(enablePin, LOW);
  }
}

That seemed nicer, but still seemed to have some unnecessary bits so I amended it to this;

/*5 LED sequential flasher based on 4017 decade counter.*/

//Define pins to be used.
int clockPin = 9;
int enablePin = 10;

void setup()
{
  //Set pins 9 & 10 as output.
  pinMode(clockPin, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop()
/*Check current state of pins, change to oposite of 
  current value. */
{
  delay(1000);
  if (clockPin == HIGH);
  {
    digitalWrite(clockPin, LOW);
    digitalWrite(enablePin, HIGH);
  }
  if (clockPin == LOW);
  {
    digitalWrite(clockPin, HIGH);
    digitalWrite(enablePin, LOW);
  }
}

That's the best I've got so far. I never realised there could be so many ways to do the same thing. So, now I'm wondering if there's a better way to go about this then what I've got.

int clockPin = 9;
  if (clockPin == HIGH);
  if (clockPin == LOW);

First, 9 will never be HIGH and it will never be LOW. Second, the body of your if statements is the ; on the end, not the code in the curly braces on the next few lines.

That's the best I've got so far. I never realised there could be so many ways to do the same thing.

Well, there are really only 2. Right and wrong, and, yes, you've demonstrated both.

Maybe my understanding of the 4017 is wrong, but I thought the advancement of the outputs are triggered by the reversal of the enable and clock pin (if clock is high and enable is low, it will progress when clock goes low and enable high).

The site I was referencing is: http://saint.419.removed.us/counter.html

I tried just sending a signal to the clockPin with reset and enable low but it didn't work. Then I read, "Just to be complete, the chip will count if the enable input goes high while clock is low. The two inputs are complementary."

Perhaps I misinterpreted this?

Regardless, all four of the sketches that I posted do work.

As for why? Like I said, just tinkering. I'm completely new to this sort of thing so I'm just trying to learn through experimentation. :slight_smile:

Regardless, all four of the sketches that I posted do work.

  if (clockPin == HIGH);
  {
    digitalWrite(clockPin, LOW);
    digitalWrite(enablePin, HIGH);
  }

The clock pin will be set LOW, and the enable pin set HIGH, regardless of the value in clockPin, which is 9, not HIGH or LOW.

int clockPin = 9;

void setup()
{
  pinMode(9, OUTPUT);
}
void loop()
{
  digitalWrite (9, HIGH);
  digitalWrite (9, LOW);
  delay(1000);

}

Yes? maybe? :~

(btw, thanks for being patient with me. Like I said, I'm new to this.)

Yes? maybe?

Look at the timing diagram. How long should the HIGH pulse be? How long is your HIGH pulse?

KE7GKP:
Taking the clock pin high and then IMMEDIATELY low may be too short a period for reliable operation of the 4017. You may need a bit of delay between the HIGH and LOW.

You defined "ClockPin" as "9", but then you go ahead and use literal "9" anyway?

It seems to be running okay.
And yeah, I got a little befuddled, heh.

PaulS:

Yes? maybe?

Look at the timing diagram. How long should the HIGH pulse be? How long is your HIGH pulse?

Do you mean it should match the duration of the LOW pulse? I tried that first but it seemed to act strangely. Each LED would light, but would get brighter halfway through. So when I load this sketch:

int clockPin = 9;

void setup()
{
  pinMode(clockPin, OUTPUT);
}
void loop()
{
  digitalWrite (clockPin, HIGH);
  delay(500);
  digitalWrite (clockPin, LOW);
  delay(500);

}

the LED lights up, then half a second later, it gets brighter, then half a second later it goes to the next LED and does the same thing.

Typically, a pin needs to be HIGH for 20 microseconds or so. Try adding

delayMicroseconds(20);

between the two digitalWrite() statements.