Oct 26th 2016
Know How... 256
Arduino 101, Basic Input and Output
Program an Arduino for simple input and output data!
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 setup()
{
// Open the serial port so we can see the values of our button
Serial.begin(9600);
// Setting the mode for the pin to which the button is connected
pinMode(buttonPin1, INPUT);
}
void loop()
{
// Call the first Button
OnOff = digitalRead(buttonPin1);
if (OnOff == HIGH)
{
Serial.print("High");
Serial.print("\t");
}
else
{
Serial.print("Low");
Serial.print("\t");
}
Serial.print("\n");
}
_05_MultiButton
// Using constants to define to which pin each button is connected
#define buttonPin1 2
#define buttonPin2 3
#define buttonPin3 4
// This is the variable that stores the state of the button
int OnOff = 0;
void setup()
{
// Open the serial port so we can see the values of our buttons
Serial.begin(9600);
// Setting each pin for input
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop()
{
// Call the first Button
OnOff = digitalRead(buttonPin1);
if (OnOff == HIGH)
{
Serial.print("High");
Serial.print("\t");
}
else
{
Serial.print("Low");
Serial.print("\t");
}
// Call the second Button
OnOff = digitalRead(buttonPin2);
if (OnOff == HIGH)
{
Serial.print("High");
Serial.print("\t");
}
else
{
Serial.print("Low");
Serial.print("\t");
}
// Call the third Button
OnOff = digitalRead(buttonPin3);
if (OnOff == HIGH)
{
Serial.print("High");
Serial.print("\t");
}
else
{
Serial.print("Low");
Serial.print("\t");
}
Serial.print("\n");
}
_06_AdvMultiButton
// Using constants to define to which pin each button is connected
#define buttonPin1 2
#define buttonPin2 3
#define buttonPin3 4
// This is the variable that stores the state of the button
int OnOff = 0;
void setup()
{
// Open the serial port so we can see the values of our buttons
Serial.begin(9600);
// Setting each pin for input
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop()
{
// Calline the buttonRead function once for each button
buttonRead(2);
buttonRead(3);
buttonRead(4);
// Start a new line in the serial output
Serial.print("\n");
}
void buttonRead(int pin)
{
// Read the digital pin and put the value in OnOff
OnOff = digitalRead(pin);
if (OnOff == HIGH)
{
Serial.print("High");
Serial.print("\t");
}
else
{
Serial.print("Low");
Serial.print("\t");
}
}
Connect with us!
- Don't forget to check out our large library of projects at https://twit.tv/shows/know-how.
- Tweet at us at @PadreSJ, @Cranky_Hippo, and @Anelf3.
Thanks to CacheFly for the bandwidth for this show.