int IR_PIN = 3; int bin_1 = 3000; //Binary 1 threshold (Microseconds) int bin_0 = 1000; //Binary 0 threshold (Microseconds) #define BIT_IS_SET(i, bits) (1 << i & bits) const int period = 26; // this is the period of 38kHz, in microseconds const int PULSE_WIDTH = 5; // time between pulses, in microseconds // some basic codes taken out of the manual: const int POWER = 138; const int PAUSE = 137; const int CLEAN = 136; const int MAX = 133; const int SPOT = 132; const int FORWARD = 130; const int LEFT = 129; const int RIGHT = 131; const int VWALL = 162; void setup() { pinMode(IR_PIN, OUTPUT); digitalWrite(IR_PIN, LOW); // Red-Status LED pinMode(4, OUTPUT); digitalWrite(4, LOW); } int i = 0; void loop() { i++; if (i == 0) digitalWrite(4, LOW); if (i == 100) digitalWrite(4, HIGH); if (i == 105) i = -1; send_byte(VWALL); } // oscillates at 38kHz to write "ON" void on(int time) { for(time = time/period; time > 0; --time) { digitalWrite(IR_PIN, HIGH); delayMicroseconds(PULSE_WIDTH); digitalWrite(IR_PIN, LOW); delayMicroseconds(PULSE_WIDTH); } } // just write pin low to write "OFF" void off(int time) { digitalWrite(IR_PIN, LOW); delayMicroseconds(time); } // write a binary 1 void WriteOne() { on(bin_1); // on for 3ms off(bin_0); // off for 1ms } // write a binary 0 void WriteZero() { on(bin_0); // on for 1ms off(bin_1); // off for 3ms } // Send a byte over the IR LED void send_byte(int bits) { for (int i = 7; i >= 0; i--) { if (BIT_IS_SET(i, bits)) { WriteOne(); } else { WriteZero(); } } delay(4); // leave 4ms at the end of the instruction }