Store › Forums › Defusable Clock › General Discussion › Modifing code
Tagged: Defusable Clock V2
- This topic has 4 replies, 3 voices, and was last updated 7 years, 10 months ago by Michael.
-
AuthorPosts
-
July 15, 2016 at 2:04 am #8325nutonasParticipant
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
July 15, 2016 at 9:20 am #8330MichaelKeymasterThe 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, 4 months ago by Michael.
January 6, 2017 at 6:14 pm #8701MartinParticipantMichael,
Do we just stick this new code right before //assign random pinsI 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
January 6, 2017 at 9:07 pm #8704MichaelKeymasterNo, 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).
January 6, 2017 at 9:17 pm #8705MichaelKeymasterHere, 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; }
-
AuthorPosts
- You must be logged in to reply to this topic.