daily pastebin goal
23%
SHARE
TWEET

Comp #1

a guest Feb 13th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Dean Nguyen
  2. //Comp #1
  3.  
  4. //All Defines
  5. #define SW1 6
  6. #define LED1 10
  7. #define MSEC_SAMPLE 1
  8.  
  9. //States
  10. enum {yellowLED_ON, yellowLED_OFF};
  11.  
  12. //Setting variables
  13. boolean isSwPressed, prevIsSwPressed, isSwJustReleased, isSwJustPressed;
  14. int state = yellowLED_OFF, prevState = !state;
  15. boolean isNewState;
  16.  
  17. void setup() {
  18.   pinMode(SW1, INPUT_PULLUP);
  19.   pinMode(LED1, OUTPUT); digitalWrite(LED1, LOW);
  20.  
  21.   Serial.begin(9600);
  22. }
  23.  
  24. void loop() {
  25.   //Switch statements
  26.   prevIsSwPressed = isSwPressed;
  27.   isSwPressed = !digitalRead(SW1);
  28.   isSwJustPressed = (isSwPressed && !prevIsSwPressed);  // switch edge detection
  29.   isSwJustReleased = (!isSwPressed && prevIsSwPressed);
  30.  
  31.   //State statements
  32.   isNewState = (state != prevState);
  33.   prevState = state;
  34.  
  35.   //Switch-Case
  36.   switch (state) {
  37.  
  38.      case yellowLED_OFF:
  39.        //Entry Housekeeping
  40.        if (isNewState) {
  41.          Serial.println("led_state: yellowLED_OFF");
  42.        }
  43.        //State Business
  44.        digitalWrite(LED1, LOW);
  45.        //Exit Housekeeping
  46.        if (isSwPressed) {
  47.          state = yellowLED_ON;
  48.        }
  49.        break;
  50.  
  51.      case yellowLED_ON:
  52.       //Entry Housekeeping
  53.       if (isNewState) {
  54.         Serial.println("led_state: yellowLED_ON");
  55.       }
  56.       //State Business
  57.       digitalWrite(LED1, HIGH);
  58.       //Exit Housekeeping
  59.       if (isSwJustReleased) {
  60.         state = yellowLED_OFF;
  61.       }
  62.       break;
  63.   }
  64.   delay(MSEC_SAMPLE);
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top