I was happy to stumble across Robin2's 9 chapter lesson (Planning and Implementing an Arduino Program - Programming Questions - Arduino Forum), and decided to follow along writing my own sketch (but exactly like Robin's) and understanding as I go.
But right off the bat (batt ?) I had to resort to trial and error to get it to run. I would have asked my question as a reply to the lesson, but I guess it is not a reply kind of format.
In the Introduction, he says that cutting and pasting code from others will get you into trouble if you don't know what is going on. Yep. And as his 'Planning and Implementing an Arduino Program' is a lesson for beginners like me, I will ask a beginners question. Actually I am not a beginner, I have made many functioning sketches and enjoy making things go using the Arduino. But it is almost all cut & paste and fadge together. So I said to myself, self, follow his lesson and you will learn the right way to do this.
In Chapter 2 (Organization) he shows a bunch of functions. Then he shows what are (i guess) calls in the Loop function to those functions, and says that, with the previous functions, it will compile.
When I tried to cut and paste the functions into the sketch I didn't know where to put them. I reasoned Setup sounds like a place to set things up, like functions. Nope, wouldn't compile. Next I stuck them at the very top of the sketch, before anything else, and it complied fine.
And in his own version he put the functions in the Loop function.
My question is this (and it IS a lesson for beginners), what goes before Setup ? what goes In Setup, and what goes in Loop ?
Look at any of the examples that ship with the IDE. It will be very obvious there, what goes in setup(), what goes in loop(), and what goes in neither...
Thanks. I notice that some programmers do put the function inside the loop (Robin2 Chapter 2), and it appears to run.
Is the idea that there is no point in running a function over and over in a loop, when it could be defined once and for all outside of the setup and the loop ?
pratto:
My question is this (and it IS a lesson for beginners), what goes before Setup ? what goes In Setup, and what goes in Loop ?
The intention of Planning and Implementing a Program
was that you would work through the complete tutorial. At the bottom of Chapter 8 the complete program is attached as LessonG.ino
Thanks for taking the time to help us beginners. It is not uncommon for me to make a mistake in understanding things, and it looks like that was the case with your Lesson. I was in no way criticizing. Just trying to understand as I go. If I could have asked you directly in a reply I would have.
My mentioning your name in the title of my post was my attempt at saying 'Robin, may I ask you about this?'