Repeat the quick motions every few seconds, and the value of Triggers should increase each time.You should see the message “Triggers = 1” appear in the SimpleIDE Terminal. Now, trigger the sensor with a quick movement, and then hold still.Replace the print statement with the if.Add a global variable named Triggers, and initialize it to zero.Click Save Project As, rename your project, and save it to the My Projects folder.The changes below take care of all these issues. Also, it might be helpful to see how many times the sensor has been triggered. That is more frequent than needed once the sensor is triggered, and maybe not frequent enough if you want your system to respond immediately to a detected motion. The example code above checked the sensor’s output about every 200 ms. You may noticed that if you make just a quick movement, the sensor stays in its triggered state for several seconds even if everything is still again. Together, these let the display use just one line and update cleanly.įinally, pause(200) slows down the loop a little bit. CLREOL means “clear to end of line” which erases any other characters that may be on the same line in the terminal. HOME returns the cursor to the upper left position before printing the text and value of state. The %c formatter in the string is needed for the HOME and CLREOL control characters. Next, a print statement displays the value of state in the SimpleIDE Terminal. If the I/O pin detects 0 V, no motion was detected and a 0 is stored in state. If the I/O pin detects 3.3 V, the PIR sensor detected motion and a 1 is stored in state. The statement int state = input(5) means “check I/O pin P5 (which is connected to the PIR sensor) and store the result in the local int variable named state.” The code is entirely inside an infinite while(1) loop. You should then see “state = 1” in the SimpleIDE Terminal.įirst, this program includes the simpletools library for its print, pause, and input functions. Now, wave your hand in front of the sensor’s dome.Hold still until you see “state = 0” in the SimpleIDE Terminal.Pause(200) // Pause 1/5 second before repeat Print("%c state = %d%c\n", // Display sensor Int state = input(5) // Check sensor (1) motion, (0) no motion #include "simpletools.h" // Include simpletools library Copy and paste the program below into SimpleIDE.Turn on power to your board’s prototyping area again (PWR switch to 1 or 2 for the Activity Board (original or WX version)). ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |