-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXBOX_360_Arduino.ino
More file actions
106 lines (95 loc) · 3 KB
/
Copy pathXBOX_360_Arduino.ino
File metadata and controls
106 lines (95 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* 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.
int prev = 1;
for(int i = 0; i < 10; i++){
while (prev == digitalRead(clock_pin)){} //detects change in clock
prev = digitalRead(clock_pin);
// should be after downward edge of clock, so send bit of data now
digitalWrite(data_pin, cmd_do[i]);
while (prev == digitalRead(clock_pin)){} //detects upward edge of clock
prev = digitalRead(clock_pin);
}
digitalWrite(data_pin, HIGH);
pinMode(data_pin, INPUT);
}
void initLEDs(){
sendData(led_cmd);
delay(50);
sendData(anim_cmd);
delay(50);
}
void wakeUp(){
sync_enable = 1;
}
void setActivateInterrupt(){
attachInterrupt(0, wakeUp, LOW);
detachInterrupt(0); // disables interrupt 0 on pin 2
}
void sleepNow() {
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
detachInterrupt(0); // disables interrupt 0 on pin 2
}
void LedUpdate(int nPin, bool bActivate, int nTimeMilliseconds){
// put your main code here, to run repeatedly:
if(bActivate){
// ativar
for(int i = 0; i < 255; i++){
analogWrite(nPin, i);
delay(nTimeMilliseconds);
}
}
else{
for(int i = 255; i > 0; i--){
analogWrite(nPin, i);
delay(nTimeMilliseconds);
}
}
}
void setup() {
Serial.begin(9600);
// leds
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(sync_pin, INPUT);
digitalWrite(sync_pin,HIGH);
pinMode(data_pin, INPUT);
pinMode(clock_pin, INPUT);
delay(2000);
initLEDs();
// sendData(sync_cmd);
}
void loop(){
if(sync_enable==1) {
Serial.println("Syncing.");
sendData(sync_cmd);
sync_enable = 0;
}
else{
Serial.println("Sleeping.");
//sleepNow();
setActivateInterrupt();
delay(200);
}
LedUpdate(5, true, 2);
LedUpdate(5, false, 2);
LedUpdate(6, true, 2);
LedUpdate(6, false, 2);
}