I'm trying to run the following code on a Pro Mini 3.3V 8Mhz board. It's a stripped down version of the code from this project, as I don't need any of the LED features included in the original code. It runs fine on a 5V Arduino Nano, but whenever I try using it with the Pro Mini, everything else will work fine, but controllers will fail to sync.
I really need the project to be able to run off a 3-3.6V battery, so I'd like to be able to get the code working, but I can't see anything that would necessarily cause issues or know what the workaround might be.
#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 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;
}
}
}