Advertisement
Guest User

Untitled

a guest
Sep 16th, 2011
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1.  
  2. void setup()
  3. {
  4.  
  5. /* Speed up timer 2 to make pins 3 and 11 output a blistering fast PWM. */
  6. TCCR2B = TCCR2B & 0b11111000 | 1;
  7. TCCR2A &= ~B11;
  8. TCCR2A |= B011;
  9. pinMode( 3, OUTPUT );
  10. pinMode( 11, OUTPUT );
  11.  
  12. /* Do the same for timer 0. WARNING: Much of the Arduino library relies on timer 0 and you will find some functions output incorrect values. Look up the full effects and make sure you don't need it. */
  13. TCCR0B = TCCR0B & 0b11111000 | 1;
  14. TCCR0A &= ~B11;
  15. TCCR0A |= B011;
  16. pinMode( 5, OUTPUT );
  17. pinMode( 6, OUTPUT );
  18.  
  19. analogWrite( 3, 0 );
  20. analogWrite( 11, 0 );
  21. analogWrite( 5, 0 );
  22. analogWrite( 6, 0 );
  23.  
  24. /* Set the reference level to 1.1v */
  25. analogReference( INTERNAL );
  26.  
  27. /* Increase our input speed - prescaler to /8 */
  28. _SFR_BYTE( ADCSRA ) &= ~_BV( ADPS2 );
  29. _SFR_BYTE( ADCSRA ) |= _BV( ADPS1 );
  30. _SFR_BYTE( ADCSRA ) |= _BV( ADPS0 );
  31.  
  32. /* Our LED. */
  33. pinMode( 13, OUTPUT );
  34.  
  35. }
  36.  
  37. unsigned short Buffer[ 256 ];
  38. byte Progress = 0;
  39. boolean Direction = 0;
  40. byte Offset = 0;
  41. byte Position;
  42. unsigned short SinceNudge = 0;
  43. unsigned short SinceUpdate = 0;
  44. unsigned short NudgeLength = 0;
  45. byte Depth = 0;
  46.  
  47. void loop()
  48. {
  49.  
  50. unsigned short Left = analogRead( 0 ) >> 1;
  51.  
  52. if( SinceUpdate ) SinceUpdate--; else
  53. {
  54.  
  55. Depth = analogRead( 3 ) >> 2;
  56. NudgeLength = analogRead( 2 );
  57. SinceUpdate = 2048;
  58.  
  59. }
  60.  
  61. Buffer[ Progress ] = Left;
  62.  
  63. Progress++;
  64.  
  65. if( SinceNudge ) SinceNudge--; else
  66. {
  67.  
  68. byte OldOffset = Offset;
  69.  
  70. if( Direction )
  71. {
  72.  
  73. Offset++;
  74.  
  75. if( Offset == 255 ){ Direction = 0; digitalWrite( 13, LOW ); }
  76.  
  77. }
  78. else
  79. {
  80.  
  81. Offset--;
  82. if( Offset < Depth ){ Direction = 1; digitalWrite( 13, HIGH ); }
  83.  
  84. }
  85.  
  86. SinceNudge = NudgeLength;
  87.  
  88. }
  89.  
  90. Position = Progress + Offset;
  91.  
  92. Left += Buffer[ Position ];
  93.  
  94.  
  95.  
  96. Left <<= 6;
  97.  
  98. analogWrite( 3, Left >> 8 );
  99. analogWrite( 11, Left & 255 );
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement