Store › Forums › Digit Shield › Bugs/Problems › Modified Reaction Timer Code
- This topic has 0 replies, 1 voice, and was last updated 11 years, 7 months ago by jjbtlb.
-
AuthorPosts
-
May 4, 2013 at 5:39 pm #611jjbtlbMember
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.#include
unsigned long start, stop;
const int knockSensor = A1;
const int ledPin = 13;
const int piezo = 8;
void setup() {
randomSeed(analogRead(0));
pinMode(ledPin, OUTPUT);
DigitShield.begin();
DigitShield.setPrecision(2);
DigitShield.setBlank(true);
}
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
DigitShield.setLeadingZeros(true);
delay(random(2000, 5000)); // delay from 2 to 5 seconds
digitalWrite(ledPin, HIGH);
tone(8, 2000);
start = millis();
DigitShield.setBlank(false);
while (true) {
DigitShield.setValue((unsigned long)(millis() - start)/1000.0);
if (analogRead(knockSensor) >=10) {
stop = millis();
break;
}
}
noTone(8);
digitalWrite(ledPin, LOW);
DigitShield.setValue((unsigned long)(stop-start)/1000.0);
DigitShield.setBlank(false);
delay(1000);
} -
AuthorPosts
- You must be logged in to reply to this topic.