Counter & LED (LED Blink)

Similar documents
Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Halloween Pumpkinusing. Wednesday, October 17, 12

Procedure: Determine the polarity of the LED. Use the following image to help:

University of Portland EE 271 Electrical Circuits Laboratory. Experiment: Arduino

Advanced Activities - Information and Ideas

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

EEG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

Fall Harris & Harris

The Big Idea: Background:

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

Sten-SLATE ESP Kit. Description and Programming

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

Arduino IDE Friday, 26 October 2018

Arduino Programming and Interfacing

IME-100 Interdisciplinary Design and Manufacturing

To lead your students through this activity, you will need your computer, attached to a projector, for projecting your code for students to see.

WALT: definition and decomposition of complex problems in terms of functional and non-functional requirements

Serial.begin ( ); Serial.println( ); analogread ( ); map ( );

Schedule. Sanford Bernhardt, Sangster, Kumfer, Michalaka. 3:10-5:00 Workshop: Build a speedometer 5:15-7:30 Dinner and Symposium: Group 2

Introduction To Arduino

Exen Mini. Setup Guide - V1. nerdonic.com

Module 003: Introduction to the Arduino/RedBoard

4Serial SIK BINDER //77

Lesson 5: LDR Control

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

Introduction to Programming. Writing an Arduino Program

Robotics and Electronics Unit 5

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

Introduction to Arduino. Wilson Wingston Sharon

ArdOS The Arduino Operating System Quick Start Guide and Examples

Review of the syntax and use of Arduino functions, with special attention to the setup and loop functions.

Lab 2 - Powering the Fubarino. Fubarino,, Intro to Serial, Functions and Variables

Elektor Uno R4 Installation & Test

keyestudio Keyestudio MEGA 2560 R3 Board

This is the Arduino Uno: This is the Arduino motor shield: Digital pins (0-13) Ground Rail

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

WALT: definition and decomposition of complex problems in terms of functional and non-functional requirements

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Digital Design through. Arduino

Arduino Micro Breadboard Laboratory Interface Processor (Micro BLIP) User Manual

Overview. Multiplexor. cs281: Introduction to Computer Systems Lab02 Basic Combinational Circuits: The Mux and the Adder

Lesson 4: Animation. Goals

Update: Ver 1.3 Dec Arduino Learning Guide For Beginner Using. Created by Cytron Technologies Sdn Bhd - All Rights Reserved

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Introduction to Microcontrollers Using Arduino. PhilRobotics

Arduino 07 ARDUINO WORKSHOP 2007

Code&Drive First steps

Microcontrollers and Interfacing week 8 exercises

Exen Mini. Setup Guide - V2. nerdonic.com

ROBOTLINKING THE POWER SUPPLY LEARNING KIT TUTORIAL

Prototyping & Engineering Electronics Kits Basic Kit Guide

USER MANUAL ARDUINO I/O EXPANSION SHIELD

Digital Pins and Constants

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

T H E I N T E R A C T I V E S H E L L

Lab 2 - Powering the Fubarino, Intro to Serial, Functions and Variables

HTML/CSS Lesson Plans

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Specification. 1.Power Supply direct from Microcontroller Board. 2.The circuit can be used with Microcontroller Board such as Arduino UNO R3.

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

CHAPTER GETTING STARTED WITH THE ARDUINO WEB EDITOR (Online IDE) Created by Tum Kurtzman

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

PDF of this portion of workshop notes:

CTEC 1802 Embedded Programming Labs

LAB PROCEDURE. Lab Objectives 1. Generate a project using the GR-Sakura web compiler 2. Edit/Compile/Build/Debug the project using the web compiler

ENGR 40M Project 3c: Coding the raindrop pattern

RoastLogger Arduino/TC4 driver installation for Windows 9/10/13 By John Hannon (JackH) at Homeroasters.org

IoT with Intel Galileo Gerardo Carmona. makerobots.tk

inches Hardware Guide For Intel Galileo

Contents. List of Figures. TermDriver Datasheet 1. 1 Overview 2. 2 Features 2. 3 Installation with Arduino 3

C++ INTRODUCTION. GHI Electronics, LLC - Where Hardware Meets Software

DESERT CODE CAMP

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Arduino Programming. Arduino UNO & Innoesys Educational Shield

22080 IoT2 Appendix B: Get Started with Arduino IDE & ExpLoRer

Arduino Prof. Dr. Magdy M. Abdelhameed

Connecting Arduino to Processing

Lecture 7. Processing Development Environment (or PDE)


Update: Ver 1.3 Dec Arduino Learning Guide For Beginner Using. Created by Cytron Technologies Sdn Bhd - All Rights Reserved

Blinking an LED 1 PARTS: Circuit 2 LED. Wire. 330Ω Resistor

CS102: Variables and Expressions

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Designed & Developed By: Ms. Jasleen Kaur, PhD Scholar, CSE. Computer Science & Engineering Department

m-block By Wilmer Arellano

Electronic Brick Starter Kit

Maciej Sobieraj. Lecture 1

Arduino 02: Using the Arduino with Python. Jeffrey A. Meunier University of Connecticut

AndyMark Arduino Tutorial

Text Input and Conditionals

Arduino - DigitalReadSerial

How to Use an Arduino

Lastly, in case you don t already know this, and don t have Excel on your computers, you can get it for free through IT s website under software.

RedBoard Hookup Guide

IAS0430 MICROPROCESSOR SYSTEMS

Getting Started Guide

Arduino For Amateur Radio

TABLE OF CONTENTS INTRODUCTION LESSONS PROJECTS

Imperativ Programming. morning (again) Jens Dalsgaard Nielsen Aalborg University (1/24)

Transcription:

1 T.R.E. Meeting #1 Counter & LED (LED Blink) September 17, 2017 Contact Info for Today s Lesson: President Ryan Muller mullerr@vt.edu 610-573-1890 Learning Objectives: Learn how to use the basics of Arduino Uno Learn how to make a counter Learn how to use if statements Learn how to make a LED blink Agenda: Download Arduino Interface Development Environment (IDE)... 2 Set up the Arduino Environment... 2 How to Compile Code... 2 Arduino Setup and Loop Functions... 2 Coding 101 in the Arduino IDE... 3 Creating a Counter:... 3 o To open the Serial Monitor... 4 Incrementing the Counter... 4 Controlling the Counter s Output... 4 FINAL COUNTER CODE... 5 Making the Arduino s Built in LED Blink... 5 FINAL LED BLINK CODE:... 6 Methods Cheat Sheet:... 6

2 Download Arduino Interface Development Environment (IDE) 1. Go to https://ww.arduino.cc/ 2. Click Software 3. Download Arduino 1.8.4 4. Use all default options Set up the Arduino Environment 1. Connect the Arduino to your laptop via the A-B USB Cable in the kit. You will know it s connected when the green ON light turns on. NOTE! While we do have official Arduino Uno boards to use in the club, the Elegoo Uno shown is a cheaper ~$10 well-made knockoff version that is available online if you wish to purchase your own less expensive alternative. 2. Go to the Tools menu in the navigation header and select the correct board and port Tools -> Board: -> Arduino/Genuino Uno Tools -> Port: -> COM3* *The number after COM may not be necessarily 3 and ultimately depends on your laptop How to Compile Code To compile code in the Arduino IDE, we will be mainly be using the Verify and Upload tools. The Verify/Compile tool will convert your code into machine language for the Arduino to read and allows you to check your code for syntax errors. The Upload tool will actually place your program onto the board and immediately start running. You can access these tools by either going to the navigation header and selecting Sketch -> Verify/Compile Sketch -> Upload Or you can use the buttons in the header to quickly verify and upload your code. Arduino Setup and Loop Functions = verify/compile = upload When you first open the IDE you ll see two different functions already opened, setup and loop. The setup function will only run once at the beginning of the code and is used to set up the function. The loop function will run after the setup function has completed and will not stop looping through the code until the Arduino runs out of or loses power.

3 NOTE!!! If we want to reset the loop function, we press the red button on the top left corner of the Arduino Uno. Coding 101 in the Arduino IDE To code in the Arduino IDE we will be using the programming language C Comments: Lines in the program that are used to inform yourself or others about the way the program works. They are ignored by the verify tool. o To comment a single line, we use double slashes (//) i.e. int counter = 0; //initialize the counter to zero o To comment multiple lines, we use a slash and a asterisk (/**/) i.e. /* your comment goes here */ To end a line in the code, not including functions definitions such as setup and header, we must use a semicolon (;) o i.e. int counter = 0; By default, using an if, for, while control structures will only control the one-line right after use. To control Creating a Counter: 1. Define an integer, numerical, value at the top of the program, with the variable name counter, above the setup function and initialize it with a value of zero to start. We will increment this value later. int counter = 0; 2. In the setup function, we will add the method Serial.begin(9600); This method will allow you to see the serial monitor, or console, where the data will be printed. 9600 is the speed at which the data is written to the serial monitor, for now we will use this value.

4 o To open the Serial Monitor, go to the navigation header and go to Tools -> Serial Monitor 3. Still in the setup function, we will now add the method Serial.println( Starting Counter ); This method will allow you to write, or print, whatever is in the parentheses to the serial monitor. If you want to print a sentence, or a string, enclose the sentence in double quotes ( ) 4. Follow up with Serial.println(counter); Note how we don t need to use the double quotes since we are printing an integer value. 5. If we run the code now, we ll see the following printed in the serial monitor: Starting Counter 0 If we press the reset function on the Arduino, we will send the same message to the serial monitor again. Congratulations! You ve made your first code in the Arduino IDE! Incrementing the Counter We have a counter, but it doesn t change. Remember the loop function in the base code the IDE gave you, we will use this to increment the counter value. 1. In the loop function, we will first increment the counter using the following algebra expression. counter = counter + 1; Don t worry about how counter is on both sides of the equation. We are just equating the value of counter, currently 0, to counter + 1. So the new value of counter is 1 and so forth 2. Next, we will print the value of counter to the serial monitor so we can keep track of it. Serial.println(counter); 3. If we run the program now, we ll see the following in the serial monitor: Starting Counter 0 1 2 3 The counter will now continue to increment from zero with no stopping point, an infinite loop, unless it crashes or the power is taken away from the Arduino. Controlling the Counter s Output So now we have an incrementing counter, but what if we wanted to control what it printed? To do this we use control structures such as if, for, while, etc. Today we will learn the if statement. The logic behind an if statement is simple, if the condition in the parentheses is met, then perform what is in the if statement. Let s try limiting the written output to be greater than 1000. 1. In the loop function, add an if statement that will only write the count to the serial monitor when the counter is greater than 1000. if (counter > 1000) { Serial.println(counter); } NOTE! You can also use the Less Than (<), Less Than Or Equal to (>=), or the Greater Than Or

5 Equal to (>=) in your condition to compare different values. Later we will learn more ways to compare and test values in logical operators like AND (&&), OR ( ), and NOT (!). 2. If you run the program now, you will find the serial monitor prints only values after 1000 in the loop function. Starting Counter 1001 1002 1003 FINAL COUNTER CODE Making the Arduino s Built in LED Blink Now we will utilize the Arduino s built in LED light. To understand this, we need to know that the Arduino contains pins on the top and bottom that can be turned ON and OFF, 1 and 0 respectively. There are three different types of pins we will be working with, Power, Analog, and Digital, but today we will be working with digital pin 13 since it is connected to the built in yellow LED light. 1. We will start in the setup function by configuring the pin to behave as input or output. This is done via the pinmode() method and we will use it as so: pinmode(13, OUTPUT); Here we set the pin to 13, in order to use the Arduino s LED, and the mode to output, since we are currently only interested in writing values to the light. If we wanted to read the the pin, we would use INPUT in the place of OUTPUT. 2. Next, we want to turn the light on. For this we need to be in the loop function and use the method digitalwrite(pin, mode), which makes sense since we are writing to a digital pin. We use it as follows. digitalwrite(13, HIGH); By using HIGH, we are turning the LED on. Again, the pin is set to 13 since we are working with the Arduino s LED.

6 3. To subsequently turn the LED off, we use LOW. digitalwrite(13, LOW); However, if we were to leave these two methods on their own in the loop function, the loop will iterate too fast for the human eye to see, at a frequency of roughly 16 MHz. 4. To delay these methods so we can clearly see the light blink, we will us a delay method to delay the program while inside the loop. delay(1000); In delay, we wrote how long we want the loop to pause before executing the next task. We can put any positive value in delay, but note that we are inputting the time in milliseconds and that there are 1000 milliseconds in a second. FINAL LED BLINK CODE: Congratulations! You made an LED light blink using an Arduino Uno! Methods Cheat Sheet: Serial.begin(9600); o Allows you to print or write to the serial monitor o 9600 is the recommended speed at which the program writes to the serial monitor Serial.println(counter); o Prints a what s in the parenthesis and moves the cursor to a new line pinmode(13, OUPUT); o Tells the pin whether you re using it for reading (INPUT) or writing (OUTPUT) In this case we are writing to the LED light at pin 13 digitalwrite(13, HIGH); o Write a LOW or HIGH signal to the digital pin specified. o HIGH = LED ON LOW = LED OFF