Advertisement
martin2250

Untitled

Nov 9th, 2013
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. void setup()
  2. {
  3.   DDRD |= 0b111100;
  4.   PORTD ^= 0b111100;
  5.   delay(10);
  6.   PORTD ^= 0b111100;
  7.   Serial.begin(115200);
  8. }
  9.  
  10. unsigned long last = 0;
  11.  
  12. byte digits[4] = {
  13.   2, 4, 7, 1};
  14.  
  15. byte positions[4] = {
  16.   1, 4, 6, 9};
  17.  
  18.  
  19. void loop()
  20. {
  21.   while(!(PIND & 0b10000000));
  22.  
  23.   unsigned long micro = micros();
  24.   unsigned long duration = micro - last;
  25.   last = micro;
  26.  
  27.   unsigned long pdigit = duration / 10 - duration / 150;
  28.   unsigned int blinkduration = pdigit / 50;
  29.  
  30.   for(byte c = 0; c < 4; c++)
  31.   {
  32.     while(micros() < micro + positions[c] * pdigit + pdigit / 4);
  33.    
  34.     PORTD ^= _BV(c + 2);
  35.     delayMicroseconds(blinkduration);
  36.     PORTD ^= _BV(c + 2);
  37.   }
  38.  
  39.  
  40.   if(Serial.available() >= 4)
  41.   {
  42.     Serial.readBytes((char*)positions, 4);
  43.     for(byte b = 0; b < 4; b++)
  44.       positions[b] = 10 - (positions[b] - '0') + b;
  45.   }
  46.   while(PIND & 0b10000000);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement