This loops through PWM outputs connected to the red and green LED.

Dependencies:   mbed

Committer:
mattshuman
Date:
Sat Aug 13 08:39:25 2016 +0000
Revision:
5:10267572c71b
Parent:
4:a8b0243f29b7
Updated the looping variables to i and j.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mattshuman 0:62faec5d4c84 1 #include "mbed.h"
mattshuman 0:62faec5d4c84 2
mattshuman 3:e13f77e8172a 3 //This creates Pulse Width Modulated outputs, r and g, and connects them to the red and green LED.
mattshuman 0:62faec5d4c84 4 PwmOut r(LED_RED);
mattshuman 0:62faec5d4c84 5 PwmOut g(LED_GREEN);
mattshuman 0:62faec5d4c84 6
mattshuman 0:62faec5d4c84 7 int main()
mattshuman 0:62faec5d4c84 8 {
mattshuman 3:e13f77e8172a 9 //The period of the PWM is set to 1 millisecond.
mattshuman 0:62faec5d4c84 10 r.period(0.001f);
mattshuman 0:62faec5d4c84 11 g.period(0.001f);
mattshuman 0:62faec5d4c84 12 while (true) {
mattshuman 0:62faec5d4c84 13 for (float i = 0.0f; i < 1.0f ; i += 0.001f) {
mattshuman 3:e13f77e8172a 14 float j = 2 * i; //Sweep the j value between 0 and 2, with .002 steps
mattshuman 5:10267572c71b 15
mattshuman 5:10267572c71b 16 if(j<1)
mattshuman 3:e13f77e8172a 17 r=j;
mattshuman 2:758d7363957f 18 else
mattshuman 3:e13f77e8172a 19 r=j-1; //Sweep the red LED 2 times during the loop, igorning the integer portion of the float number.
mattshuman 0:62faec5d4c84 20 g = i; //Sweep the green LED 1 time during the loop.
mattshuman 0:62faec5d4c84 21 wait (0.0025f); //Wait 2.5 milliseconds per iteration, 2.5 seconds per full loop.
mattshuman 0:62faec5d4c84 22 }
mattshuman 0:62faec5d4c84 23 }
mattshuman 0:62faec5d4c84 24 }