Version 6

    element14's The Ben Heck Show

    Join Karen as she shares her enthusiasm for teaching STEM subjects, gives you what you need to know to get started on electronics projects, and more.

    Back to The Ben Heck Show homepage

    The Learning Circuit
    Featured Bonus Content
    See All Episodes

     

    Karen walks you through the Spaceship Interface project which is included in the Arduino Starter Kit book.  For this project you’ll need an Arduino Uno the USB cable to plug into your computer, breadboard, jumpers, a tact switch, two red and one LEDs, three 220 ohm resistors for the LEDs, and one 10 kiloohm resistor. The code you’ll need to do this project is included in the Arduino Starter KitArduino Starter Kit book.

     

    The Arduino Starter Kit Arduino Starter Kit book includes circuit diagrams and code which is referred to as sketches.  A sketch includes a set of functions followed by curly brackets, such as void setup () and void loop (). Anything you put in the curly brackets is the code that is executed when the function is called. Sometimes you will need to create what are known as variables for your code. A variable are items that you want your code to remember so that you can reference them later.  One good thing about variables is that if you use the same variable throughout your code and need to adjust the value of the variable, you only need to set the variable to another value at the top of the code.  Karen shows you how to set an integer as a variable for the project.

     

    After defining the variable, Karen moves along to the setup code. The setup is where we configure the pins so that the Arduino knows what’s an input, what’s an output, and which pins we’re using. In her code the 3 LED are designated as outputs, for pinMode, and the button is designated as an input for pinMode.

     

    int switchState = 0; // Holds the state of the Switch (HIGH - Pressed, LOW - Floating)
    void setup(){   // The void setup function runs only once at the beginning. It is the place to define the OUTPUTs and INPUTs
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT); 
    // configures pin 3 (digital) as an OUTPUT
      pinMode(5, OUTPUT);
      pinMode(2, INPUT);
    }

     

    When the code runs it will run once at the beginning to initialize and the loop will run over and over again.  The reason for the loop is to allow the Arduino to keep sensing the inputs and outputs.  Karen enters  the remaining code for the loop and walks you through what it means.

     

    int switchState = 0; // Holds the state of the Switch (HIGH - Pressed, LOW - Floating)
    void setup(){   // The void setup function runs only once at the beginning. It is the place to define the OUTPUTs and INPUTs
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT); 
    // configures pin 3 (digital) as an OUTPUT
      pinMode(5, OUTPUT);
      pinMode(2, INPUT);
    }
    
    void loop() { // This function will execute endlessly from the first to the last row
      switchState=digitalRead(2); // reads the digital Value of pin 2
      if (switchState == LOW) { 
    // compares if the switchState corresponds to a LOW value. If the statement is true, it executes the orders contained within {}. Otherwise it ignores them. 
        digitalWrite(3, HIGH); // Sets the signal in 3 to HIGH (turn on the LED) and 4 and 5 to LOW (turn off the LEDs connected to 4 and 5)
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
      }
      else { // If the Switch is pressed, the program will ignore the 'if' conditional and enter the 'else'
        digitalWrite(3, LOW);
        digitalWrite(4, LOW); 
        digitalWrite(5, HIGH);
        delay(250); // Delays time for 250ms - a quarter of a second. It freezes the program during this time.
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        delay(250);
      }
    }