MESA National Challenge

SparkFun is proud to announce a great educational partnership with Mathematics, Engineering, Science, Achievement (MESA) around the MESA National Challenge. SparkFun is the hardware and professional development partner for MESA programs participating in the Prosthetic arm challenge this year. We are honored to be a part of the challenge this year and to be supporting the amazing projects coming from hundreds of students across 10 states.

MESA assists students at middle and senior high schools (and some elementary schools) so they excel in math and science and become competitively eligible for the most rigorous colleges and universities. MESA partners with teachers, administrators, school district officials and industry representatives to provide this academic enrichment model. Students are selected to participate in the MESA through a process that involves teachers at participating schools and locally-based MESA personnel.

National Challenge Overview

Each year MESA mosts a national design challenge to put students' knowledge, creativity and collaboration skills to the test. For the past couple of years this challenge has been focussed the design and construction of a functional prosthetic arm using cheap and easy to find materials. This year there is an extra layer of challenge. Students will be required to incorporate electronics and programming into their arm project using Arduino and a custom kit designed by SparkFun!

For more on the National Design Competition you can visit the MESA National competition page. If you are looking for a MESA program near you check out the state by state contact page for the nearest MESA program near you.

Handy Links


Purchase a MESA National Challenge Kit from SparkFun

SparkFun Tinker Kit

KIT-13930
Retired

Helpful Materials to Have Around

Designing and building a prosthetic arm that runs off an Arduino is quite a challenge! We have created a list of spare parts, tools and other materials to add functionality to your project. For one, we included an assortment of Hook-Up wire so that you can run any length of wire to your motors that you wish.

Hook-Up Wire - Assortment (Solid Core, 22 AWG)

PRT-11367
$21.50

Servo - Generic High Torque (Standard Size)

ROB-11965
$13.95

Digital Multimeter - Basic

TOL-12966
$16.50

Arduino and Breadboard Holder

DEV-11235
$4.95

Hobby Gearmotor - 65 RPM (Right Angle, Pair)

ROB-13258
$5.50

H-Bridge Motor Driver 1A

COM-00315
$2.50

Tool Kit - Beginner

TOL-13086
Retired

SparkFun Inventor's Kit Parts Refill Pack - v3.3

LAB-13110
Retired

Curriculum Materials

Through years of teaching Arduino to students, educators and the public we have come up with the idea of the "Big 6". The Big 6 are the 6 major concepts that once you tackle in Arduino you can pretty much do anything you want. Below are the Big 6 and this page and the workshops associated with them are modeled around these concepts and getting you up and running with them as quickly as possible.

The Big 6

  • Program Flow and Control
  • Inputs and Outputs
  • Data, Math and Numbers
  • Digital Vs. Analog
  • Communication
  • Libraries and Object Oriented Programming (OOP)

  • These concepts are cove throughout the circuits below. Each circuit tab has a description, wiring diagram and example code for you to reference during class or afterwards while you are extending your knowledge after we leave.

    For continued support, frequently asked questions and to share with other MESA instructors please join our Google+ Community Group.



    This section is to get everything installed and ready to go. This is also a great indepth introduction to the RedBoard and all of its parts, functions and how to upload code to it to do you bidding. You can also find other materials to help you install ArduBlock, the block based programming tool for Arduino as well as a few instructional materials to get you up and running.

    Board Setup and Software Installation

    RedBoard Hookup Guide

    January 7, 2014
    How to get your RedBoard up-and-blinking!

    Instructional Materials

    Giant Breadboard Poster

    May 27, 2014

    Looking to teach how breadboards work? Print this file out to a poster, laminate it, and use dry erase markers to illustrate the internal connections.

    Arduino Cheatsheet

    August 19, 2014

    Updated - 8/7/2017 -- This is an overview of the most important / commonly used Arduino commands. [PDF] [DOCX]

    Collect Your Parts

    Breadboard - Self-Adhesive (White)

    PRT-12002
    $5.50

    LED - Assorted (20 pack)

    COM-12062
    $3.95

    Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

    PRT-11026
    $2.45

    SparkFun RedBoard - Programmed with Arduino

    DEV-12757
    Retired

    Bare Minimum


    Blink

    LEDs are small, powerful lights that are used in many different applications. To start off, we will work on blinking an LED, the Hello World of microcontrollers. That’s right - it’s as simple as turning a light on and off. It might not seem like much, but establishing this important baseline will give you a solid foundation


    Build It!


    Program It!

    view raw blink.ino hosted with ❤ by GitHub


    Railroad Crossing

    Now that you’ve gotten your LED to blink on and off, it’s time to up the stakes a little bit – by connecting two LEDs at once. We’ll also give you a first go around with a breadboard. This circuit is a great setup to start practicing writing your own programs and getting a feel for the way Arduino works.


    Build It!


    Program It!

    void setup()
    {
    //REMEMBER THAT EVERYTIME YOU ADD HARDWARE
    //YOU NEED TO ADD A pinMode FOR THAT PIN!!!
    //set pin 13 as an output
    pinMode(13,OUTPUT);
    //set pin 12 as an output
    pinMode(12,OUTPUT);
    }
    void loop()
    {
    //turn pin 13 on and 12 off
    digitalWrite(13,HIGH);
    digitalWrite(12,LOW);
    //wait for 150 ms.
    delay(150);
    //turn pin 13 off and 12 on
    digitalWrite(13,LOW);
    digitalWrite(12,HIGH);
    //wait for 150 ms.
    delay(150);
    //loop back to the top
    }
    view raw RR_Crossing.ino hosted with ❤ by GitHub


    Landing Lights

    Now that you’re used to the breadboard, it’s time to up the stakes even more – by connecting four or more LEDs at once! We’ll also give your RedBoard or Arduino R3 a little test by creating various lighting sequences.


    Build It!


    Program It!

    void setup()
    {
    //add pins 13,12,11,10 as outputs
    pinMode(10,OUTPUT);
    pinMode(11,OUTPUT);
    pinMode(12,OUTPUT);
    pinMode(13,OUTPUT);
    }
    void loop()
    {
    //turn 13 on
    digitalWrite(10,HIGH);
    //wait 500 ms
    delay(500);
    //turn 12 on
    digitalWrite(11,HIGH);
    //wait 500 ms
    delay(500);
    //turn 11 on
    digitalWrite(12,HIGH);
    //wait 500 ms
    delay(500);
    //turn 10 on
    digitalWrite(13,HIGH);
    //wait 500 ms
    delay(500);
    //turn all pins off
    digitalWrite(10,LOW);
    digitalWrite(11,LOW);
    digitalWrite(12,LOW);
    digitalWrite(13,LOW);
    //wait 100 ms
    delay(100);
    //loop back to top
    }
    view raw LandingLights.ino hosted with ❤ by GitHub

    Collect Your Parts!

    Trimpot 10K Ohm with Knob

    COM-09806
    $1.05

    Breadboard - Self-Adhesive (White)

    PRT-12002
    $5.50

    LED - Assorted (20 pack)

    COM-12062
    $3.95

    Mini Photocell

    SEN-09088
    $1.60

    Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

    PRT-11026
    $2.45

    Resistor 10K Ohm 1/6th Watt PTH - 20 pack

    COM-11508
    $0.95

    SparkFun RedBoard - Programmed with Arduino

    DEV-12757
    Retired

    AnalogRead Print

    A potentiometer is also known as a variable resistor. When powered with 5V, the middle pin outputs a voltage between 0V and 5V, depending on the position of the knob on the potentiometer. A potentiometer is a perfect demonstration of a variable voltage divider circuit. The voltage is divided proportionate to the resistance between the middle pin and the ground pin. In this circuit you will simply take that reading of voltage and print it out over your serial port so you can view it.


    Build It!


    Program It!

    void setup()
    {
    //start the serial library at 9600bits per second
    //serial allows you send sensor values and other
    //data back to your computer from the Arduino.
    //9600 is the standard speed most devices use.
    Serial.begin(9600);
    }
    void loop()
    {
    //create an integer variable (int) called val
    //make val equal to the sensor reading on pin A0
    //you should get a sensor reading between 0 and 1023
    int val = analogRead(A0);
    //print the string of "val = " over the serial port
    Serial.print("Val = ");
    //print the variable val and end the line with a carriage return
    Serial.println(val);
    //wait a bit
    delay(100);
    //loop back to the top
    //upload this code and open your serial port (magnifying glass button
    //in the upper right corner of the Arduino IDE).
    }
    view raw analogRead_Print.ino hosted with ❤ by GitHub


    Simple Night Light

    Earlier, you got to use a potentiometer, which varies resistance based on the twisting of a knob. In this circuit, you’ll be using a photoresistor, which changes resistance based on how much light the sensor receives. Since the RedBoard can’t directly interpret resistance (rather, it reads voltage), we need to use a voltage divider to use our photoresistor. This voltage divider will output a high voltage when it is getting a lot of light and a low voltage when little or no light is present.


    Build It!


    Program It!

    //create a global integer variable called brightness
    //initialize it as 500, you will change it to your
    //highest value you get from your light sensor and
    //reupload the code
    int brightness= 500;
    void setup()
    {
    //set pin 13 as an output
    pinMode(13,OUTPUT);
    //setart serial communciation at 9600bps
    Serial.begin(9600);
    }
    void loop()
    {
    //create a local variable called light and set it to
    //the light sensor value on pin A0.
    int light = analogRead(A0);
    //print the light variable over the serial port
    Serial.println(light);
    //if light is greater than your brightness level
    //then turn pin 13 off, else (if its lower than brightness)
    //turn 13 on.
    if(light > brightness)
    {
    digitalWrite(13,LOW);
    }
    else
    {
    digitalWrite(13,HIGH);
    }
    //upload this code and open the serial port. read the highest value
    //subtract about 10 from that digit and replace 500 to whatever
    //that digit is as your global brightness value
    }
    view raw simpleNightLight.ino hosted with ❤ by GitHub


    Calibrated Night Light

    Your night light works pretty well, but what if you want to move it to a different location and not calibrate it manually? You can do that in setup and using a calibration variable to compare your current reading against. This works for nighlights, but also any other sensor that you want to have a baseline to start with.


    Build It!


    Program It!

    //create a global integer variable called brightness
    //dont initialize it yet, you will do that in setup
    int brightness;
    void setup()
    {
    //set pin 13 as an output
    pinMode(13,OUTPUT);
    //start serial communciation at 9600bps
    Serial.begin(9600);
    //take a single reading and set that as brightness
    //this calibrates brightness automatically
    brightness = analogRead(A0);
    }
    void loop()
    {
    //create a local variable called light and set it to
    //the light sensor value on pin A0.
    int light = analogRead(A0);
    //print the light variable over the serial port
    Serial.println(light);
    //if light is greater than your brightness level
    //then turn pin 13 off, else (if its lower than brightness)
    //turn 13 on.
    if(light > brightness)
    {
    digitalWrite(13,LOW);
    }
    else
    {
    digitalWrite(13,HIGH);
    }
    //This code automatically calibrates brightness in setup()
    //you do not have to open the serial port at all!
    }

    Collect Your Parts

    Breadboard - Self-Adhesive (White)

    PRT-12002
    $5.50

    Hobby Motor - Gear

    ROB-11696
    $2.10

    LED - Assorted (20 pack)

    COM-12062
    $3.95

    Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

    PRT-11026
    $2.45

    Resistor 330 Ohm 1/6 Watt PTH - 20 pack

    COM-11507
    Retired

    SparkFun RedBoard - Programmed with Arduino

    DEV-12757
    Retired

    Transistor - NPN (P2N2222A)

    COM-12852
    Retired

    Simple AnalogWrite

    description


    Build It!


    Program It!

    //create a global variable for brightness and
    //set it to 2
    int brightness = 2;
    void setup()
    {
    //set pin 9 to output, any PWM(~) pin will work
    //with analogWrite()
    pinMode(9,OUTPUT);
    }
    void loop()
    {
    //analogWrite the value of brightness to pin 9
    //change the variable of brightness to any number between
    //0 and 255 which is the full range of analogWrite()
    //you will only see a brightness change in values 0-100!
    analogWrite(9,brightness);
    }


    Dimmer

    A potentiometer is a perfect demonstration of a variable voltage divider circuit as shown earlier. The voltage is divided proportionate to the resistance between the middle pin and the ground pin. In this circuit, you’ll learn how to use a potentiometer to control a value stored as a variable and then used to control the brightness of an LED.


    Build It!


    Program It!

    void setup()
    {
    //set pin 9 as an output
    pinMode(9,OUTPUT);
    //start serial monitor at 9600bps for debugging
    Serial.begin(9600);
    }
    void loop()
    {
    //create a local variable called val and set it to
    //the sensor value on pin A0
    int val = analogRead(A0);
    //print val for debugging and viewing
    Serial.println(val);
    //analogWrite val/4 to pin 9 to create a dimmer using
    //the potentiometer or any other analog sensor
    //for more sensitivity try dividing val by 10
    analogWrite(9,val/10);
    }
    view raw dimmer.ino hosted with ❤ by GitHub


    Fade with a Variable

    Variables...change! Hence they are called variables. We can change them in code by using math! I know...Math; you have got to be kidding me! We can use an equation to increment or decrement a variable based on its value. This sketch increments a variable until it reaches 255 and then changes directions and decrements back to zero and start over again. This is a great use of an if statement as well as how you can manipulate numbers in code.


    Build It!


    Program It!

    //create a global variable of brightness and set it to 0
    int brightness = 0;
    //create a global variable of grow to increment by and set it to 5
    int grow = 5;
    void setup()
    {
    //set pin 9 as an output, make sure to use a PWM(~) pin.
    pinMode(9,OUTPUT);
    }
    void loop()
    {
    //write the value of brightness to
    analogWrite(9,brightness);
    //increment brightness by the value of grow
    brightness = brightness + grow;
    //if brightness is greather than or equal to 255 OR
    //less than or equal to 0 (the full range of analogWrite)
    //multiple grow by -1 to change the direction of brightness
    if(brightness >= 255 || brightness <= 0)
    {
    grow = grow*-1;
    }
    //wait a bit
    delay(100);
    //loop back to the top
    }


    Fade with a For Loop

    Fade with a variable is great, but it can be simplified. Enter the for loop! With a a for loop you define a variable and its value, its minimum or maximum range and then what to increment/ decrement by and then do something in that loop that many times. If this seems confusing, it is at first. But, I think once you look at the code it will start to be clearer.


    Build It!


    Program It!

    void setup()
    {
    //set pin 9 as output
    pinMode(9,OUTPUT);
    }
    void loop()
    {
    //for loop that increments the variable x from 0 to 255
    for(int x =0; x <= 255; x++)
    {
    //write x to pin 9
    analogWrite(9,x);
    delay(5);
    }
    //for loop the decrements x from 255 to 0
    for(int x= 255; x>=0; x--)
    {
    //writes x to pin 9
    analogWrite(9,x);
    delay(5);
    }
    }
    view raw fadeWithFor.ino hosted with ❤ by GitHub


    RGB LED

    You know what’s even more fun than a blinking LED? Changing colors with one LED. RGB, or red-green-blue, LEDs have three different color-emitting diodes that can be combined to create all sorts of colors. In this circuit, you’ll learn how to use an RGB LED to create unique color combinations. Depending on how bright each diode is, nearly any color is possible!


    Build It!


    Program It!

    //create 3 global variables each one
    //will hold the color value between 0 and
    //255 for Red (r), Green (g) and Blue(b).
    int r = 0;
    int g = 0;
    ing b = 0;
    void setup()
    {
    //set pins 9,10 and 11 as outputs
    //pin 9 is connected to the red pin
    pinMode(9,OUTPUT);
    //pin 10 is connected to the green pin
    pinMode(6,OUTPUT);
    //pin 11 is connected to the blue pin
    pinMode(5,OUTPUT);
    }
    void loop()
    {
    //write the RGB color value to the three
    //RGB pins
    analogWrite(5,r);
    analogWrite(6,g);
    analogWrite(9,b);
    }
    view raw simpleRGB.ino hosted with ❤ by GitHub

    Collect Your Parts

    Breadboard - Self-Adhesive (White)

    PRT-12002
    $5.50

    Tactile Button Assortment

    COM-10302
    $6.50

    Hobby Gearmotor - 65 RPM (Right Angle, Pair)

    ROB-13258
    $5.50

    Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

    PRT-11026
    $2.45

    LED - RGB Clear Common Cathode

    COM-00105
    $2.25

    H-Bridge Motor Driver 1A

    COM-00315
    $2.50

    SparkFun RedBoard - Programmed with Arduino

    DEV-12757
    Retired

    Simple H-Bridge

    Now, we are going to tackle spinning a motor. This requires the use of an H-Bridge, which can switch a larger amount of current than the RedBoard cannot do. But, wait there is more! An H-Bridge can also be used to change the direction of the current that it switches (direction of the motor spinning) and the motor speed. Did anyone say "Robots"?


    Build It!


    Program It!

    view raw HBridgeBlink.ino hosted with ❤ by GitHub


    H-Bridge and Custom Motor Functions

    The H-Bridge is awesome! You can control 2 seperate motors using only a few pins. But, more pins usually means more coding, and every time you want to change the direction you have to write a couple lines of code. There is help with that: custom functions! You can create your own functions and define them only once to use later.


    Build It!


    Program It!

    int PWM = 6;
    int DIRA = 4;
    int DIRB = 5;
    void setup()
    {
    pinMode(PWM,OUTPUT);
    pinMode(DIRA,OUTPUT);
    pinMode(DIRB,OUTPUT);
    }
    void loop()
    {
    forward(255);
    delay(1000);
    brake();
    delay(1000);
    reverse(255);
    delay(1000);
    brake();
    delay(1000);
    }
    void forward(int Speed)
    {
    digitalWrite(DIRB,HIGH);
    digitalWrite(DIRA,LOW);
    analogWrite(PWM,Speed);
    }
    void reverse(int Speed)
    {
    digitalWrite(DIRB,LOW);
    digitalWrite(DIRA,HIGH);
    analogWrite(PWM,Speed);
    }
    void brake()
    {
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,LOW);
    }
    view raw HBridgeFunctions.ino hosted with ❤ by GitHub


    Buttons and Functions

    Earlier, we used an analog input to read the potentiometer. In this circuit, we’ll be reading in one of the most common and simple inputs – a push button – by using a digital input. The way a push button works with your RedBoard is that when the button is pushed, the voltage goes LOW. You RedBoard reads this and reacts accordingly.


    Build It!


    Program It!

    //create 3 global variables
    //PWM controls speed
    //DIR A and B are the direction logic pins
    int PWM = 3;
    int DIRA = 4;
    int DIRB = 5;
    void setup()
    {
    //pinModes for H-Bridge control pins
    pinMode(PWM,OUTPUT);
    pinMode(DIRA,OUTPUT);
    pinMode(DIRB,OUTPUT);
    //set pin 6 to INPUT_PULLUP, no need for a pullup resistor
    pinMode(6,INPUT_PULLUP);
    }
    void loop()
    {
    //create a local varaible thats stores the button state (1 or 0)
    int buttonState = digitalRead(6);
    //if buttonState is 1 (greater than 0) blink the motor forward and
    //backwards
    if(buttonState > 0)
    {
    forward(255);
    delay(1000);
    reverse(255);
    delay(1000);
    }
    //else brake or stop
    else
    {
    brake();
    }
    }
    //custom function for forward
    void forward(int Speed)
    {
    digitalWrite(DIRA,HIGH);
    digitalWrite(DIRB,LOW);
    analogWrite(PWM,Speed);
    }
    //custom function for reverse
    void reverse(int Speed)
    {
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,HIGH);
    analogWrite(PWM,Speed);
    }
    //custom function for brake (stop)
    void brake()
    {
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,LOW);
    }


    Latching Button with H-Bridge

    What? You don't want to constantly hold your button down for something to happen? Enter the idea of latching! Latching is the idea of pressing a button and it staying on until you press it again and the other way around. There are latching buttons out there, but we will look at how to accomplish latching in software rather than go find latching buttons.


    Build It!


    Program It!

    //create 3 global variables
    //PWM controls speed
    //DIR A and B are the direction logic pins
    int PWM = 3;
    int DIRA = 4;
    int DIRB = 5;
    //create a global boolean variable to toggle
    //when button is pressed, set it to true.
    boolean state = true;
    void setup()
    {
    //pinModes for H-Bridge control pins
    pinMode(PWM,OUTPUT);
    pinMode(DIRA,OUTPUT);
    pinMode(DIRB,OUTPUT);
    //set pin 6 to INPUT_PULLUP, no need for a pullup resistor
    pinMode(6,INPUT_PULLUP);
    }
    void loop()
    {
    //create a local varaible thats stores the button state (1 or 0)
    int buttonState = digitalRead(6);
    //if buttonState is 0 (less than 1) toggle the state variable to
    //the opposite of the current state (!state)
    if(buttonState < 1)
    {
    state = !state;
    delay(250);
    }
    //if state is false drive motor forward, else stop.
    if(state == false)
    {
    forward(255);
    }
    else
    {
    brake();
    }
    }
    //custom function for forward
    void forward(int Speed)
    {
    digitalWrite(DIRA,HIGH);
    digitalWrite(DIRB,LOW);
    analogWrite(PWM,Speed);
    }
    //custom function for reverse
    void reverse(int Speed)
    {
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,HIGH);
    analogWrite(PWM,Speed);
    }
    //custom function for brake (stop)
    void brake()
    {
    digitalWrite(DIRA,LOW);
    digitalWrite(DIRB,LOW);
    }
    view raw LatchingButton.ino hosted with ❤ by GitHub

    Collect Your Parts

    Breadboard - Self-Adhesive (White)

    PRT-12002
    $5.50

    Servo - Generic (Sub-Micro Size)

    ROB-09065
    $9.95

    Tactile Button Assortment

    COM-10302
    $6.50

    Jumper Wires Standard 7" M/M - 30 AWG (30 Pack)

    PRT-11026
    $2.45

    SparkFun RedBoard - Programmed with Arduino

    DEV-12757
    Retired

    Simple Servo Write

    Servos are ideal for embedded electronics applications because they do one thing very well that motors cannot – they can move to a position accurately. By varying the pulse width of the output voltage to a servo, you can move a servo to a specific position. For example, a pulse of 1.5 milliseconds will move the servo 90 degrees. In this circuit, you’ll learn how to use PWM (pulse width modulation) to control and rotate a servo.


    Build It!


    Program It!

    //include the Servo library in your sketch
    #include <Servo.h>
    //create a servo object called myServo
    Servo myServo;
    void setup()
    {
    //attach myServo to pin 9
    myServo.attach(9);
    //write the angle of 90 to myServo once
    myServo.write(90);
    }
    void loop()
    {
    //do nothing in the loop
    ;
    }
    view raw simpleServoWrite.ino hosted with ❤ by GitHub


    Servo "Blink"

    Earlier you wrote a single angle to a servo. Well, thats kinda boring. You can write multiple angles in your loop to animate a servo to control a robotic arm, a small door, or even the fuel gauge on a car. This sketch mashes up the good old blink sketch and a servo to get a servo to move back and forth.


    Build It!


    Program It!

    view raw ServoBlink.ino hosted with ❤ by GitHub


    Servo "Night Light"

    Do you remember your night light sketch. It turned on an LED, which is great. But, you want to have your chicken coupe door open in the morning when the sun comes up and close in the evening when the sun goes down. You can do this with a mash up of the Night Light code and the Servo.


    Build It!


    Program It!

    //include the Servo library in your sketch
    #include <Servo.h>
    //create a servo object called myServo
    Servo myServo;
    //create calibration variable
    int cal;
    void setup()
    {
    //take a calibration value right away
    cal = analogRead(A0);
    //attach myServo to pin 9
    myServo.attach(9);
    //write 90 to myServo as a pre-set angle
    }
    void loop()
    {
    //store the sensor value on A0 to the variable val
    int val = analogRead(A0);
    //if val is lress than the calibration value minus 50 for sensitivity write 160 to
    //myServo, else write 20 to myServo
    if(val < cal -50)
    {
    myServo.write(160);
    }
    else
    {
    myServo.write(20);
    }
    }
    view raw ServoNightLight.ino hosted with ❤ by GitHub


    Button Latching with a Servo

    You can apply the concept of latching to a servo and a number of angles depending the latching variable. Instead of a single 1 or 0, you are going to store numbers from 0 to 4, each one will have an angle associated with it. To control your servo you will be using "else if" to further expand an if statement to multiple options.


    Build It!


    Program It!

    //include the servo library in your sketch
    #include <Servo.h>
    //Create a servo object called myServo
    Servo myServo;
    //create a global boolean variable to toggle
    //when button is pressed, set it to true.
    int buttonState = 0;
    void setup()
    {
    //pinModes for H-Bridge control pins
    myServo.attach(9);
    //set pin 6 to INPUT_PULLUP, no need for a pullup resistor
    pinMode(6,INPUT_PULLUP);
    }
    void loop()
    {
    //if buttonState is 0 (less than 1) toggle the state variable to
    //the opposite of the current state (!state)
    if(digitalRead(6)==0)
    {
    buttonState++;
    delay(500);
    }
    //if state is false move myServo to 145, else 20.
    if(buttonState==1)
    {
    myServo.write(40);
    }
    else if(buttonState==2)
    {
    myServo.write(60);
    }
    else if(buttonState==3)
    {
    myServo.write(120);
    }
    else if(buttonState >= 4)
    {
    myServo.write(20);
    buttonState=0;
    }
    }
    view raw LatchingServo.ino hosted with ❤ by GitHub

    Soldering Resources

    Microcontrollers for Educators Materials

    Of particular interest:

    Comments 0 comments