chipKit Sense Switch & Control LED

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

Khaled A. Al-Utaibi Interfacing an LED The Light Emitting Diode (LED) Applications DC Characteristics & Operation Interfacing to.
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Lab7: Introduction to Arduino
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.
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.
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.
Introduction to the Arduino
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Good LED Circuit 5V0 GND. What Voltage Does Meter See? Answer: 5 V.
LECTURE 2 – INPUTS THIS LECTURE WILL INTRODUCE HOW TO INPUT SIGNALS INTO AN ARDUINO.
Code The Arduino Environment.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
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.
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.
Microcontroller basics Embedded systems for mortals.
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 + Bluetooth TYWu. Connection Arduino + Bluetooth Module.
:Blink Blink: Er. Sahil Khanna
Presentation By :- Nikhil R. Anande ( ) Electronic & Communication Engineering. 3 nd Year / 5 th Semester FACULTY GUIDE : RAHIUL PATEL SIR MICROCONTROLLER.
Introduction to Programming the Arduino Dr Gaw 3/21/14.
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.
Getting Started: Building & Programming
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Introduction to Physical Computing
Assist. Prof. Rassim Suliyev - SDU 2017
Microcontroller basics
Val Manes Department of Math & Computer Science
Microcontroller basics
Arduino Programming Part II
UTA010 : Engineering Design – II
Welcome to Arduino A Microcontroller.
Get Your Project Started with Arduino
European Robotic LABoratory
INC 161 , CPE 100 Computer Programming
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
Week 6: Microcontrollers II
IoT Programming the Particle Photon.
Servos and Stepper Motors
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
CS-4540 Robotics - Lab 05 Switch inputs to the Arduino Uno
Welcome to Digital Electronics using the Arduino Board
Arduino programs Arduino toolchain Cross-compilation Arduino sketches
Programming 2: The Arduino IDE & First Sketches
CTY SAR FCPS Shawn Lupoli, Elliot Tan
TI LaunchPad I/O Programming with Energia
Arduino Uno circuit basics
Setting up a basic program with Arduino
SAURABH GINGADE.
UNIT 1 First programs.
Arduino程式範例.
Introduction to Arduino IDE and Software
Arduino Programming: “if” statements
Presentation transcript:

chipKit Sense Switch & Control LED

Program to Turn on LED /* Set Pin 25 (ledPin) to HIGH. */ int ledPin = 25; // Label Pin 25 ledPin. void setup() { // setup() runs once. pinMode(ledPin, OUTPUT); // Set ledPin for output. } void loop() { // loop() runs "forever". digitalWrite(ledPin, HIGH); // Set ledPin HIGH.

Programming Our programs consist of a collection of functions. When writing a function, must specify: What type of thing a function returns. What its name is. What “arguments” it takes (parameters enclosed in parentheses). What it does (statements enclosed in braces {}). When using (calling) a function, must provide: Name. Arguments (enclosed in parentheses).

Programming Example: void setup() { ... } Function name Return type: void = nothing Argument list (can be empty) Body of function (code specifying what function does)

Programming Our programs must have two required functions. (These are called automatically.) void setup() {...} This runs once when the program starts. void loop() {...} This runs repeatedly after setup() is run. A comment is any text after two slashes (//) or text enclosed between /* and */. Comments are ignore by computer but very important to us!

Programming All “statements” must end with a semicolon (;). We must “declare” any variables we want to use. Must provide: Type (such as int or char). Name we want to use. Assigned value (optional). Example of declaring a variable: int ledPin = 25; Braces ({}) are used to indicate a “block” of code. Do not need a semicolon after the closing brace. Variables declared in a block of code are not defined outside of that block.

Program to Turn on LED /* Set Pin 25 (ledPin) to HIGH. */ int ledPin = 25; // Label Pin 25 ledPin. void setup() { // setup() runs once. pinMode(ledPin, OUTPUT); // Set ledPin for output. } void loop() { // loop() runs "forever". digitalWrite(ledPin, HIGH); // Set ledPin HIGH.

Programming Functions, such as pinMode(), digitalRead(), and digitalWrite(), have been written for us. Constants, such HIGH, LOW, and OUTPUT, have been defined for us. How do we learn about the things that are provided for us? Within MPIDE, click on Help, then Reference.

Programming Some noteworthy functions: pinMode(pin, mode): set whether the given “pin” (connector) is used for INPUT or OUTPUT. digitalWrite(pin, value): set whether the voltage on the given pin is HIGH or LOW. digitalRead(pin): determine whether the voltage on the given pin is HIGH or LOW. (This returns an int value, so can save to int variable, but consider value to be HIGH or LOW).

Programming More noteworthy functions: delay(ms): do nothing for ms milliseconds. millis(): returns numbers of milliseconds since the board began running. Returns an unsigned long value.

Programming if statements allow us to decide whether or not to execute some code. Code associated with a “condition” is execute if the condition is “true.” if (condition) { ... // body } If the code in the body of an if statement is a single line, braces are optional. Example: if (digitalRead(buttonPin) == HIGH) digitalWrite(ledPin, HIGH);

Programming Comparison operators often used in if statements: == (equal to): 9 == 7 is false; 9 == 9 is true. != (not equal to): 9 != 7 is true; 9 != 9 is false. > (greater than): 9 > 7 is true; 9 > 9 is false. < (less than): 9 < 7 is false; 9 < 9 is false; 9 < 10 is true.

Button-Controlled LED /* Set Pin 25 (ledPin) to HIGH if Pin 4 (buttonPin) is HIGH. Otherwise set Pin 25 to LOW. */ int ledPin = 25; // Label Pin 25 ledPin. int buttonPin = 4; // Label Pin 4 buttonPin. void setup() { // setup() runs once. pinMode(buttonPin, INPUT); // Set buttonPin for input. pinMode(ledPin, OUTPUT); // Set ledPin for output. } void loop() { // loop() runs "forever". // Check if buttonPin is HIGH. if (digitalRead(buttonPin) == HIGH) { digitalWrite(ledPin, HIGH); // Set ledPin HIGH. } else { digitalWrite(ledPin, LOW); // Set ledPin LOW.

Button-Controlled (Blinking) LED /* Alternate Pin 25 (ledPin) between HIGH and LOW every half second if Pin 4 (buttonPin) is HIGH. Otherwise set Pin 25 to LOW. */ int ledPin = 25; // Label Pin 25 ledPin. int buttonPin = 4; // Label Pin 4 buttonPin. void setup() { // setup() runs once. pinMode(buttonPin, INPUT); // Set buttonPin for input. pinMode(ledPin, OUTPUT); // Set ledPin for output. } void loop() { // loop() runs "forever". // Check if buttonPin is HIGH. If so, blink. if (digitalRead(buttonPin) == HIGH) { digitalWrite(ledPin, HIGH); delay(500); // Wait a half second. digitalWrite(ledPin, LOW); delay(500); // Wait another half second. } else {

Programming In previous code, change 500 to 5000. Notice any problems? For 10 seconds we can’t do anything to change what is happening (or what will happen). We want to fix this so we are always monitoring button while also blinking the light!

int ledState = LOW; int msDelay = 5000; void loop() { if (digitalRead(buttonPin) == HIGH) { unsigned long previousTime = millis(); ledState = HIGH; while (digitalRead(buttonPin == HIGH) { unsigned long currentTime = millis(); if (currentTime – previousTime > msDelay) { previousTime = currentTime; if (ledState == LOW) else ledState = LOW; } digitalWrite(ledPin, ledState); } else { digitalWrite(ledPin, LOW);