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 );
- }
- void loop()
- {
- /* Take our left and right inputs and convert them from 10-bit unsigned (0 ... 1023) to 16-bit signed (-32768 ... 32767). */
- int Left = ( analogRead( 0 ) * 64 ) - 32768;
- int Right = ( analogRead( 1 ) * 64 ) - 32768;
- /* Process them here. */
- /* Shift the signed variables into unsigned 16-bit for output (0 ... 65535). */
- unsigned short LeftOut = Left + 32768;
- unsigned short RightOut = Right + 32768;
- /* Write our outputs. We want the two halves of the unsigned 16-bit as unsigned 8-bits, so we shift it to the right, chopping off the 8 least significant bits, and also take just the 8 least for the other pin. */
- analogWrite( 3, LeftOut >> 8 );
- analogWrite( 11, LeftOut & 255 );
- analogWrite( 5, RightOut >> 8 );
- analogWrite( 6, RightOut & 255 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement