Setting up a basic program with Arduino

Slides:



Advertisements
Similar presentations
Khaled A. Al-Utaibi Interfacing an LED The Light Emitting Diode (LED) Applications DC Characteristics & Operation Interfacing to.
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Lab7: Introduction to Arduino
Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
Embedded Sumo 1T4 – 1T5 UTRA.
Programming Microcontrollers B. Furman 19MAR2011.
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.
Intro to Programming and Microcontrollers. Activity Group into pairs and sit back-to-back. Pick one person who is going to draw. The other person will.
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.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
chipKit Sense Switch & Control LED
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Good LED Circuit 5V0 GND. What Voltage Does Meter See? Answer: 5 V.
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.
Microcontrollers, Microcomputers, and Microprocessors
Arduino libraries Datatekniker Udvidet hardware/software.
Microcontroller basics Embedded systems for mortals.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
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
Robotics Grant Agreement No LLP UK-LEONARDO-LMP Project acronym: CLEM Project title: Cloud services for E-Learning in Mechatronics Technology.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Arduino Training For You! Learn Programming at Your Own Pace! Chapter 1 A course developed from the text book: “Introduction to Arduino A Piece of Cake!”
Arduino + Bluetooth TYWu. Connection Arduino + Bluetooth Module.
:Blink Blink: Er. Sahil Khanna
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin 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.
Hacking on Arduino George Patterson
Getting Started: Building & Programming
INTRODUCTION TO ROBOTICS Part 5: Programming
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Microcontroller basics
More on LED’s with Arduino
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Val Manes Department of Math & Computer Science
Microcontroller basics
Arduino & its hardware interfacing
Arduino Programming Part II
Microcontroller basics
Get Your Project Started with Arduino
European Robotic LABoratory
UCD ElecSoc Robotics Club 2017/2018
Lab 1: Arduino Basics Topics: Arduino Fundamentals, First Circuit
INC 161 , CPE 100 Computer Programming
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
Arduino.
Въведение в Arduino.
Arduino Basics Connect Arduino to USB port
Week 6: Microcontrollers II
IoT Programming the Particle Photon.
Arduino 101 Credit(s):
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
An Introduction to Java – Part I, language basics
Chapter 2: Java Fundamentals
Programming 2: The Arduino IDE & First Sketches
EXPRESSIONS, PAUSES AND SOUNDS
Aeroponic Engineering and Vertical Farming
Arduino Uno circuit basics
Introduction to Arduinos
SAURABH GINGADE.
UNIT 1 First programs.
Arduino程式範例.
Introduction to Arduino IDE and Software
Presentation transcript:

Setting up a basic program with Arduino Variable Declarations Program Setup References: Exploring Arduino – by Jeremy Blum Arduino Project Book – by Arduino https://www.arduino.cc/en/Reference/VariableDeclaration

Single line comments denoted by // Understanding Code Basics Comments: Single line Single line comments denoted by // Compiler ignores everything after the symbol Comment out a line to troubleshoot sval = sval / 5;    // average   sval = sval / 4;    // scale to 8 bits (0 - 255)   sval = 255 - sval;  // invert output Multiple line Multiple line comments start with /* and end with*/ Compiler ignores everything between the symbols Used for large descriptions and program details /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. (Another valid comment) */

Variable Declarations: Understanding Code Basics Variable Declarations: Variables are a place in the Arduinos memory that holds information int –Primary for number storage void loop() {   int i = 2;   int j = 3;   int k;   k = myMultiplyFunction(i, j); // k now contains 6   Serial.println(k); char – Stores a character value: ex ‘A’ . Characters can be stored as numbers. Example Code char myChar = 'A’; char myChar = 65; // both are equivalent

Variable Declarations: Understanding Code Basics Variable Declarations: byte –Stores an unassigned 8-bit number from 0-255 byte x; x = 0; x = x - 1; // x now contains 255 - rolls over in neg. direction x = 255; x = x + 1; // x now contains 0 - rolls over long –Extended size variables for number storage, store 32 bits (4 bytes) from -2,147,483,648 to 2,147,483,647 Example Code long speedOfLight = 186000L; // see the Integer Constants page for explanation of the 'L'

Variable Declarations: Understanding Code Basics Variable Declarations: float –Used for numbers that have decimal numbers Example Code float myfloat; float sensorCalbrate = 1.117; int x; int y; float z; x = 1; y = x / 2; // y now contains 0, ints can't hold fractions z = (float)x / 2.0; // z now contains .5 (you have to use 2.0, not 2)

Variable Declarations: Understanding Code Basics Variable Declarations: Double –Double precision floating point number. On the Uno and other ATMEGA based boards, this occupies 4 bytes. That is, the double implementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision. If you borrow code from other sources that includes double variables may wish to examine the code to see if the implied precision is different from that actually achieved on ATMEGA based Arduinos.

Variable Declarations: Understanding Code Basics Variable Declarations: string – In general, used to represent text char Str1[15]; char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'}; char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'}; char Str4[ ] = "arduino"; char Str5[8] = "arduino"; char Str6[15] = "arduino"; Possibilities for declaring strings Declare an array of chars without initializing it as in Str1 Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2 Explicitly add the null character, Str3 Initialize with a string constant in quotation marks; the compiler will size the array to fit the string constant and a terminating null character, Str4 Initialize the array with an explicit size and string constant, Str5 Initialize the array, leaving extra space for a larger string, Str6

void setup – Used at the beginning of the program. It is where Understanding Code Basics Program Setup: void setup – Used at the beginning of the program. It is where variables are initialized, pins are defined as inputs or outputs. Executes what is between the { } (curly braces) Only runs once. When the Arduino is started or reset. void setup() {   // initialize digital pin LED_BUILTIN as an output.   pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() {   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)   delay(1000);                       // wait for a second   digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW   delay(1000);                       // wait for a second }

void loop – Runs continuously after the setup. This is where Understanding Code Basics Program Setup: void loop – Runs continuously after the setup. This is where inputs and outputs are turned on, motors controlled, sensors are Read. Executes what is between the { } (curly braces) void setup() {   // initialize digital pin LED_BUILTIN as an output.   pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() {   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)   delay(1000);                       // wait for a second   digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW   delay(1000);                       // wait for a second }

pinMode– Configures a specific pin number to be an input or an output. Understanding Code Basics Program Setup: pinMode– Configures a specific pin number to be an input or an output. void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off }

digitalRead– Reads the value from a specified pin Understanding Code Basics Program Setup: digitalRead– Reads the value from a specified pin int ledPin = 13; // LED connected to digital pin 13 int inPin = 7; // pushbutton connected to digital pin 7 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output pinMode(inPin, INPUT); // sets the digital pin 7 as input } void loop() val = digitalRead(inPin); // read the input pin digitalWrite(ledPin, val); // sets the LED to the button's value

digitalWrite– Writes a HIGH or Low value to a specific pin Understanding Code Basics Program Setup: digitalWrite– Writes a HIGH or Low value to a specific pin void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off }

delay– A delay (wait) time in milliseconds. The Arduino does Understanding Code Basics Program Setup: delay– A delay (wait) time in milliseconds. The Arduino does Nothing during this specified amount of time. void setup() { pinMode(13, OUTPUT); // sets the digital pin 13 as output } void loop() { digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off }

if– checks for a condition and executes the proceeding statement Understanding Code Basics Program Setup: if– checks for a condition and executes the proceeding statement if the condition was found to be true. else– follows the “if” and is for when the “if” condition is found to be false. if (condition1) { // do Thing A } else if (condition2) // do Thing B else { // do Thing C if (temperature >= 70) { //Danger! Shut down the system } else if (temperature >= 60 && temperature < 70) //Warning! User attention required } else //Safe! Continue usual tasks... }