Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Advertisements

Lab7: Introduction to Arduino
ARDUINO FRAMEWORK.
Mini-SumoBot Construction and Programming
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.
Programming Microcontrollers B. Furman 19MAR2011.
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Finish your programs from last week STOPLIGHT CIRCUIT! You may need … – int – void setup() – void loop() – pinMode – digitalWrite – delay.
1 Arduino Board: Arduino UNO Arduino Programing Environment: Arduino 0022
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.
Analog and Digital Measurements living with the lab 14 digital input / output pins 6 analog input pins © 2011 LWTL faculty team.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
chipKit Sense Switch & Control LED
ProtoSnap Introduction to Arduino Casey Haskell, Pete Lewis, David Stillman, Jim Lindblom, Pete Dokter, Lindsay Levkoff, Trevor Zylstra.
Introduction to the Arduino
Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm.
Autumn, 2014C.-S. Shieh, EC, KUAS, Taiwan1 智慧電子應用設計導論 (1/3) Arduino Programming Language Chin-Shiuh Shieh ( 謝欽旭 ) Department.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Suleyman Demirel University CSS340 Microprocessor Systems – Lecture 2 ATMEGA328P ARCHITECTURE ANALOG INPUTS.
Code The Arduino Environment.
BM-305 Mikrodenetleyiciler Güz 2015 (3. Sunu) (Yrd. Doç. Dr. Deniz Dal)
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
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
Microcontroller basics Embedded systems for mortals.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
Lecture 9: Introduction to Arduino Topics: Arduino Fundamentals, Bean Date: Mar 22, 2016.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Introduction to Programming the Arduino Dr Gaw 3/21/14.
Pulse-Width Modulation: Simulating variable DC output
Pulse Width Modulation Instructor Dr Matthew Khi Yi Kyaw.
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.
BM-305 Mikrodenetleyiciler Güz 2016 (3. Sunu)
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Assist. Prof. Rassim Suliyev - SDU 2017
Microcontroller basics
Val Manes Department of Math & Computer Science
Microcontroller basics
Arduino Programming Part II
Microcontroller basics
An Arduino Workshop A Microcontroller.
Welcome to Arduino A Microcontroller.
Get Your Project Started with Arduino
UCD ElecSoc Robotics Club 2017/2018
Lab 1: Arduino Basics Topics: Arduino Fundamentals, First Circuit
Arduino Part 1 Topics: Microcontrollers Programming Basics
INC 161 , CPE 100 Computer Programming
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
What is an Arduino ? Open Source electronic prototyping platform based on flexible easy to use hardware and software.
IoT Programming the Particle Photon.
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
UNIT 5 Analog signals.
Arduino Uno circuit basics
Setting up a basic program with Arduino
Introduction to Arduino IDE and Software
Pulse-Width Modulation: Simulating variable DC output
Presentation transcript:

Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1

Arduino Software At least one member of your team should have already downloaded the Arduino software onto your laptop If not, go to and download the appropriate versionhttp:// C Programming2

Connecting the Arduino The Arduino connects to your laptop through USB, which can also supply power Once connected, you can upload programs to it C Programming3

Arduino C Datatypes byte or char – 8 bits int and unsigned int – 16 bits long, unsigned long, double, and float – 32 bits Boolean variables can be either true or false, but occupy one byte C Programming4

Variable Declaration Just like C/C++ (since it is) int x; float voltage = 5.0; bool bContinue = true; const int LEDPIN = 7; C Programming5

Using Constants Many programmers use #define for this: #define LEDPIN 7 Better practice is to use const: const int LEDPIN = 7; Using constants lets you change your code easily should you decided to move things to different pins, for example Constants are, by convention, in upper case C Programming6

Statements and Operators A statement is the fundamental unit of a program Assignment statements give a value to a variable Arithmetic operators are +, -, *, /, % (moduls) and ++, -- Relational operators are >, =, <=, and != Logical operators are &&, || and !(not) Bitwise operators: &, |, ^(xor), ~(negation), >(shift right) C Programming7

Comments C allows two styles of comments // This is a comment. The rest of the line is ignored /* this is a comment up to the */ You can use the first kind for block comments and at the end of a line C Programming8

Comments You can even use the second kind within a statement: void someFunction(int input, /*value in range 0-4 */ int modifier); Use lots of descriptive comments in your programs C Programming9

Comments Programs you write for this workshop should have, at a minimum: A block of comments at the beginning of the program with your team number, the names of team members, the date you started the program, and a short description of what it does Comments above each function describing what it does Comments at the start of loops describing what they do C Programming10

Conditionals – if statement The if statement executes the next statement or block of statements if the condition in the parentheses is true, and does not execute it if the statement is false: int x = 5; if (x > 4) { Serial.println(“This will get printed”); } C Programming11

Conditionals – if/else statement The if/else statements allow you to execute one piece of code if the condition is true and another if it is false: int x = 5; if (x > 4) { Serial.println(“The statement was true”); } else { Serial.println(“The statement was false”); } C Programming12

Conditionals – switch/case statements This allows you to select a group of statements to execute based upon the value of a variable switch(expression) { case 1: break; case 2: break; default: break; } C Programming13

Looping – while loop You can loop while a condition is true The loop exits when it becomes false int x=0; while (x < 10) { Serial.println(x); x++;// Don’t forget this! } C Programming14

Looping – do-while loop This is a post-test loop. It will always get executed at least once: int x = 10; do { Serial.println(“This is always executed once.”); } while (x < 10); C Programming15

Looping – for loop If you want to iterate for a specific number of times, use this It is also a pre-test loop, meaning the body may never get executed for (int ix=0; ix<10; ix++) { Serial.println(ix); } C Programming16

Functions You can create your own functions that do things and return values You need to either define the function before you call it, or you can declare it at the top of the program. int getSpeed(int);// This declares the function C Programming17

Six Basic Arduino Concepts digitalWrite() analogWrite() digitalRead() digitalWrite() Pulse Width Modulation (PWM) Serial Communication C Programming18

Arduino Programs Arduino programs are called sketches, and your collection of programs is called your sketchbook When you open Arduino Studio, you get a basic sketch that looks like this: void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: } C Programming19

Arduino Programs And that’s it Embedded programs are meant to run forever, so there is no exit from the “loop” function C Programming20

Digital I/O The pinMode function sets a pin for either input or output pinMode(LEDPIN, OUTPUT); Change the state of the pin: digitalWrite(LEDPIN, HIGH); Note that HIGH, LOW, INPUT, and OUTPUT are in all caps because they are constants built into the Arduino development system LEDPIN is a constant you define, and should also be in all caps C Programming21

Digital I/O You can also read the value from a pin: pinMode(SWITCHPIN, INPUT); Read the state of the pin: int switchState = digitalRead(SWITCHPIN); Since this is a digital pin, you’ll get a value of 1 or 0 C Programming22

Analog I/O The Arduino Uno has a 6-channel analog-to-digital converter and six analog input pins This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023 This yields a resolution between readings of: 5 volts / 1024 units or,.0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference(). C Programming23

Pulse Width Modulation (PWM) A few pins on the Arduino allow for us to modify the output to mimic an analog signal This is done by a technique called Pulse Width Modulation (PWM) C Programming24

Pulse Width Modulation (PWM) By varying the duty cycle, we can mimic an “average” analog voltage C Programming25

Writing Analog Values C Programming26 analogWrite(pin, val); pin – refers to the OUTPUT pin (limited to pins 3, 5, 6, 9, 10, 11.) – denoted by a # symbol val – 8 bit value (0 – 255). 0 => 0V | 255 => 5V

Writing Analog Values: Fade Move the LED jumper to pin 9 In Arduino, open up: File  Examples  01.Basics  Fade C Programming27

Writing Analog Values: Fade Challenge – Change the rate of the fading in and out. There are at least two different ways to do this – can you figure them out? C Programming28

Serial Communication Method used to transfer data between two devices Called serial because only one bit is transferred at a time Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit. C Programming29

Serial Communication You can output to the serial port from within your program That’s the Serial.println() function I have been using You can monitor this from your computer for debugging once the program is running: Go to Tools->Serial Monitor and you’ll get a window showing all serial output C Programming30

Arduino Programming Reference Main programming reference page: C Programming31