Introduction: Lab One (Arduino Intro)
Lab One.
Step 1: Connect to Ground and Add LEDs.
Connect a wire from the ground rail to GND pin on the Arduino.
Place two LEDs into the small breadboard. Location and color do not matter. Connect the anodes to the ground rail.
Step 2: Connect Resistors
For each LED, connect a 220 ohm resistor to the cathode. You can connect the other end of the resistor to any pin in the Arduino; I chose pins 13 and 10.
Step 3: Add Pushbuttons
The lab also calls for two methods of input. Connect two pushbuttons to the breadboard. Bring a wire to the ground rail on the breadboard. Connect the ground rail to the 5V pin on the Arduino.
Step 4: Adding Resistors.
Unlike the LEDs, the pushbuttons require 10k ohm resistors. Use these to connect one side of each pushbutton to the ground rail.
Connect your push buttons to any pin on the Arduino board. (I chose 2 and 6).
Step 5: Code -- Initializing Variables
First, initialize the variables for the LEDs and the push buttons, as well as the push buttons' states.
int pinOne = 13; int pinTwo = 10;
int buttonOne = 6;
int buttonTwo = 2;
int buttonOneState = 0;
int buttonTwoState = 0;
Set the LEDs and buttons to the pin number they are connected to on the Arduino board. Set the button states to 0, which means that the button is off/has not been pressed.
Step 6: Code -- Setup Method
For the setup method, set your LEDs and buttons to their respective types (input and output).
void setup() {
pinMode(pinOne, OUTPUT);
pinMode(pinTwo, OUTPUT);
pinMode(buttonOne, INPUT);
pinMode(buttonTwo, INPUT);
}Step 7: Code -- Loop Method
Inside the loop method, set your button states. Currently, it is reading them as 0, or LOW.
buttonOneState = digitalRead(buttonOne);
buttonTwoState = digitalRead(buttonTwo);
In an if statement, you can control how the LEDs will react to the button being pressed. If the first button is pressed, changing the state to HIGH, the first LED will come on, and then the second one will follow after a delay.
if(buttonOneState == HIGH)
{
digitalWrite(pinOne, HIGH);
delay(100);
digitalWrite(pinTwo, HIGH);
}
In the else if portion, if the other button is pressed, both LEDs will come on at the same time.
else if(buttonTwoState == HIGH)
{
digitalWrite(pinOne, HIGH);
digitalWrite(pinTwo, HIGH);
}





