Arduino delay in for loop - Page 1
Forum section: electronics / beginners

author: nardev
author_rank: Unknown
timestamp: November 28, 2012, 04:37:12 am
content: I seems ok but it doesn't work as i need it.for (int i = 0; i <= repetitions; i++) {myservo.write(angle);delay(duration*1000);myservo.write(0);Serial.println(i);}Instead of repeating the inside of for loop, it simply rotates servo for an angle of "angle" once, passes loop for "repetitions" number of times printing "i" index and does not do delay() and myservo.write(0)Do you have any clue what can be issue here?

author: c4757p
author_rank: Super Contributor
timestamp: November 28, 2012, 05:13:35 am
content: I don't see anything obvious, but a few thoughts (apologies if you think they're too simple, I don't know what you know):I don't know what 'duration' is, and I haven't touched an Arduino in a long time nor used its programming environment, but watch out for overflow. The delay duration will be truncated at a certain length (could be 8 or 16 bits, I don't remember), which could end up making it tiny. You may need to specify a width for the number.Edit:Seems that the default 'int' is sixteen bits, so overflow will happen at 32768 if that is how you have defined 'duration'. If you define it as 'unsigned int' you'll get up to 65535, and 'long' gives you enough space for twenty-four days' worth of delay.Some servos rotatetothe angle from an origin rather than rotating by that many degrees; make sure yours is the right type. Rotatingto90° one hundred times is the same as doing it once.If you're powering it directly from the Arduino's power supply without adequate decoupling, the power spike when it turns on could be interfering with the microcontroller, leading to weird behavior. Throw a couple capacitors (100uF in parallel with 1uF or so) across it or use a separate power supply.Are you sure that's your exact code? Did you copy and paste it into the forum?

author: nardev
author_rank: Unknown
timestamp: November 28, 2012, 05:19:03 am
content: "duration" is int that i read from serial input.I removed it and put it to delay(1000) only.But same i got problem.Literary it does this:- Turns servo to the "angle"- Prints this for 10 repetitions:012345678910- Moves servo back to 0.

author: nardev
author_rank: Unknown
timestamp: November 28, 2012, 05:22:35 am
content: Ou, it seems that i need to add delay(duration) even before myservo.write(angle);Something like this:delay(500);myservo.write(angle);delay(500);myservo.write(0);Serial.println(i);Now it works.

author: c4757p
author_rank: Super Contributor
timestamp: November 28, 2012, 05:25:13 am
content: Sorry, I should have noticed that right away. (In my defense it is midnight where I am and I've been up since 3:00) Yeah, without the second delay it's sending the 'rotate to angle' command before the motor can even start moving to zero.(Edit again. Dammit, I should just go to bed)

author: nardev
author_rank: Unknown
timestamp: November 28, 2012, 05:27:20 am
content: Quote from: c4757p on November 28, 2012, 05:25:13 amThank you, andit's so strange once you move from pure software for desktop or web to this peaces of plastic, silicium, wires etcp.s. Here, in Sarajevo is 6:27 AM

author: c4757p
author_rank: Super Contributor
timestamp: November 28, 2012, 05:36:52 am
content: Quote from: nardev on November 28, 2012, 05:27:20 amThat could go either way. Just woke up? That's fine. Near the end of your "day"? Then I feel extreme pity for you.And yeah, it is a big leap from software to electronics and microcontroller firmware. I made the jump too, and I like it so much more. Not too sure my family cares for it though; I used to only make a huge mess of my computer desktop. My "real world" was quite tidy. Now I've been sitting at my workbench for a few hours and I just took a sock off to retrieve a tiny axial capacitor that seems to have made its way in from the floor...

author: nardev
author_rank: Unknown
timestamp: November 28, 2012, 07:39:45 am
content: Well, it's kind like day startBut i believe this one will end before sun set

author: ToBeFrank
author_rank: Regular Contributor
timestamp: November 28, 2012, 05:27:47 pm
content: Not sure if it's intentional or not, but your loop is doing (repetitions + 1). In other words, if repetitions is 1, you'll execute the loop twice, not once.

author: nardev
author_rank: Unknown
timestamp: November 29, 2012, 12:19:30 am
content: Quote from: ToBeFrank on November 28, 2012, 05:27:47 pmyes, that is fin.. i mean..if it was "production" that would matter