Learn the basics of programming an Arduino with some simple input and output practice! Make an LED blink, and integrate some buttons!

_01-HelloBlink

// This is the Arduino version of "Hello World"
// We're using a digital pin to power a 5V LED
// Connect the "Long leg" of the LED to the digital pin and the short leg to GND
void setup()
{
pinMode(7, OUTPUT);
// The first step is to tell the Arduino where we're going to connect the LED
// "7" tells the pinMode function that we're connecting the positive led to digital pin 7
// "OUTPUT" tells the pinMode function that we're using the pin for output, not input
}

// Remember that the Arduino will loop the code inside of "loop()" continously

void loop() {
digitalWrite(7, HIGH);
// Power-on Digital pin 7

delay(1000);
// Wait 1000 ms

digitalWrite(7, LOW);
// Power-off Digital pin 7

delay(1000);
// Wait 1000ms
}

** Let's add some constants!
_02-AdvancedBlink

// This is the Arduino version of "Hello World"
// We're using a digital pin to power a 5V LED
// Connect the "Long leg" of the LED to the digital pin and the short leg to GND

// Let's up our game a little
// We're going to use the "define" function to set our digital pin as a constant at program start
// This will make it easier to redefine what pin we'll use for our LED
// We're also going to use a constant to set our delay period

#define dpin 7
#define wait 500

void setup()
{
pinMode(dpin, OUTPUT);
// The first step is to tell the Arduino where we're going to connect the LED
// "7" tells the pinMode function that we're connecting the positive led to digital pin 7
// "OUTPUT" tells the pinMode function that we're using the pin for output, not input
}

// Remember that the Arduino will loop the code inside of "loop()" continously

void loop() {
digitalWrite(dpin, HIGH);
// Power-on Digital pin 7

delay(wait);
// Wait 1000 ms

digitalWrite(dpin, LOW);
// Power-off Digital pin 7

delay(wait);
// Wait 1000ms
}


** And now some functions!

_03-BetterBlink

// This is the Arduino version of "Hello World"
// We're using a digital pin to power a 5V LED
// Connect the "Long leg" of the LED to the digital pin and the short leg to GND

// Know what? As long as we're getting crazy with super-simple programs, what about this?
// Let's create a a function that gets called in "loop()"

// We Removed the ""wait"" constant

#define dpin 7

void setup()
{
pinMode(dpin, OUTPUT);
// The first step is to tell the Arduino where we're going to connect the LED
// "7" tells the pinMode function that we're connecting the positive led to digital pin 7
// "OUTPUT" tells the pinMode function that we're using the pin for output, not input
}


// Remember that the Arduino will loop the code inside of "loop()" continously

void blink(int on, int off)
{
digitalWrite(dpin, HIGH);
delay(on);

digitalWrite(dpin, LOW);
delay(off);
}


void loop()
{
// We call the function, and pass it the wait parameters
// The first parameter tells the function how long to keep the LED light on
// The second parameter tell the function how long to keep the LED light off
blink(2000, 500);
}


Now that we know how to push output, let's practice input!

_04_Button


// Using constants to define to which pin each button is connected

#define buttonPin1 2

// This is the variable that stores the state of the button
void
{

These show notes have been truncated due to length. For the full show notes, visit https://twit.tv/shows/know-how/episodes/256

Hosts: Fr. Robert Ballecer, SJ and Bryan Burnett

Sponsors:

Podden och tillhörande omslagsbild på den här sidan tillhör TWiT. Innehållet i podden är skapat av TWiT och inte av, eller tillsammans med, Poddtoppen.