/* BounceTimer: test switches to determine if & when they bounce While sw1 is HIGH light LED on pin 9 and Count changes of state of a switch sw2 connected to pin 3 then when sw1 goes LOW write count to serial output. REMEMBER to start serial monitor before running program The circuit: - LED attached from pin 9 to Vcc via resistor - LED attached from pin 15 to Vcc via resistor - switch 2 attached to pin 2 from ground - switch 3 attached to pin 3 from ground */ // constants won't change. They're used here to set pin numbers: const int goPin = 2; // the start - stop test switch const int sw3Pin = 3; // the switch being tested for bounce const int ledPin = 9; // the number of the go / stop LED pin const int testled = 15; // the number of the led to show state of test switch const int maxruns = 20; // number of runs per set const int pause = 800; // time for operator to prepare for next switch operation // variables bool go = 1; // variable for managing the "running test" status bool sw3State = 0; // variable for reading the switch under test bool intialsw3State = 0; // to record the transition being tested ie H->L (1) or L->H (0) bool lastsw3State = 0; //records if test switch (sw3) value has changed bool lastgoState = 0; //records if go condition has changed int count = 0; //count number of switch transitions H-L & L-H int runcount = 0; //how many time have we run test? int testduration = 5000; // allow 5msec for test duration to ensure no bounces are missed. //array to store time of each transition long timearray[64]; //allow to store 64 values - we are unlikely to get 32 bounces! unsigned long tStart; //time a test started - milliseconds; allow 500ms for operator to change test switch unsigned long firstT; //time from first transition int i; //index to print out values in array void setup() { //Initiate Serial communication. Serial.begin(9600); // initialize the LED pins as an output: pinMode(ledPin, OUTPUT); pinMode(testled, OUTPUT); // initialize the switch pins as an input with pullup: pinMode(sw3Pin, INPUT_PULLUP); pinMode(goPin, INPUT_PULLUP); } void loop() { //if first run or max_runs completed wait for sw2 (button) closure if(runcount==0){ //wait for signal to start another run while (digitalRead(goPin) == 1){ intialsw3State = digitalRead(sw3Pin); digitalWrite(testled,!intialsw3State); //show whether test switch is on or off at start of run //flash LED digitalWrite(ledPin, LOW); // turn LED on: delay(50); digitalWrite(ledPin,HIGH); // turn LED off: delay(20); } delay(2000); //pause before starting test sequence - LED is OFF, test not yet running } count = 0; //this is also the index for the array of times //reset the array values to zero for(i=0; i<=63; i++){ timearray[i]=0; } //START A TEST digitalWrite(ledPin, LOW); // turn LED on: its connected to Vcc so a low logic level turns it ON intialsw3State = digitalRead(sw3Pin); //save the current state of the switch being tested for bounce. lastsw3State = intialsw3State; //used to test & updated if its changed. //Serial.print("test started:.."); lastgoState = 1; //record test had been started tStart = millis(); while ((millis() - tStart) < 800) { // while its "go" read the switch under test - allow 800msec sw3State = digitalRead(sw3Pin); digitalWrite(testled, !sw3State); //show switch current state (inverted as LOW = "ON") if (sw3State != lastsw3State){ //if its changed record the transition time if (count == 0) firstT = micros(); //record time of first transition timearray[count] = micros() - firstT; count ++; //increment count lastsw3State = sw3State; //update laststate } } //one test terminated if (lastgoState){ digitalWrite(ledPin, HIGH); // turn LED off: Serial.print("test "); Serial.print(runcount); Serial.print(" : initial sw3 state was "); Serial.print (intialsw3State); Serial.print(": Number of transitions is "); Serial.println(count); Serial.print("times of transitions:"); for (i = 0; i=maxruns){runcount = 0; }// we have done all tests } //end of one test terminated }