Pages: [1]   Go Down
Author Topic: Simple (hopefully) question  (Read 898 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to trigger 2 things to happen at different millisecond intervals after i have taken my finger off a button.. I want the first command to happen within 500 milliseconds of taking my finger off the button and the second to happen between 500 and 1000 milliseconds..

I can get the first one happening up to 500 milliseconds with this code..

if ((digitalRead(button) == LOW) && (millis() - previousMillis) < 500) {SendCode();}

but how can i send a different code when the interval is greater than 500 but less than 1000 ?

I feel like i should add another line and it should just be

if ((digitalRead(button) == LOW) && (millis() - previousMillis) > 500 < 1000) {SendCode();}

But that hasnt worked.. Sorry if thats a stupid question but im still learning and ive been stuck on this for a few hours..
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Should be something like this:

Code:
if ((digitalRead(button) == LOW) && (millis() - previousMillis) > 500 && (millis() - previousMillis) < 1000) {SendCode();}

But realise there's no way to know for sure whether this code actually fires. If - due to some unknown delay - the loop takes longer than a second to repeat, the code will never actually be run. It's unlikely, but very possible.

Oh, and also, without some countermeasure, it's very likely the SendCode() statement will be exectuted multiple times. This may be exactly what you want, but you should be aware of it.
« Last Edit: January 09, 2011, 03:47:26 pm by Gido » Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If - due to some unknown delay - the loop takes longer than a second to repeat, the code will never actually be run. It's unlikely, but very possible.

That's a good point - what do you want to happen if it cannot do it within one second?

My suggestion is to have it fire the first one at time 0, and the second one at time 500.

If you want a random delay between 0 and 500 and 500 that's a different story.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool that worked.. So is it ok to add multiple "&&" queries when using if controls ? For instance, could i add say 4 or 5 "&&" to create a really specific question ?

Also is there a way i can do something for a certain period of time without needing to put millis() into a variable and subtract it again to determine a count ? Is there a way to use SendCode() for 500 milliseconds ?

Thanks for your help.
« Last Edit: January 09, 2011, 04:12:50 pm by ebulb » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i should note its not totally critical if something held up the loop and it didnt run that code one time, as long as it worked 99% of the time..

I am working on a customised controller for a video camera.. I will be using buttons on a joystick to control zoom and other functions but for this bit of code i wanted the zoom to behave more fluently so when the user takes their finger off the button it eases back to a stop over a set amount of milliseconds, rather than just a dead stop..

Now that it looks like its working i can tweak the values to get it looking good.. I have already added 4 steps instead of 2 and its looking good. its got a kind of "rubbery" feel, but i'll keep it subtle and it will work well..
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Cool that worked.. So is it ok to add multiple "&&" queries when using if controls ? If i had an If control could i add say 4 or 5 "&&" to c reate a really specific question ?

Yes, you can do that. Try reading up on some basic C/C++ syntax, that should solve a lot of your problems before you actually have them.

Code:
Also is there a way i can do something for a certain period of time without needing to put millis() into a variable and subtract it again to determine a count ? Is there a way to use SendCode() for 500 milliseconds ?

With the code you have now, you are not executing SendCode() for 500 milliseconds. It would be more accurate to say that there is a 500 ms window in which zero, one or multiple calls to SendCode() can be made.

If you really want to execute some specific command an unknown number of times in the next 500 ms, you should use a loop:

Code:
unsigned long start = millis();
while (millis() - start < 500) {
   // Code in here is exectuted n times during the next 500 ms (n >= 0)
}

Edit:

Quote
I am working on a customised controller for a video camera.. I will be using buttons on a joystick to control zoom and other functions but for this bit of code i wanted the zoom to behave more fluently so when the user takes their finger off the button it eases back to a stop over a set amount of milliseconds, rather than just a dead stop..

Ah. In that case, it might be too much trouble to even consider using milliseconds. That's human thinking. Thinking in code, it's much easier and robuster to, say, keep zooming for 100 iterators after a user takes the finger off the button. You could slowly decrease the zoom speed to zero after the button is raised. This is simpeler and better defined than messing around with time intervals.
« Last Edit: January 09, 2011, 04:19:25 pm by Gido » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I actually have a line of code before the one you helped me with that does call SendCode() before the 500 milliseconds point.. The problem i had was, i wasnt able to get anything between 500 and 1000 or subsequent intervals after that but now i have this.. I have tweaked the times bit and added extra stages to get it looking better..

if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) < 200) {SendCode(40,0x18);}
if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) > 200 && (currentMillis - previousMillis) < 400) {SendCode(40,0x16);}
if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) > 400 && (currentMillis - previousMillis) < 500) {SendCode(40,0x14);}
if ((digitalRead(button) == LOW) && (currentMillis - previousMillis) > 500 && (currentMillis - previousMillis) < 600) {SendCode(40,0x12);}

the values in send code are specific for the camera and that portion of the code is adapted from someone elses work who had already gotten arduino working with the camera protocol.. im so grateful that portion was done because i wouldnt have gotten that going on my own..

You have to excuse me, im really fumbling along at the moment so im not sure what iterators are ?  the only way i thought of doing this was with millis()..
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You have to excuse me, im really fumbling along at the moment so im not sure what iterators are ?

In this case, 'iterators' is a typo, I'm sorry to say. smiley I meant to type 'iterations'. Which, in the context, would of course just mean doing something 100 times.

But if the code now works to your satisfaction, that's all you can hope for, right?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah its all good, thanks for your help.. Maybe down the track i will enahnce it as i get better.. i like to make things work as efficiently as possible, but as long as its functioning thats the main thing.

cheers
Logged

Pages: [1]   Go Up
Jump to: