Download presentation
1
chipKit Sense Switch & Control LED
2
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.
3
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).
4
Programming Example: void setup() { ... } Function name Return type:
void = nothing Argument list (can be empty) Body of function (code specifying what function does)
5
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!
6
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.
7
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.
8
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.
9
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).
10
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.
11
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);
12
Programming Comparison operators often used in if statements:
== (equal to): == 7 is false; == 9 is true. != (not equal to): 9 != 7 is true; != 9 is false. > (greater than): 9 > 7 is true; 9 > 9 is false. < (less than): < 7 is false; < 9 is false; < 10 is true.
13
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.
14
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 {
15
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!
16
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);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.