/* Infrared Controlled Buggy David Johnson-Davies - www.technoblogy.com - 20th July 2015 ATtiny85 @ 1 MHz (internal oscillator; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ volatile int NextBit; volatile unsigned long RecdData; // Motor Control ********************************************** void ReceivedCode (boolean Repeat) { static int Left, Right, Speed = 127 ; // Check for correct remote control if ((RecdData & 0xFFFF) != 0xbF00) return; if (Repeat) return; // Ignore keys held down // Read key pressed int key = RecdData>>16 & 0xFF; // Motor commands if ((key >= 0x10) && (key <= 0x1A)) Speed = ((key - 16) * 12) + 7; else if (key == 0x05) { Left = 2; Right = 2; } // Go Forwards else if (key == 0x0D) { Left = -2; Right = -2; } // Go Backwards else if (key == 0x08) { Left = 2; Right = 1; } // Turn Left else if (key == 0x0A) { Left = 1; Right = 2; } // Turn Right else if ((key == 0x06) || (key == 0x09)) { Left = 0; Right = 0; } // Stop else if (key == 0x0E) { Left = 2; Right = -2; } // Rotate // if (Left >= 0) { digitalWrite(0, LOW); OCR1A = Left * Speed; } else { digitalWrite(0, HIGH); OCR1A = 255 + (Left * Speed); } if (Right >= 0) { digitalWrite(3, LOW); OCR1B = Right * Speed; } else { digitalWrite(3, HIGH); OCR1B = 255 + (Right * Speed); } } // Infrared Control ********************************************** // Interrupt service routine - called on every falling edge of PB2 ISR (INT0_vect) { int Time = TCNT0; int Overflow = TIFR & 1<= 194) && (Time <= 228) && (Overflow == 0)) { RecdData = 0; NextBit = 0; } else if ((Time >= 159) && (Time <= 193) && (Overflow == 0)) ReceivedCode(1); // Data bit } else { if ((Time > 44) || (Overflow != 0)) NextBit = 32; // Invalid - restart else { if (Time > 26) RecdData = RecdData | ((unsigned long) 1<