pinmode ( ); blink · 2018. 2. 10. · pinmode (pin, mode); pinmode sets up the specified pin to...

10
Blink and The red male pins provide constant 5v of current. Using the red voltage pins without a switch means the LED will always be on. Many times LEDs are meant to turn on and off for a purpose or flash to draw attention. Now that you can make a complete circuit, let’s learn to program the LED. The Arduino IDE, the programming platform and communication software will be used to control the LEDs. You will not need the switch for this activity. int; void setup ( ) { } pinMode ( ); void loop ( ) { } digitalWrite ( ); delay ( ); 18

Upload: others

Post on 04-Feb-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

  • Blinkand

    The red male pins provide constant 5v of current. Using the red voltage pins without a switch means the LED will always be on. Many times LEDs are meant to turn on and off for a purpose or flash to draw attention.

    Now that you can make a complete circuit, let’s learn to program the LED.

    The Arduino IDE, the programming platform and communication software will be used to control the LEDs. You will not need the switch for this activity.

    int; void setup ( ) { }pinMode ( );void loop ( ) { } digitalWrite ( ); delay ( );

    18

  • The IDE must be installed on your device or you may sign up to use the web editor. https://www.arduino.cc/en/Main/Software

    Once the code is written or pasted in the sketch section of the IDE, click the compiler to check for errors.

    If there are no errors, click on the upload button to send the code to the Arduino. (Clicking Upload will automatically compile before uploading.)

    This is where the code is written or pasted.

    Upload to the microcontroller.Compiles the sketch (Check for errors)

    Programs are uploaded to the Keyestudio UNO using the Arduino IDE, which stands for Integrated Development Environment

    Code errors will show up in this section.

    19

  • 2. The first time you try to connect the UNO and upload a sketch you will need to select the correct COM port for the UNO to connect. You may also have to select your Arduino board.

    “Avrdude:...not in sync” is a common error code and usually means you need to change the USB COM port number.

    3. Click Tools, Port, and choose the COM port number that lists the Arduino Uno.

    1. Open the Arduino IDE. (Click on the Start menu on the computer and search for Arduino.)

    Connecting the UNO to the Computer for the First Time

    20

  • Build the Circuit to make the LED blink.In order to be able to control the blinking of the LED, it must be connected to a yellow signal pin.

    1. Unplug the USB cable.

    2. Connect the resistor wire to the yellow #7 signal pin.

    3. Connect the ground wire to one of the blue GND pins.

    4. Plug the USB back in.

    5. Enter the code on the next page.

    Switch set to 5v.

    Yellow pin #7.

    Always unplug the power when changing your circuits.

    21

  • //LED Blink Turns an LED on and off for one second.

    int led1 = 7;//tells which pin is connected to the LED

    void setup() {

    pinMode(led1, OUTPUT);// sets up the pin as an output

    }

    void loop() {

    digitalWrite(led1, HIGH);//turn LED on

    delay(1000);// wait for 1000 milliseconds (one second)

    digitalWrite(led1, LOW);//turn LED off

    delay(1000);//wait one second

    }

    A program in the Arduino language is called a SKETCH.A sketch has 4 main parts: The comments, declarations, setup, and loop functions.

    The setup function runs once to tell the Arduino what variables and parts (pins) you will be using.

    The loop function is where most of your program lives and is executed after the setup is complete. The Arduino will perform the commands in the loop program over and over until you unplug the power.

    The declarations tell what variables will be used throughout the program. (Like ingredients in a recipe.)

    The comments are notes by the programmer that follow //. The Arduino ignores the comments.

    22

  • int led1 = 7;

    void setup() {

    All the setup must come between the two “setup” curly brackets. }

    void loop() {

    All the programming instructions (code) must come between the two “loop” curly brackets.

    }

    Important Points!!The computer only reads the declarations, setup, and loop functions.

    The setup telling the Arduino how to set up the pins.

    The loop - where the program runs over and over.

    The declarations- what components your sketch will use.

    Only type void setup () ONE TIME.

    Only type void loop () ONE TIME.

    23

  • 1. Type the code to the right into the Arduino IDE or go to the Learn tab at odysseyboard.com and get the code for Lesson 2. There are two options for acquiring the code. You can download the Arduino .ino file directly to your IDE or copy the code from a Google Doc link.

    2. Open the downloaded .ino file or paste the copied code into the IDE. Be sure to delete any code that may be in the sketch section.

    Upload the code to make the LED blink.

    3. Click the upload button. Make sure you have no errors and it says “Done Uploading” at the bottom of the IDE and your LED should blink on and off for one second.

    If does, congratulations! Go on to the next slide and try some of the exercises.24

  • Blink Exercises

    1. Look closely at the code and change the amount of time the LED blinks on and off.

    2. Change the pin to which the LED is connected from pin 7 to pin 2. (Think about what will have to be changed in the code.) Change the code to make it blink again.

    3. Add one more LED (with resistor) and make them blink alternating one at a time.

    4. Make the LEDs blink at the same time. Change the speed of the blinking.

    5. Connect 4 LEDs (with resistors, of course). Modify the code to turn on each LED in order and then off in order. -HINT: Hook them up one at a time and make sure the new one works before you add the next one.

    6. Now that you have 4 LEDs working, make them turn on and off in a different pattern.

    Remember to always unplug the power when you are changing wiring in your circuits.

    25

  • Programming Syntax: int var = val; int is the primary data type for storing a number. You need to set 2 parameters:

    var - the name you give the variable val - the value or pin # you assign to the variable

    Ex: For a red LED in pin #8, you may type : int ledRed = 8;

    void setup ( ) { Setup and Loop are functions that already have a lot of code built in.

    pinMode (pin, mode); pinMode sets up the specified pin to behave as an input or output. You need to set 2 parameters: pin - the number or name of the pin you wish to setmode - typically will be an input or output. Ex: pinMode (ledRed, OUTPUT);

    }void loop ( ) { The UNO executes the part between the loop { …. } over and over. digitalWrite (pin, value); digitalWrite writes (sends) a HIGH (on) or LOW (off) value to a digital pin.

    You need to set 2 parameters: pin - The pin name or number you wish to write (send voltage) to.value - HIGH or LOW Ex. digitalWrite (ledRed, HIGH);

    delay (ms); delay tells the program to wait the specified milliseconds before going on. You set 1 parameter: ms - The # of ms you want to pause. Ex. delay (2000);

    }

    Understanding the coding syntax. Syntax is like grammar rules for computer language.

    26

  • Blink programming syntax reference chart.

    27