/* SN165 Phase 2 Two Switch input along blinking LEDs and Piezo sound generation with tone Modified by Paul and David Bradt Part of this code is based on this example code is in the public domain. https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection */ // this constant won’t change: const int intTrack10 = 10; //IR Sensor 10 const int intTrack11 = 11; //IR sensor 11 int piezoPin = 8; //Setting up peizo transducer to make sound // Variables will change: int sensorVal_10 = 0; // counter for the number of button presses int sensorVal_11 = 0; // current state of the button void setup() { //configure pin 0 and 1 as an input and enable the internal pull-up resistor //configure the LED outputs pinMode(intTrack10, INPUT_PULLUP); //IR sensor Track10 pinMode(intTrack11, INPUT_PULLUP); //IR sensor Track10 pinMode(2, OUTPUT);//Red LED2 pinMode(3, OUTPUT);//Red LED3 Serial.begin(9600); } void loop() { digitalWrite(1, LOW); //Red2 digitalWrite(0, LOW); //Red3 sensorVal_10 = digitalRead(intTrack10); sensorVal_11 = digitalRead(intTrack11); //print out the value of the pushbutton Serial.print("Track 10 value: "); Serial.println(sensorVal_10); delay(50); Serial.print("Track 11 value: "); Serial.println(sensorVal_11); delay(50); // Keep in mind the pull-up means the pushbutton’s logic is inverted. It goes // HIGH when it’s open, and LOW when it’s pressed. if (sensorVal_10 == LOW || sensorVal_11 == LOW){ digitalWrite(2, LOW); //Red2 digitalWrite(3, HIGH); //Red3 delay (200); digitalWrite(2, HIGH); //Red2 digitalWrite(3, LOW); //Red3 // Section of code that simulates the crossing bell tone(piezoPin, 1000, 100); delay (200); //Pause then start again } else{ digitalWrite(2, LOW); //Red2 digitalWrite(3, LOW); //Red3 } }