So in my project im looking to use a motor to move a platform left to right, which goes up and down at certain points. I want the platform to stop moving axially and then move up, and then go axially again. I'm using an ultrasonic sensor to determine where the stops are. The issue im having is when the ultrasonic senses a certain range, I can get it to move the platform up and then down but cant get it to start moving left to right, it still reads its in that range so it moves up again.
I was wondering if there's a way to cause a delay on one function, while at the same time having something else occur? Or is there no workaround a delay, it stops everything else at that section of code?
the delay function is easy but its only for very simple projects.
there is a very accurate built in timer running on arduino boards.
you can use it for timing without stopping your code.
you use millis() to get a reading and the concept is to keep checking the time passed since the last reading.
from there you can set up timers and limit and trigger things however you like. the sky is the limit. a quick google of millis() will give plenty of examples.
unsigned long tick;
int freeze1;
int freeze2;
void setup() {
freeze1 = 60; //<delay 60 seconds
freeze2 = 20; //<delay 20 seconds
}
void loop() {
if(freeze1 == 0){
// you can put stuff here that is delayed by first timer
}
if(freeze2 == 0){
// you can put stuff here that is delayed by second timer
}
if(millis()-tick>1000){
tick=millis();
// this section happens every second without interupting code
if(freeze2>0){freeze2--;}
if(freeze1>0){freeze1--;}
}
}
Ianuno:
it still reads its in that range so it moves up again.
IDE -> file/examples/digital/state change detection
You need to know when the platform *gets to * position, not so much when it's *at * position. These are two different things. Here's some code which generates a high/true/logic one on platformOS1 for one program scan when platformInPosn1 goes true.
/*
----- Generate a positive going one-shot pulse on platform in position
*/
platformOS1 = (platformInPosn1 and _OSRSetup1); // all vars are bool
_OSRSetup1 = !platformInPosn1;
So you use platformOS1 instead of platformInPosn1 to start whatever cycle you need to do.
So you got your platform to move left and once the “sky” above it is clear it goes up. After it goes up it goes down.
Using state variables will help you get it going left/right again but “then what”? How far does it need to travel before you allow it to resume checking to see if it can go up?