Tutorial 12: For Loop Iteration: Arduino Course for Absolute Beginners (ReM)

preview_player
Показать описание
🤩 FREE Arduino Crash Course 👇👇

We designed this circuit board for beginners!

SHOP OUR FAVORITE STUFF! (affiliate links)
---------------------------------------------------

We use Rev Captions for our subtitles

Arduino UNO R3:

Budget Arduino Kits:

Multimeter Options:

Helping Hands:

Soldering Stations:

AFFILIATES & REFERRALS
---------------------------------------------------

FOLLOW US ELSEWHERE
---------------------------------------------------

*Click Below to Read About This Topic on Our Website*

*Description:*
Arduino Course for Absolute Beginners

There are few functions so useful that you find them everywhere. The 'for loop' is an example of this type. A For Loop repeats an action for a specified number of iterations, reducing the lines of code that need to be written thus making the programmers life easier.

In this example we are setting out to make a row of LEDs light up somewhat similar to Kit in Knight Rider.

You Will Need

LED (6)
220 Ohm Resistor (6)
Solder-less Breadboard
Jumper Wires (1)
Red Canary (42)
Step-by-Step Instructions

Connect one side of a resistor into pin 2, connect the other side into a row on the breadboard.
From that row in the breadboard where you attached the resistor, connect the long leg of the LED.
Connect the short leg of the LED to one of the power strip columns on your breadboard.
Now connect a resistor to pin 3, and put the other leg in a row on the breadboard (a different one then your first LED.
Connect an LED in the same manor - make sure the short leg goes in the SAME power strip column as the previous LED.
Repeat this through pin 7.
Using a jumper wire, connect the common power strip to a GND pin on the Arduino.
Connect the Arduino to your computer.
Open up the Arduino IED.
Go to File, Examples, 05.Control, ForLoopIteration
Click the Verify button (Top Left). The button will turn orange and then blue once finished.
Click the Upload button. The button will turn orange and then blue when finished.
Watch in awe as your LEDs turn on and off in sequence.

The first executable code we find is the declaration of the 'timer' variable...

int timer = 100; // The higher the number, the slower the timing.

This integer variable with a descriptive name will set the rate between one LED turning on and off. What is so important about variables like these is how easy they allow you to alter your code and how your circuit will run. If the programmer had written the number 100 in a bunch of functions through out this program we would have had the exact same effect, but as soon as we want to speed up the blinking of the LEDs, now we have to go through and change every 100 to a 500 - what a pain - especially when you miss a couple 100's, and accidentally put a 500 where there shouldn't have bee one. These variables are like controls into your program - imagine you are on the Star Ship Enterprise, all those buttons and touch screen levers - these are your control variables.

Our next friendly block of code is setup(). In this setup() function we run into our first For Loop.

void setup() {

for (int thisPin = 2; thisPin LT 8; thisPin++) {

pinMode(thisPin, OUTPUT);

}

}

Let's consider just what is inside the parenthesis following the for loop..

for (int thisPin = 2; thisPin lt 8; thisPin++)

There are three separate statements in the parenthesis separated by a semicolon. The first statement is initialization of the counter variable used in the 'for loop' and it looks like any other variable declaration and initialization that you have seen.

int thisPin = 2;

The 'thisPin' variable is what will be used in the next block of code - or the test.

thisPin lt 8;

This is the test condition that will tell the loop to keep going or to stop. If this condition is TRUE, the code in the curly brackets of the 'for loop; will be executed again, if the condition is FALSE, the
Рекомендации по теме
Комментарии
Автор

I'm from México, I'm in the fourth semester of mechatronis. I started the course and watching your video tutorials had encouraged me to bigger project on my own. Thank you for your patient and for all the info that you give us. I hope you read this comment and keep giving us a lot more of your knowledge :) Have a good day, and NeverEver Stop learning

adriansofa
Автор

I always loved that show Knight Rider and the car with the eye going back and forth scanning everything, it was so awesome. I am 53 years old and I guess you can teach a old dog new tricks. Thank you for taking the time to do the videos. You are a gifted teacher. Do not give up your teaching! The last thing i want to say is, I saw on the introduction page to your free classes that you are a veteran. I want to thank you for your service to our country and God bless you. Again thank you for taking the time to do these videos.

mod
Автор

Like everyone else, I am more than grateful for your hardwork making these tutorials!

GonnaBeJumper
Автор

keep doing what you do. I went from not knowing what Arduino is to making my own code (all be it simple one) in a day. best thing about you is that your explanations are not rushed and you refer back to previous videos to get syntax rooted in our memories.

cameronallan
Автор

Been watching all these, following along, doing the challenges at the
end. Great work explaining this statement. I didn't have enough leds for
this one so i used a 7 segment display...only problem was it's common
anode. I used +5 for the common and exchanged all the HIGH's and LOW's in the
digitalWrite arguments, with their complements...then added the 2 leds for the

challenge at the end....everything worked out aok. This has been a great series. Thanks.

spacewalker
Автор

I absolutely love this series!
It has helped me understand some fundamental Arduino concepts and for that I say thank you! You clearly have an innate understanding of these things to explain them so well and the blend of humour and graphics make these videos enjoyable to watch.

TheeSlickShady_Dave_K
Автор

Man, you're great! You explain each piece of code and you do this so well. Thanks for all!

manoelfelipe
Автор

Now a pro with Arduino, all the credits comes to your massive efforts u hv put in these tutorials.
thanks a lot!
And do make a lot more project videos on these.

varunvenkats
Автор

These tutorials are awesome. I'm learning ten times faster than I would have on my own and I'm having a blast. Thank you so much!!

JeanneduPlessis
Автор

woow. I cant believe the HUGE and AWESOME WORK you have done on this tutorials my friend. 
I dont think words could express my gratitude. 
GOD BLESS YOU

pikubird
Автор

I only had three led's in my kit, so I adjusted the sketch. You're a great teacher. Thanks!

cptgerard
Автор

this is the first time I understood FOR loops. I am SO glad I found your videos!!

krallopian
Автор

i get so excited when i hear that intro music :)

thomasrogers
Автор

Really thankful for your videos. You are an educator, be proud!

rgggvxb
Автор

Thanks to this tutorial I finally understood "for" loop. Great job.

mbs
Автор

These videos rock. Big fan! Thanks for all your work. Truly appreciated :o)

skeleton-man
Автор

This is by far the easiest and clearer explanation about a for loop. I wish you made "for i=" programmation explanation!!!

ThierryC
Автор

Hi, just found this series, and it is very helpful. I have been playing with arduinos for over a year now and my collection of them is ever growing. That said, I used to be a BASIC programmer back in the 80's and 90's so I know some programming and even sold some basic stuff back then but this is a bit different. While the For-Next differs a lot, it is made very clear in this video - Thanks a million, it looks like I can now do more then copy other's code and modify it for my purposes!

JerryEricsson
Автор

The way you explain… makes it possible for me to participate even though I’m cixelsyd/dyslexic.
Thanks man

catchulater
Автор

Even though I’m far behind watching your amazing tutorials they are a huge help for me as I’m a beginner

patmac