I have programmed for a long time and this is a first for me. I have read other posts where OP was usually using a function incorrectly. However, they resolved theirs and I can't figure out why mine is also doing the same. The following code works flawlessly if the 2 beginning/ending debug statements are uncommented. If they are commented, it's like the program never goes into the loop(). Completely boggled and was hoping someone could help me out. Maybe I should stick to Unity games haha. Appreciate any feedback.
Sensor at the moment is a break-sensor which wired correctly to the pin 2 and LED has resistor, etc. So I don't believe it's the wiring.
#define SENSOR_PIN_1 2
#define SIGNAL_LED_PIN 12
#define LED_PERIOD 1000
static bool sensorTripped = false;
void setup()
{
Serial.begin(128000);
// enable sensor 1's signal pin
pinMode(SENSOR_PIN_1, INPUT);
// set led pin
pinMode(SIGNAL_LED_PIN, OUTPUT);
Serial.println("Setup!");
}
void loop()
{
//Serial.println("-- Arduino now at top of main loop. --");
sensorTripped = ( digitalRead(SENSOR_PIN_1) == HIGH );
// if sensor is tripped, signal alert.
if (sensorTripped)
{
Serial.println("Sensor 1 tripped!");
digitalWrite(SIGNAL_LED_PIN, HIGH);
delay(LED_PERIOD);
}
else
{
digitalWrite(SIGNAL_LED_PIN, LOW);
}
//Serial.println("Arduino now at bottom of main loop.\r\n");
}