Introduction to Programming the Arduino Dr Gaw 3/21/14.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Lab7: Introduction to Arduino
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
How to use Arduino By: Andrew Hoffmaster.
An Introduction to C Adam Gleitman – IAP 2014.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
EBOT: Programming Primer Sean Donovan Alexander Hecht Justin Woodard.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
chipKit Sense Switch & Control LED
Physics 120B: Lecture 1 Course Structure Crash Course for Arduino Crash Course in C.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
CPS120: Introduction to Computer Science Decision Making in Programs.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Code The Arduino Environment.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP. ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson,
Microcontrollers, Microcomputers, and Microprocessors
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Arduino Programming. THE ARDUINO IS A MICROCONTROLLER – A LOW COST, LOW PERFORMANCE COMPUTER.
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Chapter 2 Variables.
The Machine Model Memory
Val Manes Department of Math & Computer Science
Arduino & its hardware interfacing
More important details More fun Part 3
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
Arduino Part 1 Topics: Microcontrollers Programming Basics
Control Structures – Selection
Unit 2 Programming.
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Programming 2: The Arduino IDE & First Sketches
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
EXPRESSIONS, PAUSES AND SOUNDS
Chapter 3: Selection Structures: Making Decisions
Chapter 2 Variables.
Just Enough Java 17-May-19.
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

Introduction to Programming the Arduino Dr Gaw 3/21/14

What is an Arduino? Arduino is a kind of like Lego for the computer and science crowd. – It has a microcomputer on a semiconductor board to which you attach sensors or switches The sensors include: an accelerometer, a joystick, a gyroscope, … – You then write computer code to control the sensors and do something with the information from the sensor/switch Turn on lights, motors, speakers, … “Arduino is a tool for making computers that can sense and control more of the physical world than your laptop computer”

Check it out … Fire up your browser and go to sers Or … go to Google and do a search for Arduino projects …..

C Computer Language The Arduino uses a variant of the C computer language. – The computer language C has been around since the 1970s Developed at Bell Laboratories (AT&T) – Interestingly, the first version of C was called B – Unix was rewritten in C in the 1970s There’s a C shell environment in Unix – Languages like C++ and Java were developed from C

More on C C is a complied language – You run your code through a computer program called a complier to make a machine executable version of your code – The complier will let you know if you have made any mistakes in your programing syntax The complier can not check your logic. It just checks the validity of your coding (parameters, spelling, etc) Remember, the computer executes each line of your code sequentially – so we’ll need to think like a computer

Arduino Programming Arduino code is called a “sketch” and is written in the supplied IDE (integrated development environment) Some syntax – anything after // on a line is ignored as a comment – braces { } encapsulate blocks – semicolons ; must appear after every command exceptions are conditionals, loop invocations, subroutine titles – every variable used in the program needs to be declared common options are int, float, char, long, unsigned long, void conventionally happens at the top of the program, or within subroutine if confined to { } block – Formatting (spaces, indentation) are irrelevant in C but it is to your great benefit to adopt a rigid, readable format much easier to read if indentation follows consistent rules the IDE will force some structure on your coding

Arduino Gotchas Always start your code with a call of the function setup() – This is run once and initializes variables or defines input/output pins on the Arduino – We actually type in void setup() because nothing is returned from the call We then follow with a call of the function loop() – This runs indefinitely and since it returns nothing we type in void loop() The standard arithmetic operators are available – These are: + - / * (plus, minus, divide, multiply) – The = (a single equal sign) is used to assign the value to the right of the equal sign to the variable on the left hand side of the equal sign Comparison operators (used in if statements) > Greater than>= Greater than or equal to < Less than<= Less than or equal to == (two equal signs) equal to!=not equal to Two characters are used for Boolean operators “and” + “or” &&and ||or !(called bang) not

Example Sketch in Arduino IDE

A Real Bit of Code

Arduino Commands Command reference: Here is a list of commands: –pinMode (pin, [INPUT | OUTPUT]) –digitalWrite (pin, [LOW | HIGH]) –digitalRead (pin)  int –analogWrite (pin, [0…255]) –analogRead (pin)  int in range [ ] –delay (integer milliseconds) –millis()  unsigned long (ms elapsed since reset)

And some more … –Serial.begin (baud): in setup ; 9600 is common choice –Serial.print (string): string  “example text “ –Serial.print (data): prints data value (default encoding) –Serial.print (data,encoding) encoding is DEC, HEX, OCT, BIN, BYTE for format –Serial.println() : just like print, but CR & LF ( \r\n ) appended –Serial.available()  int (how many bytes waiting) –Serial.read()  char (one byte of serial buffer) –Serial.flush() : empty out pending serial buffer

Defining Variables You must define your variables before you use them in your code – Generally done in the setup()

If statements (Conditionals) Used a lot to test whether a condition has been met – Put what you want to happen if the condition is met inside of {} If … else (very powerful)

Boolean Logic When using Boolean operators in a conditional statement, you need to know the following truth table (&& is “and” and || is “or”) – Think of A or B as the logical result (true or false) of testing the condition in an if statement, like … if(i>=3) “and” is only true when both conditions are true. “or” is only false when both conditions are false. AB(A && B)(A || B) FFFF FTFT TFFT TTTT

Compound Operators Programmers like to keep things simple, so C supports simple ways to combine commonly used operators

Functions Sometimes your program repeatedly performs an operation – Instead of writing and copying the code throughout your program, you can create a function and call it – You write the code once and put it in the function (sometimes called a subroutine)

Integer Math When working with integers, we often perform mathematical operations. Division can be tricky … Suppose you cast (declare) i and j as integers and set their values to 1 and 8 respectively. What is i/j ?? – We all know it is: – But with integer math the result will be zero – If you want it to be then you need to float the numbers before the division and to put the result into a variable that has been declared a float

Sketch Showing Integer Math

Loops Arduinos are a little strange … we actually run the microcomputer in an infinite loop that only stops when you disconnect the power – Infinite loops are generally the worse kind of coding that one can do Sometimes you might want to create a loop within your code, we do this most often with a for loop – But C also has while and do …. while loops

For Loop with Integer Math