arduino lesson for loops for simple led circuit _ technology tutorials

10
In this lesson we will create a circuit and write arduino code to control two LED’s. You can jump right to the video, or read through the tutorial. ARDUINO, TUTORIAL ARDUINO LESSON 3: FOR LOOPS FOR SIMPLE LED CIRCUIT JUNE 25, 2014 | ADMIN | 3 COMMENTS Technology Tutorials

Upload: manjunath-kalkutagi

Post on 05-Nov-2015

236 views

Category:

Documents


2 download

DESCRIPTION

basic knowledge for Arduino with basic led, Arduino IDE, OpenSource

TRANSCRIPT

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 1/10

    In this lesson we will create a circuit and write arduino code to control two LEDs.

    You can jump right to the video, or read through the tutorial.

    ARDUINO, TUTORIAL

    ARDUINO LESSON 3: FOR LOOPS FORSIMPLE LED CIRCUITJUNE 25, 2014 | ADMIN | 3 COMMENTS

    Technology Tutorials

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 2/10

    In the earlier lessons we wrote our first programs and built our first circuit. At this time youshould be getting comfortable with how the breadboard works and how to work withvariables and digitalWrite commands in the arduino IDE (Integrated DevelopmentEnvironment). Now we are going to build a slightly more complicated circuit for controllingtwo LEDs. Since we want to control each one individually, you will need to have a separatearduino pin control each LED and each LED should have its own current limiting resistor (330ohms). You should be able to sketch out your own circuit at this point. This is a diagram of thecircuit we will be using. Yours does not have to be exactly like this, but it should have the samefunction.

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 3/10

    This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

    Notice that in this circuit, the shorter leg of both LEDs needs to be connected to ground. Inorder to accomplish this we run a wire from the ground pin on the arduino to the top row ofthe breadboard. This makes the top row ground. Now any device that needs to be groundedcan just be connected to the top row, since that row of the breadboard is connected all theway across (See LESSON 1). Also note that both LEDs have their own 330 ohm currentlimiting resistor, and remember that the direction matters on diodes . . . be sure to put them inwith the longer leg connected to the more positive part of the circuit . . . in this case, the longerleg should be connected to the resistor (since the resistor connects to the + voltage comingfrom the arduino pin).

    When you get the circuit built, it should look something like this:

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 4/10

    Photograph of our Arduino Circuit for Controlling Two Diodes.

    Now thatyour circuit is built, we are ready to do some programming.

    Our objective in this exercise is to be able to independently control the LEDs. We will want toblink the red one ten times in a row, and then blink the yellow one once. A blink should beturning LED on, leaving it on for a quarter second, turning it off, and leaving it off for a quartersecond. Then that sequence will be repeated. So, we will need to think about the variables wewill need. We have two LEDs so we will need to declare two variables to indicate the pins thatthe LEDs are connected to. In the schematic we connected the red LED to pin 9 and the yellowLED to pin 10. Also, since we will be blinking two LEDs, we will need onTime and offTimevariables for each LED. You should go ahead and open your arduino IDE and set declare yourvariables. Think about what you are going to name your variables . . . you do not have to usethe same names I use. My code looks like this:

    int redLEDPin=9; //Declare redLEDPin an int, and set to pin 9 int yellowLEDPin=10; //Declare yellowLEDPin an int, and set to pin 10 int redOnTime=250; //Declare redOnTime an int, and set to 250 mseconds int redOffTime=250; //Declare redOffTime an int, and set to 250 int yellowOnTime=250; //Declare yellowOnTime an int, and set to 250

    Declare Variables and Assign Values Arduino

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 5/10

    Now that your variables are declared, what should you do next? Thats right! You need towork on your void setup(). In your void setup you will need to set your pinModes. Since you areusing 2 arduino pins this time, you need to issue two pinMode commads as follows.

    Things are moving along and we are now ready to do our main business in the void loop().Remember our goal is to blink the Red LED ten times, and then blink the yellow LED one time.

    int yellowOffTime=250; //Declare yellowOffTime an int, and set to 250

    void setup() {pinMode(redLEDPin, OUTPUT);// Tell Arduino that redLEDPin is an output pinpinMode(yellowLEDPin, OUTPUT);//Tell Arduino that yellowLEDPin is an output pin}

    void loop() {digitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTime

    Do your Void setup Arduino

    Now do the Void Loop Arduino

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 6/10

    OK, now you are ready to run your code. If you did it correctly, it should run, and blink the redLED ten times, and then blink the yellow LED one time. If it does not run correctly, you need todebug your code. If it does not work, it is because you made a mistake. Most of the time it issilly typos or forgetting to end lines with a semicolon. Check your work, and you will find yourerror. Sometimes it helps to have someone else look it over with you.

    OK, hopefully you have your code and circuit working now. You can play around with theparameters, and you can see that you can make the LEDs do whatever you want them to.

    Now imagine I asked you to make the red LED blink 25 times and then the yellow blink tentimes. The problem becomes that it gets very tedious to continue to copy and paste the code,

    digitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(redLEDPin,HIGH); //Turn red LED ondelay(redOnTime); //Leave on for redOnTimedigitalWrite(redLEDPin,LOW);//Turn red LED offdelay(redOffTime);//Leave off for redOffTimedigitalWrite(yellowLEDPin,HIGH); //Turn yellow LED ondelay(yellowOnTime); //Leave on for yellowOnTimedigitalWrite(yellowLEDPin,LOW);//Turn yellow LED offdelay(yellowOffTime);//Leave off for yellowOffTime}

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 7/10

    and it eventually becomes impossible to keep track of how many times you have pasted thecode in. We need a better way of doing repetitive tasks, like blinking. Luckily there is what iscalled a for loop a for loop will repeat a clause, or a group of commands, or lines of code aspecified number of times. The for loop looks like this:

    OK, there is lots going on with this new code, so lets break it down. First, notice the open andclose curly brackets. All of the code or command lines you put between the curly brackets willbe the code that is executed in the for loop. You can put as much or as little code as you wantin the for loop. Now lets look at the first line that actually initiates the for loop. Inside theparenthesis are the parameters or arguments that define the behavior of the loop. Notice firstthat we have introduced a new variable, j. Since we do not need this variable in other parts ofthe program, we make it a local variable. That is, we do not declare it at the top of theprogram, but declare it just when we use it. That is why we have int j=1. The int is declaringthat we are going to use a new variable called j. Now j=1 is telling the loop to start with a valueof j of 1. Then the j

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 8/10

    WOW, that is a huge improvement over our original code. The for loop makes it much easierto manage things. The one thing that I dont like about what we did in the code above is thatwe looped to the constant value of ten. It would be better and smarter to declare a newvariable at the top of the program, The new variable could be numRedBlinks. Then in the forloop we would loop until j

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 9/10

    3 THOUGHTS ON ARDUINO LESSON 3: FOR LOOPS FOR SIMPLE LED CIRCUIT

    JANUARY 15, 2015 AT 2:45 PM

    Great web site great teacher ,i have done the first 5 lessons and im very happy. My firstworking program look like :

    /*This Sketch was write by Daniel P. and inspired by great tutorials with prof. PaulMc.Whorterpost at http://www.toptechboy.comTurns on and off a LED connected to any digital pin,in my case i use pin 13,using the delay() function.

    This example code is in the public domain.

    */int red=13; //Declare my red led an integer, int and set to pin 13int redOnTime=250; //Declare how long my led will be on and set to 250 milliscondsint redOffTime=250; //Declare how long my led will be off and set to 250 millisecondsint numRedBlink; //Declare a global variable where i store an integer , how many time myled will blinkingString redMessage=The Red Led Blinking; //Declare a string variable

    void setup(){

    pinMode(red,OUTPUT); //Tell Arduino my red led is an OUTPUTSerial.begin(9600); //Turn on Serial Port}

    void loop(){

    Serial.print(How Many Times Do You Want The Red Led To Blink ? );while(Serial.available()==0){} //Wait until the user enter an integernumRedBlink=Serial.parseInt(); //Assigned a value to numRedBlinkSerial.println(numRedBlink);Serial.println(redMessage);for(int j=1;j

  • 3/17/2015 ArduinoLesson3:ForLoopsforSimpleLEDCircuit|TechnologyTutorials

    http://www.toptechboy.com/arduino/arduinolesson3simpleledcircuit/ 10/10

    Serial.println(j); //Print the counterdigitalWrite(red,HIGH); //Turn red led ondelay(redOnTime); //Keep on my led for redOnTimedigitalWrite(red,LOW); //Turn red led offdelay(redOffTime); //Keep off my led for redOffTime}Serial.println(" ");}

    Thanks a lot and keep teaching as.

    MARCH 14, 2015 AT 1:12 PM

    hello sir..may I ask ,how do we want to stop the loop after one complete cycle? Btw I reallyappreciate this lesson..Thank You

    MARCH 14, 2015 AT 1:18 PM

    The void loop continues to loop. It is not something you can stop. If you want to dosomething only once, put it in the void setup

    kukkuk

    admin