Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Oscillator: 2, 3 on uC
- Input 1 -> Pin8 (5 on uC) B2
- Input 2 -> Pin7 (6 on uC) A7
- Taster -> Pin6 (7 on uC) A6
- 7Segments Cathodes -> Pin0-2 (12 - 10 on uC) + Output8 of Shift Register
- 888.8
- 012 O8
- ShiftRegister -> CL: Latch: Pin3(10) Pin4(9) DATA: Pin5(8)
- IO:9 -Cathodes = 5 -Inputs = 2
- */
- const byte dataPin = 3;
- const byte latchPin = 4;
- const byte clockPin = 5;
- const byte segments[] = {
- 252, 144, 122, 218, 150, 206, 238, 152, 254, 222, 2, 108}; //Replace with 7 segment numbers, 255 = '-', 254 = 'C'
- void setup()
- {
- DDRA = 0b10111111;
- pinMode(7, OUTPUT); //DECIMAL POINT
- //pinMode(6, INPUT);
- pinMode(8, INPUT);
- }
- void writeSegment(byte segment, byte value)
- {
- byte out = segments[value];
- byte sethigh = false;
- for(byte i = 0; i < 3; i++)
- digitalWrite(i, LOW);
- if(segment == 3)
- bitSet(out, 0); //replace according to wiring
- else
- sethigh = true;
- shiftOut(dataPin, clockPin, MSBFIRST, out);
- digitalWrite(latchPin, HIGH);
- if(sethigh)digitalWrite(segment, HIGH);
- digitalWrite(latchPin, LOW);
- }
- void loop()
- {
- writeSegment(0, 10); //show -
- unsigned long bef = 0;
- unsigned long af = 0;
- while(!(PINB & 0b100)); //wait for B2 Input 1
- bef = micros();
- PORTA = 0;
- while(!(PINA & 0b1000000)); //wait for A6 Input 2
- af = micros();
- digitalWrite(8, LOW);
- unsigned long xspeed = (1000000UL / (af - bef)); //10x the actual speed
- if(xspeed < 10000ul)
- digitalWrite(7, HIGH); //decimal point
- else
- xspeed = xspeed / 10;
- byte digits[4];
- String s = String(xspeed);
- while(s.length() < 4)
- s = "0" + s;
- byte blinkb = 0;
- byte blinkstate = 0;
- if(xspeed > 9999)
- blinkb = true;
- for(int i = 0 ; i < 4; i++) //get digits
- {
- digits[i] = s[i] - '0';
- }
- if(!blinkb)
- {
- while(true) // show digits
- {
- for(byte i = 0; i < 4; i++)
- {
- writeSegment(i, digits[i]);
- delayMicroseconds(1500); //~200 Hz update rate
- }
- }
- }
- else
- {
- while(true) // show digits
- {
- for(byte b = 0; b < 30; b++)
- {
- for(byte i = 0; i < 4; i++)
- {
- writeSegment(i, digits[i]);
- delayMicroseconds(1500); //~200 Hz update rate
- }
- }
- blinkstate = !blinkstate;
- digitalWrite(7, blinkstate);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement