Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const byte digit_pins[3] = {5,6,7};
  2. const byte data_pin = 2;
  3. const byte sh_pin = 4;
  4. const byte st_pin = 3;
  5.  
  6. unsigned long tm, next_flick, generator_flick;
  7. const unsigned int to_flick = 1;
  8. const unsigned int to_generator_low = 1;
  9. const unsigned int to_generator_high = 2;
  10. int sensorValue = 0;
  11. float voltage = 0;
  12.  
  13. byte digit = 0;
  14. byte is_high = 0;
  15. unsigned int counter = 0;
  16.  
  17. const byte digits[10] = {
  18.     B11101110,
  19.     B10000010,
  20.     B11011100,
  21.     B11010110,
  22.     B10110010,
  23.     B01110110,
  24.     B01111110,
  25.     B11000010,
  26.     B11111110,
  27.     B11110110
  28. };
  29.  
  30. void fill( byte d ){
  31.     for(char i=0; i<8; i++){
  32.         digitalWrite(sh_pin, LOW);
  33.         digitalWrite(data_pin, digits[d] & (1«i));
  34.         digitalWrite(sh_pin, HIGH);
  35.     }
  36.     digitalWrite(st_pin, HIGH);
  37.     digitalWrite(st_pin, LOW);
  38. }
  39.  
  40. void setDigit( byte digit, unsigned int counter ){
  41.     byte d = 0;
  42.     switch ( digit ){
  43.         case 0:
  44.         digitalWrite(digit_pins[2], LOW);
  45.         d = counter % 10;
  46.         fill(d);
  47.         digitalWrite(digit_pins[0], HIGH);
  48.         break;
  49.         case 1:
  50.         digitalWrite(digit_pins[0], LOW);
  51.         d = (counter % 100) / 10;
  52.         fill(d);
  53.         digitalWrite(digit_pins[1], HIGH);
  54.         break;
  55.         case 2:
  56.         digitalWrite(digit_pins[1], LOW);
  57.         d = ( counter % 1000 ) / 100;
  58.         fill(d);
  59.         digitalWrite(digit_pins[2], HIGH);
  60.         break;
  61.     }
  62. }
  63.  
  64. void setup() {
  65.     for(int i=0; i<3; i++){
  66.         pinMode(digit_pins[i], OUTPUT);
  67.     }
  68.     pinMode(data_pin, OUTPUT);
  69.     pinMode(sh_pin, OUTPUT);
  70.     pinMode(st_pin, OUTPUT);
  71.     pinMode(13, OUTPUT);
  72.     pinMode(8,OUTPUT);
  73.     digitalWrite(8,HIGH);
  74.     pinMode(9,OUTPUT);
  75.  
  76. }
  77. void set_generator(byte high)
  78. {
  79.     if (high == 0)
  80.         digitalWrite(9, LOW);
  81.     if (high == 1)
  82.         digitalWrite(9, HIGH);
  83. }
  84.  
  85. void loop() {
  86.     tm = millis();
  87.     if( tm > next_flick ){
  88.         next_flick = tm + to_flick;
  89.         digit++;
  90.         if( digit == 3 )
  91.             digit = 0;
  92.         sensorValue = analogRead(A0);
  93.         voltage = sensorValue * (10010 / 1023.0)/10;
  94.         setDigit( digit, voltage );
  95.     }
  96.     tm = millis();
  97.     if (tm > generator_flick) {
  98.         ++is_high;
  99.         if (is_high == 2)
  100.             is_high = 0;
  101.        
  102.         if (is_high == 0)
  103.             generator_flick = tm + to_generator_low;
  104.         else
  105.             generator_flick = tm + to_generator_high;
  106.        
  107.         set_generator(is_high);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement