Is that why there was a delay(30) there?
Well, I'm not saying it is the problem, but just that it may be, as some devices are not super fast in returning and could block the following operations, not sure, but the fact that it is in the setup, and does follow the PWM move statement, indicates that it may be a possible cause in the loop section, which is running very fast... perhaps you can do a few tests to try to break it, with and without it and different values, etc... What happens if you put the Serial.print statements before the .PWM call?
Ha ha ha, I have enough problems trying to get things to work without trying to break them - lol! But the issue seems to have been resolved. It loads no matter where I put or don't put the Serial.print statements
David
Ha ha ha, I have enough problems trying to get things to work without trying to break them - lol!
🙂
Nevertheless, something to keep in mind for future troubleshooting... glad you got it resolved!
Eric
David
Can I use a For loop in the Declaration "area" or in the Setup?
David, a for loop is a statement, so you can use it fine in setup(), but not in the declarations.
Looking a your V2,what I'm wondering is if you are ok to wait 2.2seconds when switch0 is toggled, before checking switches 1 to 4 ?
Eric
because such a for loop :
for(angle = 90; angle<120; angle +=1){ // this is the range of movement delay(75); pwm.setPWM(0, 0, angleToPulse(angle) ); // move the servo clockwise }
will run during 29 x 75 =2175 ms
Edit: hum, actually it's 30x75...
Eric
Maybe this can help you understand better the pros/cons of delay vs. millis :
https://dzone.com/articles/arduino-using-millis-instead-of-delay
Eric
Also this one (there are hundreds like that on the web because it's probably the first and foremost issue encountered by developers adding features to an existing sketch).
https://learn.adafruit.com/multi-tasking-the-arduino-part-1/overview
Eric
Here's me thinking that I'm getting my head around this and you go and throw me in the deep end - lol!
The 2nd example is easier to understand than the 1st. But I get the impression that using millis() is more for when you need timing?
If you don't mind, clarify something for me. If each section of code in the Loop takes 2.2 secs, does that mean if someone flicks the toggle for number 4 (the 5th), they could wait up to 10 secs before there's any movement?
David
Sorry Eric, I didn't see your post with the code in it before I posted my own. I'll study it.
David
If each section of code in the Loop takes 2.2 secs, does that mean if someone flicks the toggle for number 4 (the 5th), they could wait up to 10 secs before there's any movement?
I'm back online...
Answering your question : each of your for loops in code V2 will use about 2.2secs, but they will only execute if the angle is not equal to the target, so only those for switches that have just been flipped since the last execution of void loop() would run...which could be quite a long total delay depending on the number of toggled switches.
The worst case would be if all the 5 switches are flipped in a very short time : chances are that the next execution of void loop() will run 5 for loops, one per switch, each around 2.2s
Eric
Eric, I've had a look at your sketch and, whilst I can follow most of it, there are a number of black holes. I can't test it at the moment, I'm waiting for more servos to arrive. I've already fitted the ones I have to the underside of my layout.
I have a bunch of questions regarding your sketch (if you don't mind). But one of the things that I find is missing is a method of stepping through a sketch so that one can follow the path of the program. What do you use?
David