Go Down

Topic: Placing of setup instructions (Read 296 times) previous topic - next topic

vagulus

I am working through the Arduino ShiftOut Tutorial.    Looking at Code Sample 2.2 2 Byte One By One I notice the lines 107 and 108 are
   
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);


I would have expected to find these in setup() so there must be a reason that they are in a function.

What reason would that be?

Thanks
"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977))

jbellavance

#1
Dec 16, 2017, 02:02 am Last Edit: Dec 16, 2017, 02:02 am by jbellavance
I imagine that the author wanted to let people just copy/paste the function into another sketch and made sure that all the code would be there to function correctly.

You are right in thinking that the proper place for those two lines is within setup(), as they would be executed only once, before the loop(). Having them executed every time the function is called is wasted time, but causes no harm either.

Jacques
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

vagulus

"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977))

UKHeliBob

Note too that there is a shiftOut() function as part of the Arduino core

https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/

It takes the data and clock pin numbers as parameters so for convenience it presumably sets their mode within the function.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jbellavance

Quote
it presumably sets their mode within the function
In the example code, the modes are set in the setup() section of the sketch. I don't see any evidence that the function sets the modes of either pin.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up