Advertisement
martin2250

Ballistic Chronograph

Nov 5th, 2013
1,743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. /*
  2.   Oscillator: 2, 3 on uC
  3.  Input 1 -> Pin8 (5 on uC)    B2
  4.  Input 2 -> Pin7  (6 on uC)   A7
  5.  Taster  ->  Pin6  (7 on uC)  A6
  6.  
  7.  7Segments Cathodes -> Pin0-2  (12 - 10 on uC)  + Output8 of Shift Register
  8.  
  9.  888.8
  10.  012 O8  
  11.  
  12.  ShiftRegister -> CL: Latch: Pin3(10) Pin4(9)  DATA: Pin5(8)
  13.  
  14.  IO:9   -Cathodes = 5 -Inputs = 2
  15.  
  16.  
  17.  */
  18. const byte dataPin = 3;
  19. const byte latchPin = 4;
  20. const byte clockPin = 5;
  21.  
  22. const byte segments[] = {
  23.   252, 144, 122, 218, 150, 206, 238, 152, 254, 222, 2, 108};  //Replace with 7 segment numbers, 255 = '-', 254 = 'C'
  24.  
  25. void setup()
  26. {
  27.   DDRA = 0b10111111;
  28.   pinMode(7, OUTPUT);    //DECIMAL POINT
  29.   //pinMode(6, INPUT);
  30.   pinMode(8, INPUT);
  31. }
  32.  
  33. void writeSegment(byte segment, byte value)
  34. {
  35.   byte out = segments[value];
  36.   byte sethigh = false;
  37.  
  38.   for(byte i = 0; i < 3; i++)
  39.     digitalWrite(i, LOW);
  40.  
  41.   if(segment == 3)
  42.     bitSet(out, 0);            //replace according to wiring
  43.   else
  44.     sethigh = true;
  45.  
  46.   shiftOut(dataPin, clockPin, MSBFIRST, out);  
  47.  
  48.   digitalWrite(latchPin, HIGH);
  49.   if(sethigh)digitalWrite(segment, HIGH);
  50.   digitalWrite(latchPin, LOW);
  51. }
  52.  
  53. void loop()
  54. {
  55.   writeSegment(0, 10);    //show -
  56.  
  57.   unsigned long bef = 0;
  58.   unsigned long af = 0;  
  59.  
  60.   while(!(PINB & 0b100));      //wait for B2  Input 1
  61.   bef = micros();
  62.  
  63.   PORTA = 0;          
  64.  
  65.   while(!(PINA & 0b1000000));    //wait for A6 Input 2
  66.   af = micros();
  67.  
  68.   digitalWrite(8, LOW);
  69.  
  70.  
  71.   unsigned long xspeed = (1000000UL / (af - bef));    //10x the actual speed
  72.  
  73.   if(xspeed < 10000ul)
  74.     digitalWrite(7, HIGH);      //decimal point
  75.   else
  76.     xspeed = xspeed / 10;
  77.   byte digits[4];
  78.  
  79.   String s = String(xspeed);
  80.   while(s.length() < 4)
  81.     s = "0" + s;
  82.    
  83.   byte blinkb = 0;
  84.   byte blinkstate = 0;
  85.  
  86.   if(xspeed > 9999)
  87.     blinkb = true;
  88.  
  89.     for(int i = 0 ; i < 4; i++)                    //get digits
  90.     {
  91.       digits[i] = s[i] - '0';
  92.     }  
  93.  
  94.  
  95.   if(!blinkb)
  96.   {
  97.     while(true)                      // show digits
  98.     {
  99.       for(byte i = 0; i < 4; i++)
  100.       {
  101.         writeSegment(i, digits[i]);
  102.         delayMicroseconds(1500);                              //~200 Hz update rate
  103.       }
  104.     }
  105.   }
  106.   else
  107.   {
  108.     while(true)                       // show digits
  109.     {
  110.       for(byte b = 0; b < 30; b++)
  111.       {
  112.         for(byte i = 0; i < 4; i++)
  113.         {
  114.           writeSegment(i, digits[i]);
  115.           delayMicroseconds(1500);                              //~200 Hz update rate
  116.         }
  117.       }
  118.       blinkstate = !blinkstate;
  119.       digitalWrite(7, blinkstate);
  120.  
  121.     }
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement