I'm working on this sketch that I've already removed a decent chunk of code from and I need to remove the sleep function from the code, as I don't believe it's really needed and it causes interference with some code I need to add to it. The code is for connecting an Xbox 360 wireless controller to the wireless RF board from an Xbox 360 console.
The issue I'm having is that when I remove the code that I believe needs to go, previously connected controllers will still work fine, but I can no longer sync new controllers. I think this is due to something that changes in the loop(), but I don't know what.
This is the code before removing any related to sleep:
#include <avr/sleep.h>
#define sync_pin 2
#define data_pin 3
#define clock_pin 4
int sync_cmd[10] = {0,0,0,0,0,0,0,1,0,0};
int turn_off_cmd[10] = {0,0,0,0,0,0,1,0,0,1};
volatile boolean sync_pressed = 0;
int sync_hold_time = 0;
boolean turn_off_controllers = false;
void sendData(int command[])
{
pinMode(data_pin, OUTPUT);
digitalWrite(data_pin, LOW);
int previous_clock = 1;
for(int i = 0; i < 10; i++)
{
while (previous_clock == digitalRead(clock_pin)){}
previous_clock = digitalRead(clock_pin);
digitalWrite(data_pin, command[i]);
while (previous_clock == digitalRead(clock_pin)){}
previous_clock = digitalRead(clock_pin);
}
digitalWrite(data_pin, HIGH);
pinMode(data_pin, INPUT);
delay(50);
}
void wakeUp()
{
sync_pressed = 1;
}
void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
sleep_mode();
sleep_disable();
detachInterrupt(0);
}
void setup()
{
pinMode(sync_pin, INPUT);
digitalWrite(sync_pin,HIGH);
pinMode(data_pin, INPUT);
pinMode(clock_pin, INPUT);
delay(2000);
}
void loop()
{
if(!sync_pressed)
{
sleepNow();
}
delay(200);
if(sync_pressed)
{
if(sync_hold_time >= 1000)
{
turn_off_controllers = true;
sync_hold_time = 1000;
}
if (digitalRead(sync_pin))
{
if(turn_off_controllers)
{
sendData(turn_off_cmd);
turn_off_controllers = false;
}
else
{
sendData(sync_cmd);
}
sync_hold_time = 0;
sync_pressed = false;
}
else
{
sync_hold_time += 200;
}
}
}
This is the stuff I removed:
#include <avr/sleep.h>
void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0, wakeUp, LOW);
sleep_mode();
sleep_disable();
detachInterrupt(0);
}
if(!sync_pressed) // from void loop()
{
sleepNow();
}
delay(200);
Leaving me with:
#define sync_pin 2
#define data_pin 3
#define clock_pin 4
int sync_cmd[10] = {0,0,0,0,0,0,0,1,0,0};
int turn_off_cmd[10] = {0,0,0,0,0,0,1,0,0,1};
volatile boolean sync_pressed = 0;
int sync_hold_time = 0;
boolean turn_off_controllers = false;
void sendData(int command[])
{
pinMode(data_pin, OUTPUT);
digitalWrite(data_pin, LOW);
int previous_clock = 1;
for(int i = 0; i < 10; i++)
{
while (previous_clock == digitalRead(clock_pin)){}
previous_clock = digitalRead(clock_pin);
digitalWrite(data_pin, command[i]);
while (previous_clock == digitalRead(clock_pin)){}
previous_clock = digitalRead(clock_pin);
}
digitalWrite(data_pin, HIGH);
pinMode(data_pin, INPUT);
delay(50);
}
void wakeUp()
{
sync_pressed = 1;
}
void setup()
{
pinMode(sync_pin, INPUT);
digitalWrite(sync_pin,HIGH);
pinMode(data_pin, INPUT);
pinMode(clock_pin, INPUT);
delay(2000);
}
void loop()
{
if(sync_pressed)
{
if(sync_hold_time >= 1000)
{
turn_off_controllers = true;
sync_hold_time = 1000;
}
if (digitalRead(sync_pin))
{
if(turn_off_controllers)
{
sendData(turn_off_cmd);
turn_off_controllers = false;
}
else
{
sendData(sync_cmd);
}
sync_hold_time = 0;
sync_pressed = false;
}
else
{
sync_hold_time += 200;
}
}
}
Any ideas? Changing the sleep mode to IDLE doesn't affect the way the controller operates, but still causes some issues with the sketch I was planning on integrating into this one, so isn't a suitable workaround...