Modified Reaction Timer Code

Store Forums Digit Shield Bugs/Problems Modified Reaction Timer Code

Viewing 1 post (of 1 total)
  • Author
  • #611

    I modified reaction timer code so that a piezo sensor would start timer, LED would flash 3 times before random count, then LED would turn on and timer would start counting. When piezo sensor touched again LED would go out and timer would stop. Everything working fine that way, but now I want to add a piezo as a buzzer to sound at the same time LED turns on and goes out when LED turns off, which it does, but
    after the first start when I start timer again the LED and the random lines take 3 times as long to run for some reason the process takes longer. What I want to do, is have LED turn on and buzzer sound, but only sound for only 250ms without effecting timer count. I tried different ways but no luck. Can you help with this project? code below.


    unsigned long start, stop;
    const int knockSensor = A1;
    const int ledPin = 13;
    const int piezo = 8;

    void setup() {
    pinMode(ledPin, OUTPUT);


    void loop() {

    while (analogRead(knockSensor) <=10); // wait until button press
    for (int i=0; i<3; i++) // declares i, tests if less
    { // than 3, increments i by 1
    digitalWrite(13, HIGH); // turns pin 13 on
    delay(500); // pauses for 1/2 second
    digitalWrite(13, LOW); // turns pin 13 off
    delay(500); // pauses for 1/2 second
    DigitShield.setBlank(true); // turn off the display
    delay(random(2000, 5000)); // delay from 2 to 5 seconds
    digitalWrite(ledPin, HIGH);
    tone(8, 2000);
    start = millis();
    while (true) {
    DigitShield.setValue((unsigned long)(millis() - start)/1000.0);
    if (analogRead(knockSensor) >=10) {
    stop = millis();
    digitalWrite(ledPin, LOW);
    DigitShield.setValue((unsigned long)(stop-start)/1000.0);
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.