I'm having a strange problem with digitalWrite. I have some writes that are not occurring that are immediately before another write. See the code below (this is on an Uno R3):
#include <EEPROM.h>
enum ACTIVE_OUTPUT_STATES {NONE, INPUT0, INPUT1, INPUT2};
enum POWER_STATES {ON, OFF};
const int EEPAddress = 50;
const int PixelData = 2;
const int PowerSW = 3;
const int PowerLED = 4;
const int PowerEnable = 19;
const int Input0SW = 5;
const int Input0LED = 6;
const int Input1SW = 7;
const int Input1LED = 8;
const int Input2SW = 9;
const int Input2LED = 10;
const int Input0Enable = 11;
const int Input1Enable = 12;
const int Input2Enable = 13;
const int debounceTime = 80; //ms
const int messageTime = 1000; //ms
const int timeoutTime = 250; //ms
int OutputMemory;
int LastMessage = 0;
int x = 4;
ACTIVE_OUTPUT_STATES ActiveOutput = NONE;
POWER_STATES Power = OFF;
void setup() {
// assign pinmodes
Serial.begin(9600);
pinMode(PixelData, OUTPUT);
pinMode(PowerSW, INPUT);
pinMode(PowerLED, OUTPUT);
pinMode(PowerEnable, OUTPUT);
pinMode(Input0SW, INPUT);
pinMode(Input0LED, OUTPUT);
pinMode(Input0Enable, OUTPUT);
pinMode(Input1SW, INPUT);
pinMode(Input1LED, OUTPUT);
pinMode(Input1Enable, OUTPUT);
pinMode(Input2SW, INPUT);
pinMode(Input2LED, OUTPUT);
pinMode(Input2Enable, OUTPUT);
//set initial states of outputs;
digitalWrite(PowerLED, LOW);
//setting PowerLED high for debug purposes, this shoudl be changed to LOW before release
digitalWrite(PowerEnable, LOW);
digitalWrite(Input0LED, LOW);
digitalWrite(Input0Enable, LOW);
digitalWrite(Input1LED, LOW);
digitalWrite(Input1Enable, LOW);
digitalWrite(Input2LED, LOW);
digitalWrite(Input2Enable, LOW);
// initialize state machines
Serial.println("Setup Complete\n\n");
}
void loop() {
//
// digitalWrite(PowerEnable, LOW);
// delay(1000);
// digitalWrite(PowerEnable, HIGH);
// delay(1000);
switch (Power)
{
case OFF:
if (!digitalRead(PowerSW)) {
x = debounce(PowerSW);
if (x) {
Serial.println("Power On\n");
digitalWrite(PowerEnable, HIGH);
digitalWrite(PowerLED, HIGH);
Power = ON;
}
}
break;
case ON:
if (!digitalRead(PowerSW)) {
x = debounce(PowerSW);
if (x) {
Power = OFF;
Serial.println("Power Off\n");
digitalWrite(PowerEnable, LOW);
digitalWrite(PowerLED, LOW);
}
}
break;
default:
Serial.println("Power Error \n\n");
Power = OFF;
break;
}
I have an LED hooked up to PowerLED and PowerEnable is hooked up to the gate of a FET. Currently the state changes are working and when I push the button (PowerSW) the LED turns off and on as expected. However, the PowerEnable writes aren't occurring.
As a sanity check I wrote a little PowerEnable toggle (you can see it commented out at the top of loop()) and that works so I know the HW is ok. Does anyone know why the PowerEnable digitalWrite isn't working?