Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void setup()
- {
- /* Speed up timer 2 to make pins 3 and 11 output a blistering fast PWM. */
- TCCR2B = TCCR2B & 0b11111000 | 1;
- TCCR2A &= ~B11;
- TCCR2A |= B011;
- pinMode( 3, OUTPUT );
- pinMode( 11, OUTPUT );
- /* 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. */
- TCCR0B = TCCR0B & 0b11111000 | 1;
- TCCR0A &= ~B11;
- TCCR0A |= B011;
- pinMode( 5, OUTPUT );
- pinMode( 6, OUTPUT );
- analogWrite( 3, 0 );
- analogWrite( 11, 0 );
- analogWrite( 5, 0 );
- analogWrite( 6, 0 );
- /* Set the reference level to 1.1v */
- analogReference( INTERNAL );
- /* Increase our input speed - prescaler to /8 */
- _SFR_BYTE( ADCSRA ) &= ~_BV( ADPS2 );
- _SFR_BYTE( ADCSRA ) |= _BV( ADPS1 );
- _SFR_BYTE( ADCSRA ) |= _BV( ADPS0 );
- /* Our LED. */
- pinMode( 13, OUTPUT );
- }
- unsigned short Buffer[ 256 ];
- byte Progress = 0;
- boolean Direction = 0;
- byte Offset = 0;
- byte Position;
- unsigned short SinceNudge = 0;
- unsigned short SinceUpdate = 0;
- unsigned short NudgeLength = 0;
- #ifdef LINEARITY
- unsigned short OldLinearity = 65535;
- byte Delin[ 256 ];
- #endif
- void loop()
- {
- unsigned short Left = analogRead( 0 ) >> 1;
- if( SinceUpdate ) SinceUpdate--; else
- {
- #ifdef LINEARITY
- unsigned short Linearity = analogRead( 3 ) >> 5;
- if( Linearity != OldLinearity )
- {
- unsigned short LUT = 0;
- while( LUT < 256 )
- {
- Delin[ LUT ] = ( byte ) ( ( cos( ( ( ( float ) LUT ) * 3.14f ) / 255.0f ) + 1.0f ) * 128.0f );
- Delin[ LUT ] = ( ( Delin[ LUT ] * Linearity ) + ( ( 255 - LUT ) * ( 31 - Linearity ) ) ) / 31;
- LUT++;
- }
- OldLinearity = Linearity;
- }
- #endif
- NudgeLength = analogRead( 2 );
- SinceUpdate = 2048;
- }
- Buffer[ Progress ] = Left;
- Progress++;
- if( SinceNudge ) SinceNudge--; else
- {
- byte OldOffset = Offset;
- if( Direction )
- {
- Offset++;
- if( Offset == 0 ){ Offset = 255; Direction = 0; digitalWrite( 13, LOW ); }
- }
- else
- {
- Offset--;
- if( Offset == 255 ){ Offset = 0; Direction = 1; digitalWrite( 13, HIGH ); }
- }
- SinceNudge = NudgeLength;
- }
- Position = Progress + Offset;
- #ifdef LINEARITY
- Position = Delin[ Position ];
- #endif
- Left += Buffer[ Position ];
- Left <<= 6;
- analogWrite( 3, Left >> 8 );
- analogWrite( 11, Left & 255 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement