chip Kit Sense Switch Control LED Program to

  • Slides: 16
Download presentation
chip. Kit Sense Switch & Control LED

chip. Kit Sense Switch & Control LED

Program to Turn on LED /* Set Pin 25 (led. Pin) to HIGH. */

Program to Turn on LED /* Set Pin 25 (led. Pin) to HIGH. */ int led. Pin = 25; // Label Pin 25 led. Pin. void setup() { pin. Mode(led. Pin, OUTPUT); } // setup() runs once. // Set led. Pin for output. void loop() { // loop() runs "forever". digital. Write(led. Pin, HIGH); // Set led. Pin HIGH. }

Programming • Our programs consist of a collection of functions. • When writing a

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

Programming Example: Return type: void = nothing Function name Argument list (can be empty)

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

Programming • Our programs must have two required functions. (These are called automatically. )

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

Programming • All “statements” must end with a semicolon (; ). • We must “declare” any variables we want to use. Must provide: 1. Type (such as int or char). 2. Name we want to use. 3. Assigned value (optional). • Example of declaring a variable: int led. Pin = 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 (led. Pin) to HIGH. */

Program to Turn on LED /* Set Pin 25 (led. Pin) to HIGH. */ int led. Pin = 25; // Label Pin 25 led. Pin. void setup() { pin. Mode(led. Pin, OUTPUT); } // setup() runs once. // Set led. Pin for output. void loop() { // loop() runs "forever". digital. Write(led. Pin, HIGH); // Set led. Pin HIGH. }

Programming • Functions, such as pin. Mode(), digital. Read(), and digital. Write(), have been

Programming • Functions, such as pin. Mode(), digital. Read(), and digital. Write(), 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: • pin. Mode(pin, mode): set whether the given “pin” (connector)

Programming Some noteworthy functions: • pin. Mode(pin, mode): set whether the given “pin” (connector) is used for INPUT or OUTPUT. • digital. Write(pin, value): set whether the voltage on the given pin is HIGH or LOW. • digital. Read(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

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.

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 (digital. Read(button. Pin) == HIGH) digital. Write(led. Pin, HIGH);

Programming Comparison operators often used in if statements: == (equal to): 9 9 !=

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

Button-Controlled LED /* Set Pin 25 (led. Pin) to HIGH if Pin 4 (button.

Button-Controlled LED /* Set Pin 25 (led. Pin) to HIGH if Pin 4 (button. Pin) is HIGH. Otherwise set Pin 25 to LOW. */ int led. Pin = 25; // Label Pin 25 led. Pin. int button. Pin = 4; // Label Pin 4 button. Pin. void setup() { // setup() runs once. pin. Mode(button. Pin, INPUT); // Set button. Pin for input. pin. Mode(led. Pin, OUTPUT); // Set led. Pin for output. } void loop() { // loop() runs "forever". // Check if button. Pin is HIGH. if (digital. Read(button. Pin) == HIGH) { digital. Write(led. Pin, HIGH); // Set led. Pin HIGH. } else { digital. Write(led. Pin, LOW); // Set led. Pin LOW. } }

Button-Controlled (Blinking) LED /* Alternate Pin 25 (led. Pin) between HIGH and LOW every

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

Programming In previous code, change 500 to 5000. Notice any problems? For 10 seconds

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 led. State = LOW; int ms. Delay = 5000; void loop() { if

int led. State = LOW; int ms. Delay = 5000; void loop() { if (digital. Read(button. Pin) == HIGH) { unsigned long previous. Time = millis(); led. State = HIGH; while (digital. Read(button. Pin == HIGH) { unsigned long current. Time = millis(); if (current. Time – previous. Time > ms. Delay) { previous. Time = current. Time; if (led. State == LOW) led. State = HIGH; else led. State = LOW; } digital. Write(led. Pin, led. State); } } else { digital. Write(led. Pin, LOW); }