Two LEDs blink at different frequencies controlled by two timers. Two additional LEDs connected to PWM change brightness gradually also at different rates.

Dependencies:   mbed

main.cpp

Committer:
titanium
Date:
2018-03-30
Revision:
0:3eb6cb11b409
Child:
1:75e11b7fe6d7

File content as of revision 0:3eb6cb11b409:

#include "mbed.h"
// #include "rtos.h"

Ticker timer1, timer2, timer3, timer4;
DigitalOut digled1(LED1), digled2(LED2);
PwmOut pwmled3(LED3), pwmled4(LED4);

// DigitalIn digin5(p5);
DigitalOut digout7(p7);

void task1()
{ while (1) wait(0.3); }

// Thread th1(&task1, NULL, 50, 512, malloc(512));
// Thread th2(&task1, NULL, 51, 512, malloc(512));

void flipled1()  { digled1 = !digled1;}
void flipled2 () { digled2 = !digled2; }
void changeled3()
{ static float pwmval=0.0;
  static int seq=0;
  seq=(seq+1) % 100; // 1-100
  pwmval= float(seq*seq) / 10000.0;
  pwmled3.write(pwmval);    }
void changeled4() 
{ static float pwmval=0.0;
  static int seq=0;
  seq=(seq+1) % 100; // 1-100
  pwmval= float(seq*seq) / 10000.0;
  pwmled4.write(pwmval);    }

int main() {
   timer1.attach(&flipled1, 0.8);
   timer2.attach(&flipled2, 0.35);
   timer3.attach(&changeled3, 0.005);
   timer4.attach(&changeled4, 0.003);
   while(1) { digout7= ! digout7; }    }