Clicky

Loading

MakerBot Print is our newest print-prepration software, which supports native CAD files and STL assemblies,
allows you to interact with all your printers via the Cloud, and many other exciting new features.

Download Now

Report as inappropriate

/*
You could use something like the Button2 library and its
"was_pressed_for" function to simplify (it handles some
button "debouncing" also, which can be helpful), but
if you really want to do it without a lib, I'd do
something like this:
*/

unsigned long leftPressStart = 0;
unsigned long rightPressStart = 0;

const unsigned long maxHoldTime = 5000; // in milliseconds

bool pbLeft = false;
bool pbRight = false;

void loop() {
  // put your main code here, to run repeatedly:
  uint32_t timeNow = millis();

  /* Button Sampling */
  bool leftNow = (digitalRead(left) == HIGH); // assuming "HIGH" is pressed
  if (leftNow && !pbLeft)
  {
    leftPressStart = timeNow;
  }
  pbLeft = leftNow;

  // exactly the same for right (also an indication that
  // you should probably reuse a struct or class)
  bool rightNow = (digitalRead(right) == HIGH);
  if (rightNow && !pbRight)
  {
    rightPressStart = timeNow;
  }
  pbRight = rightNow;

  /* Crush Logic */
  bool performCrush = false;
  if (pbLeft && pbRight)
  {
    unsigned long leftHeld = timeNow - leftPressStart;
    unsigned long rightHeld = timeNow - rightPressStart;
    if ((leftHeld < maxHoldTime) && (rightHeld < maxHoldTime))
    {
      performCrush = true;
    }
  }

  /* Output Action */
  if (performCrush)
  {
    digitalWrite(crush, HIGH);
  }
  else
  {
    digitalWrite(crush, LOW);
  }
}