Saturday 20 September 2014

ATTiny85 – Chasing LED sketch – Revised

I wanted to make the chasing LED sketch more efficient so that I could reuse the code for chained Shift Register (74HC595) chips without having to touch the code much.

There is really only one change that needs to be made programmatically to do this (I think). I figured that, if I set the number of LED in a variable in the header of the sketch, that this would be the only place that needs to be updated.

Of course, to prove this, I’m going to need to get another 74HC595 chip and connect it up (with the additional LED and resistors. So, at the moment, this is really only theoretical.

In addition to setting the number of LED in a variable, I’ve changed the “for” loop to a “while” loop, that’s really just preference, but you’ll see what it does in the code.

/*
  Shift Register Chasing LED
  Sends the data value to the shift register to
turn each
  LED on in turn for a chasing lights circuit
*/
int latchPin = 2;  // connected to ST_CP (12) of 74HC595
int clockPin = 3;  // connected to SH_CP (11) of 74HC595
int dataPin = 0;   // connected to DS (14) of 74HC595

int nLED = 8;      // 8 LED connected to the array
int nPause = 60;   // the delay between each cycle

byte data;

void setup()
{
  pinMode(latchPin, OUTPUT);
  blinkAll_2Bytes(2, 500);
}

void loop()
{
  int i = 0;
  byte ledValue = 1;
  while( i < (nLED))
  {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, ledValue);
    digitalWrite(latchPin, 1);
    delay(nPause);
    ledValue = ledValue * 2;
    i++;
  }
}

void shiftOut(int myDataPin, int myClockPin, byte myDataOut)
{
  int i = 0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);
 
  for(i = nLED-1; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    if (myDataOut & (1<<i))
    {
      pinState = 1;
    }
    else
    {
      pinState = 0;
    }
    digitalWrite(myDataPin, pinState);
    digitalWrite(myClockPin, 1);
    digitalWrite(myDataPin, 0);
  }
  digitalWrite(myClockPin, 0);
}

void blinkAll_2Bytes(int n, int d) {
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, 0);
  shiftOut(dataPin, clockPin, 0);
  digitalWrite(latchPin, 1);
  delay(200);
  for (int x = 0; x < n; x++) {
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 255);
    shiftOut(dataPin, clockPin, 255);
    digitalWrite(latchPin, 1);
    delay(d);
    digitalWrite(latchPin, 0);
    shiftOut(dataPin, clockPin, 0);
    shiftOut(dataPin, clockPin, 0);
    digitalWrite(latchPin, 1);
    delay(d);
  }
}

The “while” loop starts with a ledValue of 1 and, for each loop, it multiplies ledValue by 2 to get the next lamp ID (1, 2, 4, 8, 16, 32, 64, 128) and it increments “i” stopping when i = nLED. The ledValue is a byte value and this is sent to the shiftOut function.

Secondly, the “for” loop in the shiftOut function also takes the number of LED (nLED) as a control value to set the pin state.

The other change that I’ve made to the sketch is to set the delay value as a variable in the sketch header so that I can change that value quickly, without having to hunt for it in the code.

Anyway, I’ll come back to this sketch when I’ve put in the second shift register (not until next week, I’m afraid … I’m off on holidays right now and several hundred kilometres away from my equipment and components).

No comments:

Post a Comment

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.