2/18/2023 0 Comments Using arduino as timer![]() Hi PaulS and UKHelibob, I actually wanted to do what you said previously, which is measured distance needs to be below 30 cm for 5 continuous seconds before that message is triggered. If the distance just needs to drop below 30 for message to be printed, but the message should be printed 5 seconds after that happens, then the code would be very different from that to print the message if the distance is less than 30 for 5 seconds. If that is the case, you need to define what should happen if the distance goes about 30 before the 5 seconds has elapsed. It sounds like the measured distance needs to be below 30 cm for 5 continuous seconds before that message is triggered. Try again to explain what is supposed to trigger the “Cut tape” message to be printed. If you are going to annotate your code to describe what is happening, use comments, so that the code can still be compiled. The code you posted hasn’t a hope in hell of compiling. Why not name the variable for the event that was supposed to have occurred? StartMillis is supposed to hold the time that some event occurred. currentMillis? If someone asks you what time it is, do you look at your watch and say “The current second is …”? Or, do you say “It is now …”? ![]() You should read up on scope, and minimize the use of global variables, especially ones that you never use (like inches). I think that each time through loop() you need to measure the distance again, and do something IF cm less than 30. It is extremely unlikely that you want a while statement here. StartMillis = currentMillis // to save the latest time so can get the diff when looping again. less than 5s jump to ‘here’.Įlse if(( currentMillis-startMillis )= period) of the ping to the reception of its echo off of an object.Ĭm = (duration/2) / 29.1 // Divide by 29.1 or multiply by 0.0343 duration is the time (in microseconds) from the sending Read the signal from the sensor: a HIGH pulse whose Give a short LOW pulse beforehand to ensure a clean HIGH pulse: The sensor is triggered by a HIGH pulse of 10 or more microseconds. Myservo.attach(9) // attaches the servo on pin 9 to the servo object Int pos = 0 // variable to store the servo position twelve servo objects can be created on most boards Servo myservo // create servo object to control a servo Unsigned long startMillis //some global variables available anywhere in the program Thank you.Ĭonst unsigned long period= 5000 // init period=5000. Ignore the codes for servo for now as it is in development mode. However, once I ran the program, it stills prints " Cut tape" even though it the period haven’t reach 5 secs. ![]() This program supposedly needs to print the signal " cut tape" only when the result of the ultrasonic sensor is less than 30 cm for 5 seconds. I’m a beginner and I recently wrote a code using millis. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |