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;
- byte Depth = 0;
- void loop()
- {
- unsigned short Left = analogRead( 0 ) >> 1;
- if( SinceUpdate ) SinceUpdate--; else
- {
- Depth = analogRead( 3 ) >> 2;
- NudgeLength = analogRead( 2 );
- SinceUpdate = 2048;
- }
- Buffer[ Progress ] = Left;
- Progress++;
- if( SinceNudge ) SinceNudge--; else
- {
- byte OldOffset = Offset;
- if( Direction )
- {
- Offset++;
- if( Offset == 255 ){ Direction = 0; digitalWrite( 13, LOW ); }
- }
- else
- {
- Offset--;
- if( Offset < Depth ){ Direction = 1; digitalWrite( 13, HIGH ); }
- }
- SinceNudge = NudgeLength;
- }
- Position = Progress + Offset;
- Left += Buffer[ Position ];
- Left <<= 6;
- analogWrite( 3, Left >> 8 );
- analogWrite( 11, Left & 255 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement