Friday 19 September 2014

ATTiny85 Tutorial 3 – Reading Digital (On/Off) Input

I’m still trying to find the time to do these tutorial projects in amongst the rest of the things that I’m doing … so here goes with Tutorial 3 – Reading Digital (On/Off) Input for the ATTiny85.

The main thing to note here is that I’m only using 4 LED rather than the 8 LED that you will find in the Freetronics tutorials. Of course, that’s because the ATTiny85 doesn’t have the masses of pins that the UNO does, so I’ve scaled it back to 4 LED and modified the sketch accordingly.

The other thing to consider is that the available pins for the ATTiny85 are enumerated differently, so, rather than having pins 6 through to 13, we have pins 0 – 4. I need to use one of those pins as a digital input, so that leaves me with 0 – 3.

ATTiny85 Pin assignment

I’ve marked out the pins with the associated sketch variable so that you can see at a glance what connects where.

And here’s the sketch that I loaded onto the ATTiny85 (using my handy-dandy ATTiny85 ICSP from my previous article).

int ledCount = 4;
int ledPins[] = {0,1,2,3};
int ledDelay = 300;
int buttonPin = 4;

void setup() {
  for(int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop() {
  for(int thisLed = 0; thisLed < ledCount; thisLed++) {
    digitalWrite(ledPins[thisLed], HIGH);
    delay(ledDelay);
    while(digitalRead(buttonPin) == HIGH) {
      delay(10);
    }
    digitalWrite(ledPins[thisLed], LOW);
  }
}

The sketch initialises the ledCount variable, the ledPins integer array, the ledDelay and the buttonPin variable. The setup function sets the led pins as output and the button pin as input.

The loop cycles through the array making each LED turn on and then turn off, when the LED is HIGH, if the momentary button is held down, the LED stays HIGH until the momentary button is released, then it just keeps going through the cycle.

Project3

As you can see from the above image, the wiring for this circuit is also pretty straight forward. You will note in the top right hand corner, I’m using my 9V to 5V power regulator mini-board. This is another circuit that I completed in a previous post.

This is a very simple tutorial and it does not require any hard to find parts. Instead of using my regulator, I could have connected the positive rail to the 5V of the Arduino and the negative rail to the GND of the Arduino and then slaved the power from the Arduino via a USB connection to my computer, that’s OK and it’s the easiest way to do it if you don’t already have a 5V regulated power supply. Also, you could have breadboarded the ICSP rather than using a dedicated circuit … but since I have them and I built them for this purpose … I’m going to use them!

Well … there you go, Tutorial #3 – Reading Digital (On/Off) Input converted for the ATTiny85 for your entertainment and my fun.

Check out the rest of the tutorials here.

No comments:

Post a Comment

Paypal Donations

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