Modifing code

Store Forums Defusable Clock General Discussion Modifing code

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #8325
    nutonas
    Participant

    Hi, which part of code to edit if i want that, if you cut wrong wire time it’s reduces for example 3 times, second wrong wire, time reduces more 3 times…

    Thanks for help

    #8330
    Michael
    Keymaster

    The function countdown() detects wire cuts. A wire is cut if a digitalRead returns HIGH:

    
    if (digitalRead(WIRE_1) == HIGH) {
      // wire 1 is cut.
      if (countdownSeconds >= 3) {
        countdownSeconds = countdownSeconds - 3;
      } else {
        countdownSeconds = 0;
      }
    }
    
    • This reply was modified 8 years, 5 months ago by Michael.
    #8701
    Martin
    Participant

    Michael,
    Do we just stick this new code right before //assign random pins

    I did this and got all kinds of errors in my compiling and upload.

    Can you save me time and send me the code with this mod?

    Thanks

    #8704
    Michael
    Keymaster

    No, you put the code that checks whether wires are cut in the loop that checks the wires. In the loop where it says “get input”. That’s where it is checking if the detPin or defusePin are cut (they are HIGH).

    #8705
    Michael
    Keymaster

    Here, try this for countdown():

    void countdown() {
      int ledCounter = 0;
      int ledCounterThreshold = 100000;
      byte ledCurrentState = HIGH;
      byte defusePin;
      byte detPin;
      byte wrongPin1;
      byte wrongPin2;
      boolean wrongPin1Cut = false;
      boolean wrongPin2Cut = false;
      boolean defused = false;
      countdownRunning = true;
      int fractionalSecond;
    
      // assign random pins
      defusePin = random(WIRE_1, (WIRE_4 + 1));
      detPin = defusePin;
      while (detPin == defusePin) {
        detPin = random(WIRE_1, (WIRE_4 + 1));
      }
      wrongPin1 = detPin;
      while ((wrongPin1 == defusePin) || (wrongPin1 == detPin)) {
        wrongPin1 = random(WIRE_1, (WIRE_4 + 1));
      }
      wrongPin2 = detPin;
      while ((wrongPin2 == defusePin) || (wrongPin2 == detPin) || (wrongPin2 == wrongPin1)) {
        wrongPin2 = random(WIRE_1, (WIRE_4 + 1));
      }
    
      digitalWrite(LED_PM, LOW); // turn off the PM LED
    
      // Keep track of how far we are into the current
      // second so we can correct later.
      fractionalSecond = TCNT1 - TIMER1_SECOND_START;
    
      // Reset back to the last second boundary so we can start the countdown
      // immediately and so that the first second isn't truncated
      TCNT1 = TIMER1_SECOND_START;
    
      beep(3800, 30);
      digitalWrite(LED_DET, ledCurrentState);
      while ((countdownSeconds > 0) && (!defused)) {
        for (int i = 0; i < 10000; i++) {
          // get input
          if (digitalRead(detPin) == HIGH) {
            countdownSeconds = 0;
            break;
          }
          if (digitalRead(defusePin) == HIGH) {
            defused = true;
            break;
          }
          if ((digitalRead(wrongPin1) == HIGH) && !wrongPin1Cut) {
            if (countdownSeconds >= 3) {
              countdownSeconds = countdownSeconds - 3;
            } else {
              countdownSeconds = 0;
            }
          }
          if ((digitalRead(wrongPin2) == HIGH) && !wrongPin2Cut) {
            if (countdownSeconds >= 3) {
              countdownSeconds = countdownSeconds - 3;
            } else {
              countdownSeconds = 0;
            }
          }
        }
        delay(20);
        if (ledCounter++ > ledCounterThreshold) {
          ledCounter = 0;
          if (ledCurrentState == HIGH) {
            ledCurrentState = LOW;
          } else {
            ledCurrentState = HIGH;
          }
          digitalWrite(LED_DET, ledCurrentState);
        }
      }
      digitalWrite(LED_DET, LOW);
      countdownRunning = false;
      if (!defused) {
        detonate();
      } else {
        beep(4500, 80);
        isDefused = true;
      }
    
      // Now to keep the time accurate, add back in the fractional
      // second that we took off when we started the countdown sequence.
      // Wait until we can add it back to TCNT1 without overflowing.
      while (TCNT1 >= (65535 - fractionalSecond));
      TCNT1 += fractionalSecond;
    }
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.