Advertisement
ksoltan

Blinky-Estop

Sep 15th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. // Katya Soltan
  2. // Onboard LED blinks until a switch (or an E-stop button) is pressed.
  3. // When the button is released, the LED will resume blinking.
  4. // Make sure to connect a resistor between power and GND on the button to avoid burning it out.
  5. #define switchPin 10
  6. #define LEDPin 13
  7. boolean isLEDOn = false;
  8.  
  9. void setup() {
  10.   Serial.begin(9600);
  11.   pinMode(switchPin, INPUT);
  12.   pinMode(LEDPin, OUTPUT);
  13. }
  14.  
  15. void loop() {
  16.   // When the switch is pressed, the pin will read 0, as the circuit completes the loop to GND
  17.   // When the switch is not pressed, the pin will read 1, as the voltage doesn't got to GND
  18.   if(digitalRead(switchPin)){
  19.     // Switch is pressed, LED should be OFF
  20.     digitalWrite(LEDPin, LOW);
  21.   }else{
  22.     // Switch is not pressed, LED should be ON and BLINKING
  23.     if(isLEDOn){ // if the LED is currently on, turn it off
  24.       digitalWrite(LEDPin, LOW);
  25.       isLEDOn = false; // Update LED state
  26.     }else{ // otherwise, turn the LED on to blink it
  27.       digitalWrite(LEDPin, HIGH);
  28.       isLEDOn = true; // Update LED state
  29.     }
  30.   }
  31.   delay(50); // LED blinks every 50ms.
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement