Advertisement
Guest User

Untitled

a guest
Sep 15th, 2011
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 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. #ifdef LINEARITY
  46. unsigned short OldLinearity = 65535;
  47. byte Delin[ 256 ];
  48. #endif
  49.  
  50. void loop()
  51. {
  52.  
  53. unsigned short Left = analogRead( 0 ) >> 1;
  54.  
  55. if( SinceUpdate ) SinceUpdate--; else
  56. {
  57. #ifdef LINEARITY
  58. unsigned short Linearity = analogRead( 3 ) >> 5;
  59.  
  60. if( Linearity != OldLinearity )
  61. {
  62.  
  63. unsigned short LUT = 0;
  64.  
  65. while( LUT < 256 )
  66. {
  67.  
  68. Delin[ LUT ] = ( byte ) ( ( cos( ( ( ( float ) LUT ) * 3.14f ) / 255.0f ) + 1.0f ) * 128.0f );
  69. Delin[ LUT ] = ( ( Delin[ LUT ] * Linearity ) + ( ( 255 - LUT ) * ( 31 - Linearity ) ) ) / 31;
  70.  
  71. LUT++;
  72.  
  73. }
  74.  
  75. OldLinearity = Linearity;
  76.  
  77. }
  78. #endif
  79. NudgeLength = analogRead( 2 );
  80. SinceUpdate = 2048;
  81.  
  82. }
  83.  
  84. Buffer[ Progress ] = Left;
  85.  
  86. Progress++;
  87.  
  88. if( SinceNudge ) SinceNudge--; else
  89. {
  90.  
  91. byte OldOffset = Offset;
  92.  
  93. if( Direction )
  94. {
  95.  
  96. Offset++;
  97.  
  98. if( Offset == 0 ){ Offset = 255; Direction = 0; digitalWrite( 13, LOW ); }
  99.  
  100. }
  101. else
  102. {
  103.  
  104. Offset--;
  105. if( Offset == 255 ){ Offset = 0; Direction = 1; digitalWrite( 13, HIGH ); }
  106.  
  107. }
  108.  
  109. SinceNudge = NudgeLength;
  110.  
  111. }
  112.  
  113. Position = Progress + Offset;
  114.  
  115. #ifdef LINEARITY
  116. Position = Delin[ Position ];
  117. #endif
  118.  
  119. Left += Buffer[ Position ];
  120.  
  121. Left <<= 6;
  122.  
  123. analogWrite( 3, Left >> 8 );
  124. analogWrite( 11, Left & 255 );
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement