// The ES6937 Motion Sensing Swivel Disc - Stabilizing // CONNECTIONS: //AD3 is ground //AD1 is 5v //AD2 is PIR // The on-board LED is what we're going to use to see when the motion sensor output has become stable. No need to connect an external LED =) // The PCB connector pins are labelled AD1, AD2, and AD3. int hold = 0; // This integer will hold our Analog-to-digital data when sampling the motion sensor output int state = 0; // This integer will hold us in loops while certain criteria are being met. int counter = 0; // This integer holds a count value that will helps us to stabilize the sensor #define LED 13 // This is the built-in LED at GPIO 13. We still need to declare it // We're going to build on the last piece of code. This time, we're going to automate calibration, or rather, we'll program the unit to wait until the output is stable and reliable. void setup() { // Let's set things up Serial.begin(9600); // Start serial comms so that we can use the serial plotter or serial monitor pinMode(A3,OUTPUT); // Set analog pin#3 as an output. This will be set to LOW, and act as an artificial ground pinMode(A1,OUTPUT); // Set analog pin#1 as an output. This will be set to HIGH, and act as a power supply to the disc. The disc takes very little power to operate. pinMode(LED,OUTPUT); // Declare the built-in LED as an output. It should be set low/off by default digitalWrite(A3,LOW); // Greate the artificial ground by setting A3 LOW delay(1); // This 1ms delay is optional. digitalWrite(A1,HIGH);// Set A1 to high to apply 5v to the disc delay(1000); } // Doneski! // Open the serial plotter as soon as you program the code. Nothing will show up on the serial plotter until the output is stable. void loop() { // We're going to add in a function here that takes care of watching the sensor output until is has become stable. Once stable, we're going to view the output... // ...on the serial monitor as opposed to the plotter. stabilizer(); // This calls the stabilizer function below. When the sensor is table, the following code will execute: digitalWrite(LED,HIGH); // This is a visual indicator for you to use just in case you don't have the serial plotter on. This turns the LED on. // In other words, when the on-board LED turns on, the output of the disc is stable. state = 0; // Once stable, set state to 0 again, and do the following forever, as we're not going to exit the following while loop. while(state == 0){ // Do the following while state equals 0. In this code, we never exit this while loop once the output is stable. hold = analogRead(A2); // Sample the disc output and place the analog output value (0-1023) into hold. 1023 being 5v, 0 being 0v, and any value in between. Serial.println(hold); // Send the value to the serial monitor/plotter. delay(10); // Sample every 10ms. } } void stabilizer(){ state = 0; // Set state to 0 counter = 0; // Empty the counter while(state == 0){ // Do the following over and over until the sensor is considered stable... delay(10); // Add a 10ms delay, sot hat this code section is executed once every 10 seconds until stabilization is achieved. hold = analogRead(A2); // Sample the disc output and place the analog output value (0-1023) into hold. 1023 being 5v, 0 being 0v, and any value in between. if(hold > 200 && hold < 600){ // If the returned value is greater than 200, but less than 600, increment the counter integer. Otherwise, reset counte rot 0 counter++; // If yes, increment counter by 1 } else{ counter = 0; // If not, clear counter and reset it back to 0 } if(counter == 300){ // If counter equals 300 (300 consecutive stable samples in a row = 10ms x 300 = 3 seconds of consistent stability. In other words, consistently stable for 3 seconds straight... state = 1; // End this function, as the output is stable. Setting state to 1, will end the while loop. } } } // Notes: // In this code, we wait for a stable output before opening up the serial monitor. We just built off of the first piece of code. Neat, eh?