Schedule‎ > ‎

Lab 04: Coding Basics

Objectives

  • Start practicing C++ commands
  • Write your first original program!
  • Work with int variables

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are expected to work either by yourself.
  • You may NOT give a copy of your code to anyone.
  • You must breadboard the circuits yourself.
  • You may not look at another student's code.
  • You may get help from people if you get stuck, but only if they do not show or tell you the code to type.

^ top

Project A: Emergency Flasher

In this lab we modify the standard "blinky" program for the build-in LED to make an "emergency" flasher.

Specifications:

  1. Start with the Blink sketch

    File > Examples > Basics > Blink

  2. Save the sketch as emergency.
  3. In the loop() function, write code to turn the built-in LED on for .05 seconds, then off for .05 seconds
  4. Repeat this on-off sequence five times total.  You must use a for loop to achieve this.
  5. After the fifth off, extend the on and off time to one second.
  6. You should see your built-in LED blink on and off quickly 5 times,  then turn on for 1 second then turn off for 1 second.  Then repeat from the beginning. 
  7. Allow the process of five shorts and one long to continue repeating as part of the loop() function.
  8. Submit your emergency.ino file as part of this assignment.

Notes and Explanations

  • Remember that one second is 1000 milliseconds.

^ to

Project B: Alternating LEDs

In this project we alternate the blinking of two LEDs like a railroad crossing.

Railroad Crossing Signal

Source: Railroad Safety Kids Page

Specifications:

  1. Gather the parts shown in the parts listed below.
  2. Breadboard the circuit as shown in the Breadboard Layout diagram below.
  3. Based on what you learned about how to blink a single LED, spend some time to think about how you might blink two LEDs. Now think about how you might blink two LEDs so that one is off while the other is on.
  4. Plug the USB cable into the Arduino.
  5. Start with a new sketch and save the sketch as railroad.
  6. Write code to alternate the blinking of the two LEDs such that the LEDs connected to pins 12 and 11 blink in an alternating pattern at 1/2 second on and 1/2 second off.
  7. 2 pts of this assignment:   you use one or more if-else statements to complete this assignment.  You can do it without if-else statements but you will lose 2 pts.
  8. Submit your railroad.ino file as part of this assignment.

Parts

  • Arduino board
  • USB cable
  • Solderless breadboard
  • 2x red LEDs
  • 2x resistors, in the range of 220-600 Ohms
  • Jumper Wires

Breadboard Layout

Two LED circuit

^ top

Extra Credit

The following are worth extra credit points:

  1. Modify the Alternating LEDs project as follows: (2 points)
    1. Start with the railroad sketch and save the modified code as railroadx2.
    2. Use one or more if-statements and a single variable to control the flashing of the LEDs
    3. Use only one delay() statement for both the 1/2 second on and 1/2 second off timing.
    4. Submit your railroadx2.ino file, in addition to railroad.ino, as part of this assignment.

Make certain that your README.txt file describes any extra credit attempted.

^ top

Grading Criteria (20)

Grading:  Part 1 10 points  (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt,  Works as Specified 6 pts)
                Part 2 10 points  (Header 1pt, Compiles 2 pts, Proper Formatting 1 pt, Works as Specified 6 pts)
                Extra Credit:  2 pts

Submit your files to Canvas

^ top

How to Submit

Upload all project files to Canvas in the project folder that matches the name of this project. Include the following items for grading:

  1. emergency.ino
  2. railroad.ino
  3. Optionally, railroadx2.ino (extra credit)

 If you make a mistake, you can resubmit up to the deadline, but must resubmit all your assignment files at the same time.

COMPLETED CODE FOR EMERGENCY.INO

int i = 0;
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {

  if (i < 5) {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(50);                       // wait for a .05 s
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(50);  // wait for a .05 s
    i = i + 1;
  }else{
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a .05 s
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);  // wait for a .05 s
    i = 0;
  }
}

COMPLETED CODE FOR RAILROAD EXTRA CREDIT:
#include <ArduinoSTL.h>
using namespace std;
int firstLight = 11;
int secondLight = 12;
int whichState = 0;  //4 states  0 = firstLight, turn it On state
//          1 = firstLight, turn it Off state
//          2 = secondLight, turn it On state
//          3 = secondLight, turn it Off state

void setup() {
  Serial.begin(9600);
  pinMode(firstLight, OUTPUT);
  pinMode(secondLight, OUTPUT);
}
void loop() {
  if (whichState == 0)  {
    digitalWrite(firstLight, HIGH);
    whichState = 1;
  }  else  if (whichState == 1)  {
    digitalWrite(firstLight, LOW);
    whichState = 2;
  }  else if (whichState == 2)  {
    digitalWrite(secondLight, HIGH);
    whichState = 3;
  }  else if (whichState == 3)  {
    digitalWrite(secondLight, LOW);
    whichState = 0;
  }
  delay(500);
}


^ top