Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Katya Soltan
- // Onboard LED blinks until a switch (or an E-stop button) is pressed.
- // When the button is released, the LED will resume blinking.
- // Make sure to connect a resistor between power and GND on the button to avoid burning it out.
- #define switchPin 10
- #define LEDPin 13
- boolean isLEDOn = false;
- void setup() {
- Serial.begin(9600);
- pinMode(switchPin, INPUT);
- pinMode(LEDPin, OUTPUT);
- }
- void loop() {
- // When the switch is pressed, the pin will read 0, as the circuit completes the loop to GND
- // When the switch is not pressed, the pin will read 1, as the voltage doesn't got to GND
- if(digitalRead(switchPin)){
- // Switch is pressed, LED should be OFF
- digitalWrite(LEDPin, LOW);
- }else{
- // Switch is not pressed, LED should be ON and BLINKING
- if(isLEDOn){ // if the LED is currently on, turn it off
- digitalWrite(LEDPin, LOW);
- isLEDOn = false; // Update LED state
- }else{ // otherwise, turn the LED on to blink it
- digitalWrite(LEDPin, HIGH);
- isLEDOn = true; // Update LED state
- }
- }
- delay(50); // LED blinks every 50ms.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement