Arduino Part #3 Variables

Slides:



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

Electrical team Arduino tutorial I BY: JOSHUA arduini
Lab7: Introduction to Arduino
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.
Finish your programs from last week STOPLIGHT CIRCUIT! You may need … – int – void setup() – void loop() – pinMode – digitalWrite – delay.
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.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
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.
Github & Arduino How To: Post On Github How To: Download from Github How To: Upload Code Downloaded from Github to Arduino Tommy Sanchez EE400D Spring.
Combining Integers What is an Integer? (No decimals – No Fractions) Negative numbers Positive numbers Zero.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Tweaking Your Simon Adding a photoresistor and changing code Instruction by Pete Lewis and Linz Craig.
Notes and Practice Adding and Subtracting Integers.
Code The Arduino Environment.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Microcontroller basics Embedded systems for mortals.
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
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!”
:Blink Blink: Er. Sahil Khanna
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.
The Internet of Things: Using the Arduino to Teach Coding
INTRODUCTION TO ROBOTICS Part 5: Programming
Build on a Breadboard Simulator
Math operations 9/19/16.
Microcontroller basics
If you want to swing an robot arm or …
Val Manes Department of Math & Computer Science
Microcontroller basics
Unit C: Expressions Lesson 03: Mathematical Properties with Variables
Arduino Programming Part II
UTA010 : Engineering Design – II
An Arduino Workshop A Microcontroller.
Welcome to Arduino A Microcontroller.
UCD ElecSoc Robotics Club 2017/2018
4th Grade ICAP Academic Planning Understanding your Report Card
Introduction to Arduino Microcontrollers
2-2 Subtracting Integers Warm Up Problem of the Day
Introduction to Arduino Microcontrollers
Arrays & Functions Lesson xx
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Arduino 101 Credit(s):
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Welcome to Digital Electronics using the Arduino Board
Rules of Integers.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Alice Variables Pepper.
The RGB LED.
Programming 2: The Arduino IDE & First Sketches
Arduino Part 4 Let there be more light.
CTY SAR FCPS Shawn Lupoli, Elliot Tan
Multiplexing seven-segment displays
Relations And Functions.
Aeroponic Engineering and Vertical Farming
you get to solve puzzles!
Lab #1: Getting Started.
Arduino Uno circuit basics
Variables in C Topics Naming Variables Declaring Variables
Intro to Arduino Part 2 The Breadboard
Setting up a basic program with Arduino
Arduino程式範例.
Project #1 Traffic Light
Introduction to Arduino IDE and Software
Presentation transcript:

Arduino Part #3 Variables

What is a Variable? Lets have a discussion about a powerful and semi-confusing programming topic - variables. A variable is like a bucket.   You choose what types of stuff you want in the bucket and can change the contents as often as you like.  When you declare a variable, you are telling the program two things, first - what types of things you plan to put in the bucket, and secondly, what the name of the bucket is so you can refer to it later.

If you tell the program you will be putting fluids in the bucket, than you can go all day filling it with soda, water, and iced tea - but the second you try to fill it with rocks, the compiler will call you out on your discrepancy.   Only fluids go in a bucket declared for fluids.  To declare a variable, you write the type of contents it will hold followed by the name: ex fluid bucketVariable; Notice in the above declaration statement that the word fluid is a different color - that is because Arduino knows variable data types - and they get a special color to reduce confusion and of course, because they are cool.

Variables There are several types of variable data types you can declare. In this lesson we will discuss the integer data type. You probably know that an integer is a whole number (no decimals). For Arduino an integer is a number from -32,768 to 32,767. If you try to put a number bigger than that into an integer variable, the value will roll over to the opposite side like a game of Pac Man. If you add 5 to 32,767, you would get -32,764. If you subtracted 5 from -32,768 you would get 32,763. Integer is abbreviated int. Since an integer is an Arduino data type, it will change color to an orange. Here is an example of how to declare a variable < int led;> This tells the Arduino that a variable led is going to contain an integer.

Variables The name of the variable can be whatever you want with certain restrictions. There are also a couple good rules to follow... The variable name should be descriptive of its function, for example the ledPin variable could be the pin number that you put your LED into on your Arduino board. By rule, most variables start lowercase. Variable names cannot be the same as keyword names. Ex pinMode or digitalWrite.

Now what if we want to put something in the bucket Now what if we want to put something in the bucket?  We first need to declare the variable, this tells the Arduino what type of data is going into the variable. We want to put an integer in it so we use the “int” Declaration. When we assign the value, it is called initialization, and we use the equal sign to do so.  It looks like this. int led;  //first we declare the variable led = 13;  //now we initialize the variable This means the led is equal to 13 anywhere in the program.

Variables Or, we can initialize and declare a variable at the same time... int led = 13; //declare and initialize a variable with a single statement This statement will go above the “void setup” section.

Lets Try it Out If your program from part 2 is not open go ahead an open up the Arduino IDE and then open your program from part 2. If you have to build the circuit again the instructions will be on the next slide, if you don’t then just skip that slide.

Lets Build a Circuit We are going to build this circuit in the diagram on the right. This circuit contains 5 things. Part 1 The Arduino board. Part 2 The breadboard. Part 3 Two wires. Part 4 An LED Part 5 A 330 ohms resistors

Lets Make the Changes Look at the code on the right and make the changes to your program

Lets Make the Changes Now this time before we upload the sketch, click on the checkmark button. This will verify that your sketch is correct. If it is not the error section at the bottom will tell you what the problem is. If your program is correct click the arrow button to send it to the Arduino.

Why We Use Variables You noticed that y0ur circuit performed exactly how it did the last time. The only change we made was to add the variables. So why do we do this. It is easier to change your program when you use variables, because we only have to make the change once compared to having to change every line. For example if we wanted to change the delay from 1000 to 200 we would have to go back and locate each delay and then make the change from a 1000 to 200. In this small program not the hard but in a large one with maybe 50 delay commands that would be very difficult

Variables Easy to Change If we wanted to change the delay time using variables all we have to do is change the line “int time = 500” to maybe a 1000. That is the only line we have to change, because the program looks at the variable “time” and then inserts the value it is equal to. Lets try it out! In your program change the variable “time” to equal a different number. And download it do it a couple times with different numbers and observe the differences.

Credit Call me over when this circuit is complete so you get credit for building the circuit.

Review Ok What have learned? Variables are used to store information. We need to tell the Arduino what is going to be stored in the variable and we do this by using the “int” command which stands for integer. Ex: int led this says that the variable will contain an integer. To initialize a variable we set it equal to a number for example: int delay = 1000. This declares the variable as an integer and assigns the value 1000 to it. We also saw that changing your program is easier with variables, you just need to change one line instead of many. Quiz time, go to schoology and take the Part 3 Quiz.