//Example sketch to run the Pmod Racing Ruler //library inclusions #include #include ACL myACL; // the library object //some global variables bool tapTrig = false; bool runState = true; int en1 = 29; int dir1 = 26; int dir2 = 27; int en2 = 22; int tapCount = 0; uint8_t dID = 0; int delayDetect = 0; void setup(){ //debugging via serial //Serial.begin(9600); pinMode(7, INPUT); //set INT3 as an input pinMode(dir1, OUTPUT); pinMode(en1, OUTPUT); pinMode(dir2, OUTPUT); pinMode(en2, OUTPUT); digitalWrite(en1, LOW); digitalWrite(dir1, LOW); digitalWrite(en2, LOW); digitalWrite(dir2, LOW); delay(500); // initialize PmodACL on SPI myACL.begin(PAR_ACCESS_DSPI0); // corresponds to DSPI0 //Put the device into standby mode to configure it. myACL.SetMeasure(false); // set data range to +/- 16g myACL.SetGRange(PAR_GRANGE_PM16G); //enable tap detection along the X-axis myACL.SetTapAxesBits(MSK_TAP_AXES_TAPXENABLE, true); myACL.SetThresholdG(PAR_THRESH_TAP, 10); //Set the Tap Threshold to 10g myACL.SetTimeS(PAR_TIME_DUR, 0.01); // maximum duration of acceleration event is 10 ms myACL.SetTimeS(PAR_TIME_LATENT, 0.1); // latent 100 ms - 100ms Latency before the second tap can occur. myACL.SetTimeS(PAR_TIME_WINDOW, 0.318); // 318 ms window to detect a second acceleration event //configure the interrupt myACL.ConfigureInterrupt(PAR_ACL_INT2, PAR_EXT_INT3, MSK_INT_SINGLE_TAP, &tap, PAR_INT_ACTIVEHIGH); // set Measure bit to true myACL.SetMeasure(true); // calibrate the accelerometer using X axis as neg-gravitational myACL.CalibrateOneAxisGravitational(PAR_AXIS_XN); //start the engines digitalWrite(en1, HIGH); digitalWrite(en2, HIGH); attachInterrupt(3,tapISR,RISING); delayDetect = millis(); runState = true; } void loop(){ //if we are currently running if(runState == true){ //check to see if an acceleration event has occured if(tapTrig = true){ //if yes, go to the tap function //serial debugging //Serial.println("entering function from ISR"); tap(); tapTrig = false; } delay(10); } //if we are not in the run state else{ //do nothing }// */ } /* ------------------------------------------------------------------- */ /* void tap() ** ** Parameters: ** none ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** This function is called when accelerometer interrupts occur. It ** was configurated using myACL.ConfigureInterrupt() function. ** The function detects the interrupt source (single tap) and sets ** the global variable bTapInfo accordingly. ** -----------------------------------------------------------------------*/ void tap() { // reading the INT_SOURCE register causes interrupt flags to be cleared if(myACL.GetInterruptSourceBits(MSK_INT_SINGLE_TAP)) { if((millis()-delayDetect)<1000){ //ignore the first bump runState = true; } else{ // the interrupt is triggered by a single-tap //Serial.println("DOUBLE TAP"); digitalWrite(en1, LOW); digitalWrite(en2, LOW); runState = false; } }//end of if the single tap interrupt was triggered } /* ------------------------------------------------------------------- */ /* void tapISR() ** ** Parameters: ** none ** ** Return Value: ** none ** ** Errors: ** none ** ** Description: ** This function executes when an interrupt is triggered ** It sets a flag so that the main loop can tell if an ** interrupt has occurred or not. ** -----------------------------------------------------------------------*/ void tapISR(){ tapTrig = true; }