Hi,
I'm trying to operate a motor with arduino uno: the motor should slowly build up its speed than fading down, but only stop when a pushbutton is pressed. To do so, I combined 2 codes that separately worked fine. Ones I put them into one code together it didn't work. In the code are 2 control systems in the main loop: for & if...else. Somehow it skips the if...else part.
The delay() prevents the button pin being read for 5 seconds. As a result you have to be pressing the button at just the right time for it to be noticed. How long are you holding the button down and do you really want/need that delay() ?
I wrote a demonstration sketch that (hopefully) illustrates how different things can be controlled at the same time - including detecting a button press. You may find it useful.
Two points - loop() is just that it is a loop so ditch the for loops then take a good long look at blink without delay. If you use a delay of more than a milli second or so you have got it wrong! No excuses, IT IS WRONG.
I think I should give some specification:
What I try to do is a range of assignments. First the motor should speed up and slow down again. A micro switch is closed (shortly) (this is the pushbutton) EVERY cycle of the motor. But should ONLY be seen after a whole range of cycles..
I've put the delay in the end of the sketch to let the motor run slowly for 5 sec. just before it is supposed to stop.
As I understand the delay can cause some problems. I'll look into this.
The sketch 'Demonstration code for several things at the same time' looks very useful. I wonder though does it also implies 'several things at different times'? This is in the end what I try to do.
In mean time I'll do some more study en try-outs.
Thanks,
Vonky
vonky:
The sketch 'Demonstration code for several things at the same time' looks very useful. I wonder though does it also implies 'several things at different times'? This is in the end what I try to do.
It uses the exact same technique as "blink without delay" and it can be used to time things whenever you want. The only difference is that "blink without delay" blinks one LED whereas my sketch blinks 3 LEDS, toggles another LED with a switch and runs a servo.
In reality an AVR MCU can't even do two things at the same time - it does just does them one after the other very quickly. You can also get it to do them one after the other very slowly. Just change the interval between events to suit yourself.