Student Lecture 3

1,291 views

Published on

Steve, Thai, Bruce - AnalogWrite, PWM, Fading LED's, Multiple LED's via PWM, Tri-Color LEDs, Cases, Switch Case, goto

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,291
On SlideShare
0
From Embeds
0
Number of Embeds
337
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Student Lecture 3

  1. 1. Student Lecture Session 3 AnalogWrite, PWM, Fading LED's, Multiple LED's via PWM, Tri-Color LEDs Cases, Switch Case, goto Thai Le, Bruce Drummond, Stephen Varga
  2. 2. Pulse Width Modulation Pulse Width Modulation 50% or fade? on off analog = 50% brightness 2.5v 0v 5v digital = 0% or 100% 0v / 5v
  3. 3. Analog Write pulse width dealy 5volts 0volts delay Fading modulation - analogWrite(ledpin1, pulsewidth); // 255 brightest to ~ 0 5volts “equivalent voltage” 0volts delay
  4. 4. Fading LED’s int 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); } }
  5. 5. Multiple LED’s via PWM
  6. 6. Tri-Color LED’s • Two types • Common Anode • Connect common pin to ground • Common Cathode • Connect common pin to +5V
  7. 7. Tri-Color LED’s
  8. 8. Tri-Color LED’s
  9. 9. 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
  10. 10. 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
  11. 11. 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 }
  12. 12. 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 }
  13. 13. 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; }
  14. 14. 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 }
  15. 15. Bringing it all together • Here is a comparison of the same statment in if/else format if else Statement Switch int whichLED=3; int whichLED=3; Switch(whichLED) { if(whichLED==0) { Case 0: //Code to Light LED #0 //Code to Light LED #0 break; } else if (whichLED==1) { Case 1: //Code to Light LED #1 //Code to Light LED #1 } else if (whichLED==2) { break; Case 2: //Code to Light LED #2 //Code to Light LED #2 } else if (whichLED==3) { break; //Code to Light LED #3 Case 3: } else { //Code to Light LED #3 break; //Turn off all the LEDs Default: } //Turn off all the LEDs break; }
  16. 16. 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
  17. 17. goto Example • Example from Arduino site for(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:

×