Will Pay! 2 Pin sketch

Hi,

I need a sketch that executes the following: Button is pressed> Output Pin 1 goes HIGH for 2000 milliseconds Sketch stops and waits for another button press When the button is pressed again> Output Pin 2 goes High for 2000 milliseconds Sketch stops and waits for a button press to repeat the above loop.

Hi nique0827,

What should it do if the button is held? Should the output stay high until the button is released or should it turn off after 2 seconds?

Pat

you will also have to decide what to do when button is pressed once or more times before the 2000ms expire.

  • Turn on the other led too?
  • turn off the currently on led?
  • remember number of clicks and keep switching leds on/off each 2000ms for how many times button was clicked?

Here ya go.

Please deposit $20 in my paypal account pmurray@bigpond.com . If it's incorrect because there's a bunch of stuff you want it to do that your post doesn't mention, well then we can talk about moneys.

enum State {
  WAIT1 = 1,
  ON1 = 2,
  WAIT2 = 3,
  ON2 = 4
} state = WAIT1;

uint32_t stateChangeMs;

const uint32_t ON1_ms = 2000L;
const uint32_t ON2_ms = 2000L;


// PINOUT

const byte buttonPin = 4;
const byte out1Pin = 5;
const byte out2Pin = 6;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(out1Pin, OUTPUT);
  pinMode(out2Pin, OUTPUT);
  digitalWrite(out1Pin, LOW);
  digitalWrite(out2Pin, LOW);
}

void loop() {
  switch (state) {
    case WAIT1:
      if (digitalRead(buttonPin) == LOW) {
        digitalWrite(out1Pin, HIGH);
        stateChangeMs = millis();
        state = ON1;
      }
      break;
    case ON1:
      if (millis() - stateChangeMs > ON1_ms) {
        digitalWrite(out1Pin, LOW);
        state = WAIT2;
      }
      break;
    case WAIT2:
      if (digitalRead(buttonPin) == LOW) {
        digitalWrite(out2Pin, HIGH);
        stateChangeMs = millis();
        state = ON1;
      }
      break;
    case ON2:
      if (millis() - stateChangeMs > ON2_ms) {
        digitalWrite(out2Pin, LOW);
        state = WAIT1;
      }
      break;
  }
}