• BME 200
  • Final Project - Part I - Due Nov 13

    Getting Started

    Installing the IDE

    The program you use to write programs for the Arduino is called an Integrated Development Environment (IDE). You can read the instructions for installing the appropriate IDE for your computer here:

    Windows (https://www.arduino.cc/en/Guide/Windows)

    Mac OS X (https://www.arduino.cc/en/Guide/MacOSX)

    Getting your computer to talk to the Arduino

    Plug the USB cable into your computer and into the Arduino.

    If you have not already, run the Arduino IDE. At the top of the screen you should see Tools in the toolbar. Click on Tools -> Port -> (Arduino/Genuino Uno). This action will tell the IDE which port to use to talk to your Arduino. You should only need to do this once during each work session with your Arduino. If you get an error when trying to upload a program, be sure to check that the port is set correctly.

    Programming the Arduino

    Writing a program for the Arduino is similar to writing a program for any other computer. The language you use for Adruino is loosely based on C and looks similar to Matlab code. Like all programming languages, the Arduino language has a built-in list of keywords. You can find the complete list here.

    If you look through the list you will see familiar keywords like if...else and for. The skills you have used to program in other languages are the same skills you will use here. So if you know how loops work in another language, you already know how they will work in the Arduino language.

    Once you write a program (more on that below), you should verify your program before uploading it to the arduino. You verify your program by clicking on the checkmark icon at the top of the program window. When you click verify, the IDE will read through your code and check for syntax errors (misplaced semicolons, or typos). The IDE will not check for or catch logical errors, so it is entirely possible that you will write a program that verifies correctly but that does not do what you want.

    After you verify the program you will need to upload it to the Arduino. Click on the right arrow at the top of the code window. This step transfers the program from your computer to the Arduino. Once the program is on the Arduino, it will start to run automatically and run until you stop it. You can stop your program by unplugging the USB cable.

    Program structure in Arduino

    The Arduino is a little different than other computer platforms because it will continuosly run a program that you upload until you either unplug the Arduino or otherwise stop it. All Arduino programs have the following structure:

    void setup(){
    
    }
    
    void loop(){
    
    }
    

    Notice that there are two sections, one called setup() and the other called loop(). Also note that there are a pair of curly brackets {} following each of these names. Anything that is inside these curly brackets belongs to that section. The proper term for setup() and loop() is function.

    Things that go in the setup() function are statements that set the initial conditions for your program. So you might set some constants or declare some variables here or load other programs into memory. This section is run only once, right when the Arduino is powered up, or right after you upload a new program.

    Code that is located in the loop() function is executed over and over until you power off the Arduino.

    Writing your first program

    Your first program will print some numbers. Since the Arduino does not have a screen, you view the output on a screen on your computer called Serial Monitor. You get to the Serial Monitor window by clicking on Tools -> Serial Monitor in the toolbar. Make sure you set the baud rate on the Serial Monitor to the same value that you specified in your program.

    In words, this is what the program will do. First, it will create a variable called i. The variable i will start with a value of 0. The program will print the value of i to the screen. Then, the program will add one to i. If the value of i<3 then the loop will start over. The new value of i will be printed and then incremented again. This sequence of events will continue until i=3 at which point i<3 is now false. Since there is no more code further down the screen, the whole loop() will start over.

    Here is the program. Open a new window in your Arduino IDE (File->New) then copy and paste the code below into your window.

    //  Simple program to print some numbers to the screen
    //
    //  Use Tools->Serial Monitor to bring up the screen
    //  so you can see the numbers
    
    
    // The code in the setup() function sets the Arduino up.
    // It is executed only once.
    void setup() {
      // You always have to tell the Arduino how fast it can
      // talk to your computer if you plan on transferring data
      // from the Arduino to your computer or vice versa. The number
      // you put in the parenthesis tells the Arduino how fast it can
      // transfer data (this is called the baud rate).
      // You can choose these numbers for speed: 300, 600, 1200, 2400,
      // 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200.
    
      Serial.begin(9600);
    }
    
    // The code in the loop() function is executed over and
    // over until the Arduino is turned off.
    void loop() {
    
      // A for loop lets you execute the same steps multiple times.
      // Instead of writing the same command 10 times, you can use
      // a for loop and make it loop 10 times. This saves space and
      // makes the code more readable.
      //
      // In the loop below, we create a variable called "i" that
      // starts at 0. Then we print the value of i. Then the value
      // of i is incremented by one (i++ is a shorthand way of
      // writing i=i+1). This loop will continue running until the
      // value of i is equal to 3. At this point the whole loop will
      // start over.
      
      for(int i=0;i<3;i++){
    
        // For each value of i we want to print it to the screen.
        // We use the function println() for that.
    
        Serial.println(i);
    
        // We might want to put a delay between each print. Use the
        // command delay(x) to delay your program x ms between steps.
    
        delay(250);
      }
    
      while(1);
    }
    

    Your second program

    This second program shows how to make a small light on the Arduino turn on and off. The logic of the program is as follows.

    In setup() tell the Arduino what pin you will be talking to. Pin 13 has a small LED attached to it. When you send that pin a LOW voltage (0V) the LED will turn off. When you send that pin a HIGH voltage (5V) the LED will turn on. In setup use the function pinMode(13, OUTPUT) to tell the Arduino that you will be writing data to pin number 13.

    In the main loop() function turn the led on, wait 1 second, turn it off, wait 1 second, and so on. The command you use to turn the LED on is digitalWrite(13,HIGH). In English this means write a HIGH signal to pin number 13. Arduino assumes HIGH means 5V or ON and that LOW means 0V or OFF. So to turn the LED off, you use digitalWrite(13,LOW).

    // The setup function runs once when you press reset or 
    // reset the power power the board
    void setup() {
      
      // We need to tell the Arduino that we want to turn the
      // built-in LED on the board on and off. The LED is
      // controlled by pin number 13. We set the mode to OUTPUT
      // which means that the Arduino should expect us to send
      // values to that pin (as opposed to the Arduino sending
      // values from that pin to us as INPUT).
      pinMode(13, OUTPUT);
    }
    
    // the loop function runs over and over 
    void loop() {
    
      // pin 13 is what is called a digital pin, so we use 
      // the function digitalWrite to talk to it.
    
      // this will turn the pin on
      digitalWrite(13, HIGH);   
    
      // this will wait 1000 ms
      delay(1000);             
    
      // this will turn the led off
      digitalWrite(13, LOW);
      
      delay(1000);
    }
    




    Last updated:
    November 5, 2018