/* ******************************************************************** Name : shiftOutAnalogueDisplay, Test code Author : Bruce Reagan adapted from source by Benjo Charlie Source : Benjo Charlie Date : 20 Dec, 2014 Notes : This is an adaptation of the ReadAnalogVoltage tutorial. : : The idea is to map the analogue input voltage and display that : mapped value on a 2 digit 7 segment display via a shiftbit register. ******************************************************************** 74HC595 Map: _______ LED Q1-|B * 16|-5V LED Q2-|C 15|-LED Q0-|A LED Q3-|D 14|-PIN 2 --- attiny85 LED Q4-|E 13|-GND LED Q5-|F 12|-PIN 1 --- attiny85 LED Q6-|G 11|-PIN 0 ; 3.3uF TO GND --- attiny85 DOT Q7-|Swch 10|-5V GND -|8_____9|-Serial */ int latchPin = 1; //Pin connected to ST_CP of 74HC595 int clockPin = 0; //Pin connected to SH_CP of 74HC595 clock latch int dataPin = 2; //Pin connected to DS of 74HC595 int digit1Pin = 3; // Pin used to switch between two digits byte SegDisplay; int sensorPin = A2; int x = 0; int tensval = 0; int onesval = 0; // Setup the combination to display each number on the display byte ZERO = 63; byte ONE = 6; byte TWO = 91; byte THREE = 79; byte FOUR = 102; byte FIVE = 109; byte SIX = 124; byte SEVEN = 7; byte EIGHT = 127; byte NINE = 103; byte MAX = 128; void setup() { pinMode(0, OUTPUT); pinMode(1, OUTPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(A2, INPUT); } void loop() { int analogueValue = analogRead(sensorPin); analogueValue = map(analogueValue, 0, 1023, 0, 40); tensval = analogueValue/10; onesval = analogueValue % 10; if(x==2) x=0; // even or odd cycle, resets after 1 if(x==0) { digitalWrite(latchPin, LOW); if (tensval==0) SegDisplay=ZERO; if (tensval==1) SegDisplay=ONE; if (tensval==2) SegDisplay=TWO; if (tensval==3) SegDisplay=THREE; if (tensval==4) SegDisplay=FOUR; if (tensval==5) SegDisplay=FIVE; if (tensval==6) SegDisplay=SIX; if (tensval==7) SegDisplay=SEVEN; if (tensval==8) SegDisplay=EIGHT; if (tensval==9) SegDisplay=NINE; SegDisplay = SegDisplay + 128; // digit2 off shiftOut(dataPin, clockPin, MSBFIRST, SegDisplay); digitalWrite(latchPin, HIGH); //take the latch pin high so the LEDs will light up: digitalWrite(digit1Pin, LOW); // turns digit1 (tens) ON } if(x==1) { digitalWrite(latchPin, LOW); if (onesval==0) SegDisplay=ZERO; // Min value if (onesval==1) SegDisplay=ONE; if (onesval==2) SegDisplay=TWO; if (onesval==3) SegDisplay=THREE; if (onesval==4) SegDisplay=FOUR; if (onesval==5) SegDisplay=FIVE; if (onesval==6) SegDisplay=SIX; if (onesval==7) SegDisplay=SEVEN; if (onesval==8) SegDisplay=EIGHT; if (onesval==9) SegDisplay=NINE; shiftOut(dataPin, clockPin, MSBFIRST, SegDisplay); digitalWrite(digit1Pin, HIGH); // turns digit1 OFF tens digitalWrite(latchPin, HIGH); //take the latch pin high so the LEDs will light up: } x++; // counter to ID even or odd loops delay(6); }