int sensorPin = 4; // 220 or 1k resistor connected to this pin
long result = 0;
double start = 0;
double time = 0;
double duration = 3000;
int touch = 0;
double rate = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
//Serial.println("start");
}
void loop() // run over and over again
{
//Serial.println( RCtime(sensorPin));
delay(10);
start = millis();
//Serial.print("start = ");
//Serial.println(start);
time = millis();
while(time<(start+duration)){
// Serial.println("while");
if(RCtime(sensorPin)>2000){
touch = touch + 1;
// Serial.print("touch = ");
// Serial.println(touch);
time=millis();
// Serial.print("time = ");
// Serial.println(time);
}
}
if(time>(start+duration)){
// Serial.println("more than 3 sec");
rate = touch/(duration/1000);
// Serial.println(touch);
Serial.print("You tapped at a rate of ");
Serial.print(rate);
Serial.println(" taps per second.");
time=millis();
start=millis();
touch = 0;
}
}
long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(10); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}
Circuit:
long result = 0;
double start = 0;
double time = 0;
double duration = 3000;
int touch = 0;
double rate = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
//Serial.println("start");
}
void loop() // run over and over again
{
//Serial.println( RCtime(sensorPin));
delay(10);
start = millis();
//Serial.print("start = ");
//Serial.println(start);
time = millis();
while(time<(start+duration)){
// Serial.println("while");
if(RCtime(sensorPin)>2000){
touch = touch + 1;
// Serial.print("touch = ");
// Serial.println(touch);
time=millis();
// Serial.print("time = ");
// Serial.println(time);
}
}
if(time>(start+duration)){
// Serial.println("more than 3 sec");
rate = touch/(duration/1000);
// Serial.println(touch);
Serial.print("You tapped at a rate of ");
Serial.print(rate);
Serial.println(" taps per second.");
time=millis();
start=millis();
touch = 0;
}
}
long RCtime(int sensPin){
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(10); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin)){ // wait for pin to go low
result++;
}
return result; // report results
}
Circuit:
No comments:
Post a Comment