Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit edit - 3 November 2019 - version 2.0
- This is an edit of some code posted online that appeared to have an error in it.
- The original source of the code is here: https://dilisilib.wordpress.com/hacking/xbox-360-rf-module-arduino/
- */
- /* Arduino code to communicate with xbox 360 RF module.
- Original work by (yaywoop) / additional ideas from Alexander Martinez - modified by dilandou (www.dilandou.com, www.diru.org/wordpress)
- First sends LED initialisation code followed by LED startup animation code, then sleeps until a button press for sync command.
- RF module must be powered with 3.3V, two diodes in series with USB 5v will do. Connect the USB wires to a host computer, and the data and serial wires to Arduino.
- of course, make sure to have a common ground */
- #include <avr/sleep.h>
- #define sync_pin 2 //power button repurposed for sync button (pin 5 on the module)
- #define data_pin 3 //data line (pin 6 on the module)
- #define clock_pin 4 //clock line (pin 7 on module)
- int led_cmd[10] = {0, 0, 1, 0, 0, 0, 0, 1, 0, 0}; //Activates/initialises the LEDs, leaving the center LED lit.
- int anim_cmd[10] = {0, 0, 1, 0, 0, 0, 0, 1, 0, 1}; //Makes the startup animation on the ring of light.
- int sync_cmd[10] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0}; //Initiates the sync process.
- volatile boolean sync_enable = 0;
- void sendData(int cmd_do[]) {
- pinMode(data_pin, OUTPUT);
- digitalWrite(data_pin, LOW); //start sending data.
- /*
- The section below is totally re-written, to send 10 data bits whenever the clock pin goes from low to high.
- */
- int clock_pin_change = 1; // This represents if the data will be sent on the rise or fall of the clock signal.
- // 1 means send when the clock rises to 1, 0 means send when the clock pin falls to zero.
- // You might need to try setting it both ways, to zero and 1.
- int current_clock_pin_value = 0; // This should be set to the opposite value of clock_pin_change.
- int previous_clock_pin_value = 0; // This should be set to the opposite value of clock_pin_change.
- int bytes_to_send = 10;
- int bytes_sent = 0;
- while (bytes_sent < bytes_to_send) { // There is 10 bytes to send, so this keeps looping until 10 bytes get sent.
- current_clock_pin_value = digitalRead(clock_pin);
- if (current_clock_pin_value != previous_clock_pin_value) { // This requires that the clock pin state has changed.
- if (current_clock_pin_value == clock_pin_change) {
- digitalWrite(data_pin, cmd_do[bytes_sent]);
- bytes_sent++;
- }
- }
- previous_clock_pin_value = current_clock_pin_value;
- }
- pinMode(data_pin, INPUT);
- }
- void initLEDs() {
- sendData(led_cmd);
- delay(50);
- sendData(anim_cmd);
- delay(50);
- }
- void wakeUp() {
- sync_enable = 1;
- // This function has been changed{ the following two lines have been moved from sleepNow() to here.
- sleep_disable(); //disable sleep bit
- detachInterrupt(0);
- }
- void sleepNow() {
- // This sleep function is not the way the Arduino website shows-- https://playground.arduino.cc/Learning/ArduinoSleepCode/
- /*
- set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set sleep mode
- sleep_enable(); //enable sleep bit
- attachInterrupt(0, wakeUp, LOW);
- sleep_mode();
- sleep_disable(); //disable sleep bit --------------------------------------- I think this was supposed to go into the wakeUp function.
- detachInterrupt(0); // disables interrupt 0 on pin 2 ----------------------- And this also.
- */
- // Below is what the website shows.
- // The website notes that calling these in the correct order is necessary for them to all work correctly.
- sleep_enable();
- attachInterrupt(0, wakeUp, LOW);
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- cli();
- sleep_bod_disable();
- sei();
- sleep_cpu();
- }
- void setup() {
- Serial.begin(9600);
- pinMode(sync_pin, INPUT);
- digitalWrite(sync_pin, HIGH);
- pinMode(data_pin, INPUT);
- pinMode(clock_pin, INPUT);
- delay(2000);
- initLEDs();
- // sendData(sync_cmd);
- }
- void loop() {
- Serial.println("Sleeping.");
- sleepNow();
- delay(200);
- if (sync_enable == 1) {
- Serial.println("Syncing.");
- sendData(sync_cmd);
- sync_enable = 0;
- }
- }
- // ~~~~~~~~ the end ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment