If you have reached this section we will assume that you have successfully completed the Guide to Setting Up your Computer so that the Device Drivers and the Arduino IDE are all in good functioning order. This only has to be done once, but it has to be done properly. If you have not, please do this now.

As we go through this tutorial, we are assuming that all your technical setup problems have been taken care of. We assume that your Arduino Kit is in good working order and communicating with your PC properly. We will also assume that you have the Arduino IDE in good working order and it is open on your screen.

An archived file of all our examples for this unit is available HERE. We urge you to read this guide to the end and attempt to complete the challenge at the end of this guide. This challenge is designed to be quite a bit harder than the examples. This will force you to learn to resolve a situation that is new to you. Remember, the collective knowledge of humanity is available on the internet. Research what needs to be done and change that information for your own needs. Improvise, Adapt, Overcome.

The ARXD19 is a very basic device. It is based on an Arduino Uno board that has three switches (INPUTS) and six LEDs (OUTPUTS). Every ARXD19 is wired the exact same way internally:

See those numbers on the side of the Arduino connectors? Those are the pin numbers. Each pin can be configured to READ something or to DO something. The pins connected to the switches (S1,S2,S3) will be reading the position of the switch. The pins connected to the LEDs (L1 through L6) will be turning the LEDs on and off.

The human body is built in much the same way. Every one of our fingers (just like the pins on the Arduino) has the ability to sense things (heat, texture) but also has the ability to exert forces (push piano keys, flick a bug). Some fingers are more dexterous than others meaning that they can do special things – such as our opposable thumb. In much the same way, some Arduino pins have the ability to perform special functions. For now, let’s stick to the simple operations.

While working, it is customary to create and Input-Output Table to keep on hand. This will allow the programmer to have on hand a summary of the resources that must be controlled. Such a sheet was already designed for this project. You can download and print it from HERE. We encourage to use this sheet as a template for future projects.

Designator Description Arduino Pin Configuration
S1 Switch #1 A5 INPUT
S2 Switch #2 A4 INPUT
S3 Switch #3 A3 INPUT
L1 Red LED 3 OUTPUT
L2 Yellow LED 5 OUTPUT
L3 Green LED 6 OUTPUT
L4 Red LED 9 OUTPUT
L5 Yellow LED 10 OUTPUT
L6 Green LED 11 OUTPUT

 

Further we encourage you to keep a neat notebook with your personal notes as you are working. Label and date the entries in the notebook so you can later figure our what you were working on. Use pencil so you can erase things. The more organized you are, the easier it will be to learn. Here’s an example for a simple entry for the notebook:

Understanding the environment

The Arduino IDE has a few commonly used buttons and areas.

  • The Checkmark button (outlined is red) is the Verify button. It checks that your code does not contain any errors.
  • The Arrow Right button (outlined in yellow) is the Verify&Upload button. It will check the code and send it to the Arduino Kit.
  • The black area at the bottom is used to inform you where the errors is, if one was found.
  • Finally, the middle part (white) is where you will be typing your program.
  • Don’t forget to save often (Arrow Down).
Understanding Code Structure
  • Each program has two main components: SETUP and LOOP.
  • The SETUP area is an area that will be executed only once, at the beginning of the program (eg. when something is powered down and back up). This is where we define how each pin shall initially behave.
  • The LOOP area is the actual working area. After executing the SETUP, the “brain” will remain in this loop area executing the instructions in there over and over again. The only way to stop it is to unplug or re-program the device.
Example #1 – Blinking L1 (Red LED #1)

Let’s take the simple code for this example and analyze it as the Arduino Kit would do.

void setup()
 {
   pinMode(3, OUTPUT);
 }

 void loop()
 {
   digitalWrite(3,HIGH);
   delay(500);
   digitalWrite(3,LOW);
   delay(500);
 }
  • First we analyze the SETUP area:
    • pinMode(3,OUTPUT) indicates to the Arduino that it will be using Pin #3 as an output.
    • Once this program zone is executed, it will not be re-visited again.
  • Analyzing the LOOP area.
    • digitalWrite(3,HIGH) indicates to the Arduino that we want a positive voltage on PIN #3. This will turn on LED L1 because it is wired that way.
    • delay(500) indicates to the Arduino that we wish to wait 500ms (0.5s) before we continue executing the program.
    • digitalWrite(3,LOW) indicates to the Arduino that we do not want a positive voltage on PIN #3. This will turn off LED L1.
    • delay(500) indicates to the Arduino that we wish to wait 500ms (0.5s) before we continue executing the program.
    • Once the end of the loop is reached, it will go back to the top of the loop and re-execute the first instruction which is digitalWrite(3,HIGH)
  • So what does the program do?
    • Instructs the brain that Pin #3 will be an OUTPUT.
    • Repeats the following instructions, in order, for as long as it powered:
      • Turn on LED L1
      • Wait 0.5s.
      • Turn off LED L1
      • Wait 0.5s
    • The result of this program is LED blinking once a second. It is lit for half of a second and it is dark for the next half of a second.
  • Copy replace the program in your Arduino IDE with the one above.
    • Try Verifying it.
    • Try Uploading it.
    • Is the the Red LED L1 blinking?
Example #1 – Blinking L1 – Part 2

While it’s unusual for an example to have a “Part 2” we wish to illustrate an important concept in the “C” programming language. This concept is the “#define” instruction in C. This instruction allows us to access resources much easier, by renaming them into what we actually want. Allow us to illustrate:

#define L1 3
 void setup()
 {
   pinMode(L1, OUTPUT);
 }
 void loop()
 {
   digitalWrite(L1,HIGH);
   delay(500);
   digitalWrite(L1,LOW);
   delay(500);
 }

The example performs the same action as before. The difference is that we have “defined” the constant “L1” to mean the number 3 on the very first line. This way we can just use “L1” every time we try to turn on or off LED L1. Much easier to remember than trying to remember what the pin number is. Notice how we not used “L1” in all the places where the number 3 used to be?

This type of definition has an added benefit. If, hypothetically, we decide to move LED L1 to Arduino Pin #4, we would only have to change the first line into “#define L1 4”. The rest of the program would use the alias “L1” so they would all mean the number four now. If the program is very long or complex, this could mean the difference between one change and hundreds of changes. Remember – work smart, not hard.

Example #2 – One Switch to All Lights

This example attempts to illustrate that one INPUT can be linked to MULTIPLE OUTPUTS. We attempt to control all the LEDs with just Switch #1 (S1).

#define L1 3
#define L2 5
#define L3 6
#define L4 9
#define L5 10
#define L6 11

#define S1 A5
#define S2 A4
#define S3 A3

 void setup()
 {
   pinMode(L1, OUTPUT);
   pinMode(L2, OUTPUT);
   pinMode(L3, OUTPUT);
   pinMode(L4, OUTPUT);
   pinMode(L5, OUTPUT);
   pinMode(L6, OUTPUT);
   
   pinMode(S1, INPUT);
   pinMode(S2, INPUT);
   pinMode(S3, INPUT);
 }

 void loop()
 {

 if (digitalRead(S1)==HIGH) {
     digitalWrite(L1,HIGH);
     digitalWrite(L2,HIGH);
     digitalWrite(L3,HIGH);
     digitalWrite(L4,HIGH);
     digitalWrite(L5,HIGH);
     digitalWrite(L6,HIGH);  
   }
   else {
     digitalWrite(L1,LOW);
     digitalWrite(L2,LOW);    
     digitalWrite(L3,LOW);
     digitalWrite(L4,LOW);    
     digitalWrite(L5,LOW);
     digitalWrite(L6,LOW);    
   }
 }
  • First, we #define each Input and Output name to correspond to the actual pin on the Arduino.
  • Second, we define what each one of these pins does – using the new alias we defined it as. This way if something changes pins, we don’t have to go looking for it pas the first few lines of code.
  • Finally, we run the loop that simply instructs as follows:
    • If Switch S1 voltage is HIGH (present) – turn all the lights on.
    • If Switch S1 voltage is LOW (missing) – turn all the lights off.
  • Try uploading this code to your own ARXD19 and see how it works.
Example #3 – Single Traffic Light with Toggled Advanced Signalling

Let’s pretend that L1, L2 and L3 are the three bulbs on a standard traffic light. The traffic light will provide an advanced green if Switch #1 is ON, otherwise it will not. The switch from one mode to another will only take place after the cycle completes (when it goes back to red). This is the way actual traffic lights work – the difference being that there’s nobody throwing a switch. The signal for Advanced Green comes from a clock that is synchronized to high traffic volume. The timing of our traffic light will be faster than a regular one to illustrate this example in a timely manner.

Sometimes it is easier to resolve a problem if we attempt to resolve a problem by structuring it before we begin. By structuring we mean asking yourself the following question: What is the sequence of events that I need to produce in order to resolve this problem? Imagine getting a flat on your car. You know the sequence is as follows: Get spare tire and tools out of the trunk. Lift car on jack. Remove flat wheel. Install spare wheel. Put car down from jack. Put tools and flat tire in trunk. If one were to draw this process out, it would be called a flowchart. Let’s draw a flowchart for our problem:

Traffic Light with Toggled Advanced Green Flowchart

As we can see from above, the program stays in the loop so technically it can never “end”. From this flowchart we can try to design our program.

#define L1 3
#define L2 5
#define L3 6
#define L4 9
#define L5 10
#define L6 11

#define S1 A5
#define S2 A4
#define S3 A3

 uint8_t State;

void setup()
{
   pinMode(S1, INPUT);
   pinMode(S2, INPUT);
   pinMode(S3, INPUT);

   pinMode(L1, OUTPUT);
   pinMode(L2, OUTPUT);
   pinMode(L3, OUTPUT);
   pinMode(L4, OUTPUT);
   pinMode(L5, OUTPUT);
   pinMode(L6, OUTPUT);
 State = 0;
}

 void loop()
{
 uint8_t i;

 switch (State) {
     case 0: digitalWrite(L1, HIGH);
             digitalWrite(L2, LOW);
             digitalWrite(L3, LOW);
             delay(5000);
             if (digitalRead(S1)) {
               State=1;
             }
             else {
               State=2;
             }
             break;
     case 1: digitalWrite(L1, LOW);
             digitalWrite(L2, LOW);
             digitalWrite(L3, LOW);
             for (i=0;i<25;i++) {
               digitalWrite(L3,!digitalRead(L3));
               delay(100);
             }          
             State=2;
             break;
     case 2: digitalWrite(L1, LOW);
             digitalWrite(L2, LOW);
             digitalWrite(L3, HIGH);
             delay(5000);
             State=3;
             break;
     case 3: digitalWrite(L1, LOW);
             digitalWrite(L2, HIGH);
             digitalWrite(L3, LOW);
             delay(1500);
             State=0;
             break;
     default: State=0; 
             break;
   }
}
Programming Challenge

Consider the intersection below. The traffic lights are marked as to correspond to the two columns of lights on your ARXD19 device. You can also download this challenge HERE if you want to print it out. This challenge is supposed to be hard. We expect you will have to do some research before completing this.

General Specifications:

  • Red should be on for 5 seconds every cycle.
  • Yellow should be on for 1.5 seconds every cycle, but is only used when going from green to red.
  • Advanced Green, if called for, shall be on for 2.5 seconds.
  • Solid Green should be on for 5 seconds every cycle. This is in addition to the 2.5s if Advanced Green if required.
  • Any changes in configuration have to be done in such a way as to not expose drivers to potential hazards.
  • It is assumed in this assignment that all drivers obey the traffic signals all the time.
  • We recommend saving each assignment in a different file (“Sketch”) so that you can revisit them later or ask someone for help.

Assignment #1 – Program the ARXD19 in such a way that it will run the traffic lights normally, without any advanced green signalling.

Assignment #2 – Scott Street is a major street and will require Advanced Green on every cycle. David Street does not require Advanced Green. Program the traffic light accordingly.

Assignment #3 – Both streets now require Advanced Green on every cycle. Program the traffic lights accordingly.

Assignment #4 – The Advanced Green light for Scott Street is controlled by Switch #1. The Advanced Geen light for David Street is controlled by Switch #2. Program the traffic lights accordingly so that they operate safely.