Post on 18-Dec-2014
description
transcript
Student Lecture Session 3AnalogWrite, PWM, Fading LED's,
Multiple LED's via PWM, Tri-Color LEDsCases, Switch Case, goto
Thai Le, Bruce Drummond, Stephen Varga
Pulse Width ModulationPulse Width Modulation
on off 50% or fade?
0v 5v2.5v
analog
digital
0v / 5v
= 50% brightness
= 0% or 100%
Analog Write
0volts
5volts
pulse widthdealy
delay
0volts
5volts
delay
Fading modulation - analogWrite(ledpin1, pulsewidth); // 255 brightest to ~ 0
“equivalent voltage”
Fading LED’sint value = 0; // variable to keep the actual value int ledpin = 9; // light connected to digital pin 9
void setup() { // nothing for setup } void loop() {
for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) { analogWrite(ledpin, value); // sets the value (range from 0 to 255) delay(30); // waits for 30 milli seconds to see the dimming effect }
for(value = 255; value >=0; value-=5) // fade out (from max to min) { analogWrite(ledpin, value); delay(30); } }
Multiple LED’s via PWM
Tri-Color LED’s
• Two types
• Common Anode
• Connect common pin to ground
• Common Cathode
• Connect common pin to +5V
Tri-Color LED’s
Tri-Color LED’s
Switch/Case Statement
• Used when in place of complicated if/else logic statements
• Functionality is the same, but easier to read,modify and use.
• Preferred by advanced programmers, however easy to use for everyone
Basic Syntax
• Statements start with “Switch(variable or statement)” syntax
• ie: Switch(skyIsBlue) or Switch(myVariable)
• Variable or statement is evaluated by the “Switch()” statement, and the results are then accessed by a “Case” statement
Basic Syntax
• “Case” statements are used to evaluate all possible values of the “Switch()” results
• Switch(skyIsBlue) { Case 0: //Sky Isn’t Blue, do something Case 1://Sky is Blue, do something else}
Syntax
• “Default” is a special case, used if none of the “Case” values are met
• Switch(skyIsBlue) { Case 0: //Sky Isn’t Blue, do something Case 1://Sky is Blue, do something else Default://Do the default functionality}
Syntax• Each condition must end in a “break;”
• If “break;” is not present, the statement will continue to execute. This may or may not be useful.
• Switch(skyIsBlue) { Case 0: //Sky Isn’t Blue, do something break; Case 1: //Sky is Blue, do something else break; Default: //Do the default functionality break;}
Bringing it all together• Here is an example of the full syntax of a Switch/Case Statement
• int whichLED=3;
Switch(whichLED) { Case 0: //Code to Light LED #0 Case 1: //Code to Light LED #1 Case 2: //Code to Light LED #2 Case 3: //Code to Light LED #3 Default: //Turn off all the LEDs}
Bringing it all together
int whichLED=3;
Switch(whichLED) { Case 0: //Code to Light LED #0 break; Case 1: //Code to Light LED #1 break; Case 2: //Code to Light LED #2 break; Case 3: //Code to Light LED #3 break; Default: //Turn off all the LEDs break;}
int whichLED=3;
if(whichLED==0) { //Code to Light LED #0} else if (whichLED==1) { //Code to Light LED #1} else if (whichLED==2) { //Code to Light LED #2} else if (whichLED==3) { //Code to Light LED #3} else { //Turn off all the LEDs}
if else Statement Switch• Here is a comparison of the same statment in if/else format
goto Statements
• Allow you to control the flow of your program
• Takes the program out of its ordinary logical execution pattern and allows you to label places in the program to jump to
• Pretty much universally unrecommended
goto Example
• Example from Arduino sitefor(byte r = 0; r < 255; r++){ for(byte g = 255; g > -1; g--){ for(byte b = 0; b < 255; b++){ if (analogRead(0) > 250){ goto bailout;} // more statements ... }
}}bailout: