arduino tutorial 1

Upload: wolf-lol

Post on 24-Feb-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Arduino Tutorial 1

    1/9

  • 7/25/2019 Arduino Tutorial 1

    2/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Figure 1: Arduino Mega 2560 Board and Bread Board

    Figure 2: Arduino Sketch Environment

  • 7/25/2019 Arduino Tutorial 1

    3/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Connecting the Board and Uploading a Sketch

    The process of uploading your Arduino Sketch to the board consists of two steps. The first step

    is to verify the sketch; the icon is the check mark (see Figure 3). Verifying the sketch compiles

    it, checking for syntax errors and turning the code into something the microcontroller can process.

    The second step is to upload the sketch; the icon for upload is the right facing arrow (see Figure

    3).

    Figure 3: Controls for Uploading an Arduino Sketch

    To upload your sketch you need to ensure that the correct port and board are selected in the

    Arduino environment. To select the correct board type go to Tools >> Board >> Arduino Mega or

    Mega 2560 (see Figure 4). Also ensure the correct processor (i.e. 2560) is selected (see Figure 4a).

    To select the correct port go to Tools >> Serial Port then select the port indicated in the lower right

    of the Arduino environment (see Figure 5).

    Once you have selected the correct board and serial port, you will be able to upload sketches to

    your Arduino board. If you perform this process incorrectly, you will be able to verify your

    sketch, but you will get an error when you try and upload your sketch.

    Incorrect Board:

    avrdude: stk500_getsync(): not in sync: resp=0x00.

    Incorrect Serial Port:

    avrdude: stk500v2_ReceiveMessage(): timeout

    avrdude: stk500v2_ReceiveMessage(): timeout

    avrdude: stk500v2_ReceiveMessage(): timeout

    avrdude: stk500v2_ReceiveMessage(): timeout

    avrdude: stk500v2_ReceiveMessage(): timeout

    avrdude: stk500v2_ReceiveMessage(): timeout

    avrdude: stk500v2_getsync(): timeout communicating with programmer

  • 7/25/2019 Arduino Tutorial 1

    4/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Figure 4: Select the Correct Board Type (Arduino Mega or Mega 2560)

    Figure 4a: Ensure the 2560 processor is selected

  • 7/25/2019 Arduino Tutorial 1

    5/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Figure 5: Select the Correct Serial Port

  • 7/25/2019 Arduino Tutorial 1

    6/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Creating a Sketch

    To create a sketch, follow the basic format below; the setup and loop functions are required,

    and more complex sketches will have additional functions. There are also many predefined

    Arduino functions (see the reference link provided). Commented text shows up as gray, and is

    used to document what the sketch is doing. Other reserved words show up as orange (functions

    and data/variable types) or blue (constants). Note that the default time unit is milliseconds.

    // Sketch Title

    // Used to Comment One Line of Code

    /*

    Used to Create a Block Comment

    */

    //Define Global Variables

    intRedLEDpin = 13;

    void setup()

    {

    // The setup function is run once when the sketch is uploaded, or if

    // the reset button is pressed

    // Declare Pins Being Used as Output or Input

    pinMode(RedLEDpin, OUTPUT);

    }

    void loop()

    {

    // The loop function runs after setup and continues on indefinitely

    // until your turn off the board

    digitalWrite(RedLEDpin, HIGH); // Turn on the LED

    delay(1000); // Wait for one second

    digitalWrite(RedLEDpin, LOW); // Turn off the LED

    delay(1000); // Wait for one second

    }

  • 7/25/2019 Arduino Tutorial 1

    7/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Test Sketches

    For each test sketch you will be examining how the hardware and software interact to control the

    behavior of the system (in this case, a blinking LED). While simple, it is enough to determine if

    you have the ability to control a system.

    Breadboard Short CourseEach breadboard is a little different in terms of what connections the board has (see

    manufacturing specifications for details (and often you can see the connections by looking at the

    back of the board. For the bread board provided in the lab, the connections are shown in Figure

    6. Connections between components are made on the same row of the breadboard; though the

    divider between column e and f on the lab boards separates each row into (effectively) two

    different rows (i.e., row 1 a-e and row 1 f-j). The positive and negative rails are joined on each

    side, but power must be provided to each side or the two sides must be connected with jumper

    wires.

    Figure 6: Breadboard Diagram

    OneBlinkingLED

    Setup the circuit that is shown in Figure 7, paying careful attention to the polarity of the LED legs.

    Open the OneBlinkingLED sketch. Check to make sure the correct board and serial port are

    selected. Verify the sketch and upload it. If done correctly, your LED will turn on for one second,

    then off for one second.

  • 7/25/2019 Arduino Tutorial 1

    8/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    Figure 7: Circuit Setup for the First Two Sketches

    OneBlinkingLED Modifications

    Modify the sketch so that the LED is lit for 3 seconds and off for 1 second.

    OneBlinkingLEDNoDelayUse the same circuit from the previous exercise shown in Figure 7. Open the sketch named

    OneBlinkingLEDNoDelay. Verify the sketch and upload it. Confirm that this sketch has the same

    behavior as OneBlinkingLED.

    OneBlinkingLEDNoDelay Modifications

    Modify the sketch so that the LED is lit for 2 seconds and off for 0.5 seconds.

  • 7/25/2019 Arduino Tutorial 1

    9/9

    MAE 334: Mechanical and Aerospace Engineering Lab I University at Buffalo

    Department of Mechanical and Aerospace Engineering Spring 2015

    Copyright 2014 Phillip M. Cormier and Jason N. Armstrong

    TwoBlinkingLED

    Open the TwoBlinkingLED sketch. Modify the circuit shown in Figure 7 to include the additional

    green LED. Verify the sketch and upload it. Note that the pin to be used is already specified in

    the sketch. Verify the sketch and upload it. If done correctly, your LED will turn on for one

    second, then off for one second.

    TwoBlinkingLED Modifications

    Modify the sketch so that the green LED is lit while the red LED is off, and vice versa.

    PotControlBlinkingLED

    Setup the circuit that is shown in Figure 8. Open the PotControlBlinkingLED sketch. Verify the

    sketch and upload it. Adjust the potentiometer such that the LED is on/off for roughly 1 second.

    Figure 8: Circuit Setup for PotControlBlinking LED

    PotControlBlinkingLED Modifications

    Modify the sketch so that the LED is off for twice as long as it is on.