student lecture 3
DESCRIPTION
Steve, Thai, Bruce - AnalogWrite, PWM, Fading LED's, Multiple LED's via PWM, Tri-Color LEDs, Cases, Switch Case, gotoTRANSCRIPT
![Page 1: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/1.jpg)
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
![Page 2: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/2.jpg)
Pulse Width ModulationPulse Width Modulation
on off 50% or fade?
0v 5v2.5v
analog
digital
0v / 5v
= 50% brightness
= 0% or 100%
![Page 3: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/3.jpg)
Analog Write
0volts
5volts
pulse widthdealy
delay
0volts
5volts
delay
Fading modulation - analogWrite(ledpin1, pulsewidth); // 255 brightest to ~ 0
“equivalent voltage”
![Page 4: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/4.jpg)
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); } }
![Page 5: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/5.jpg)
Multiple LED’s via PWM
![Page 6: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/6.jpg)
Tri-Color LED’s
• Two types
• Common Anode
• Connect common pin to ground
• Common Cathode
• Connect common pin to +5V
![Page 7: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/7.jpg)
Tri-Color LED’s
![Page 8: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/8.jpg)
Tri-Color LED’s
![Page 9: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/9.jpg)
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
![Page 10: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/10.jpg)
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
![Page 11: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/11.jpg)
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}
![Page 12: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/12.jpg)
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}
![Page 13: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/13.jpg)
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;}
![Page 14: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/14.jpg)
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}
![Page 15: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/15.jpg)
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
![Page 16: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/16.jpg)
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
![Page 17: Student Lecture 3](https://reader033.vdocuments.us/reader033/viewer/2022051610/54978e9fb47959564d8b52ad/html5/thumbnails/17.jpg)
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: