Factory-written program for GR-PEACH.

Dependencies:   SoftPWM mbed

Your how to videos will be live at:

https://youtu.be/Y3Wqu3FsA_o Part1

https://youtu.be/HdGGfkqVoBQ Part2

https://youtu.be/2cOqjSruDQM Part3

Committer:
1050186
Date:
Thu Apr 14 02:56:23 2016 +0000
Revision:
1:87af4f55c068
Parent:
0:52f96d297004
Change main.cpp for optimization due to the version-up of the online compiler.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyoheiHagimoto 0:52f96d297004 1 #include "mbed.h"
RyoheiHagimoto 0:52f96d297004 2 #include "SoftPWM.h"
RyoheiHagimoto 0:52f96d297004 3
RyoheiHagimoto 0:52f96d297004 4 #define LED_ON 1
RyoheiHagimoto 0:52f96d297004 5 #define LED_OFF 0
RyoheiHagimoto 0:52f96d297004 6
RyoheiHagimoto 0:52f96d297004 7 #define TIME_10ms 1
RyoheiHagimoto 0:52f96d297004 8 #define TIME_20ms 2
RyoheiHagimoto 0:52f96d297004 9 #define TIME_30ms 3
RyoheiHagimoto 0:52f96d297004 10 #define TIME_40ms 4
RyoheiHagimoto 0:52f96d297004 11 #define TIME_50ms 5
RyoheiHagimoto 0:52f96d297004 12 #define TIME_100ms 10
RyoheiHagimoto 0:52f96d297004 13 #define TIME_200ms 20
RyoheiHagimoto 0:52f96d297004 14 #define TIME_500ms 50
RyoheiHagimoto 0:52f96d297004 15
RyoheiHagimoto 0:52f96d297004 16 static Ticker flipper; // Tick Timer
RyoheiHagimoto 0:52f96d297004 17
1050186 1:87af4f55c068 18 static DigitalOut ledu(LED_USER); // LED-User
1050186 1:87af4f55c068 19 static SoftPWM ledr(LED_RED); // LED-Red
1050186 1:87af4f55c068 20 static SoftPWM ledg(LED_GREEN); // LED-Green
1050186 1:87af4f55c068 21 static SoftPWM ledb(LED_BLUE); // LED-Blue
RyoheiHagimoto 0:52f96d297004 22
RyoheiHagimoto 0:52f96d297004 23 static unsigned int syscnt_u; // SystemCounter for LED-User
RyoheiHagimoto 0:52f96d297004 24 static unsigned int syscnt_r; // SystemCounter for LED-Red
RyoheiHagimoto 0:52f96d297004 25 static unsigned int syscnt_g; // SystemCounter for LED-Green
RyoheiHagimoto 0:52f96d297004 26 static unsigned int syscnt_b; // SystemCounter for LED-Blue
RyoheiHagimoto 0:52f96d297004 27
1050186 1:87af4f55c068 28 volatile static unsigned int flg_ledu; // LED-User Flag
1050186 1:87af4f55c068 29 volatile static unsigned int flg_ledr; // LED-Red Flag
1050186 1:87af4f55c068 30 volatile static unsigned int flg_ledg; // LED-Green Flag
1050186 1:87af4f55c068 31 volatile static unsigned int flg_ledb; // LED-Blue Flag
RyoheiHagimoto 0:52f96d297004 32
RyoheiHagimoto 0:52f96d297004 33 void flip() {
RyoheiHagimoto 0:52f96d297004 34 // Check 500ms for LED-User
RyoheiHagimoto 0:52f96d297004 35 syscnt_u++; // increment SystemCounter for LED-User
RyoheiHagimoto 0:52f96d297004 36 if( syscnt_u >= TIME_500ms ){
RyoheiHagimoto 0:52f96d297004 37 flg_ledu++;
RyoheiHagimoto 0:52f96d297004 38 syscnt_u = 0;
RyoheiHagimoto 0:52f96d297004 39 }
RyoheiHagimoto 0:52f96d297004 40
RyoheiHagimoto 0:52f96d297004 41 // Check 10ms for LED-Red
RyoheiHagimoto 0:52f96d297004 42 syscnt_r++; // increment SystemCounter for LED-Red
RyoheiHagimoto 0:52f96d297004 43 if( syscnt_r >= TIME_10ms ){
RyoheiHagimoto 0:52f96d297004 44 flg_ledr++;
RyoheiHagimoto 0:52f96d297004 45 syscnt_r = 0;
RyoheiHagimoto 0:52f96d297004 46 }
RyoheiHagimoto 0:52f96d297004 47
RyoheiHagimoto 0:52f96d297004 48 // Check 20ms for LED-Green
RyoheiHagimoto 0:52f96d297004 49 syscnt_g++; // increment SystemCounter for LED-Green
RyoheiHagimoto 0:52f96d297004 50 if( syscnt_g >= TIME_20ms ){
RyoheiHagimoto 0:52f96d297004 51 flg_ledg++;
RyoheiHagimoto 0:52f96d297004 52 syscnt_g = 0;
RyoheiHagimoto 0:52f96d297004 53 }
RyoheiHagimoto 0:52f96d297004 54
RyoheiHagimoto 0:52f96d297004 55 // Check 30ms for LED-Blue
RyoheiHagimoto 0:52f96d297004 56 syscnt_b++; // increment SystemCounter for LED-Blue
RyoheiHagimoto 0:52f96d297004 57 if( syscnt_b >= TIME_30ms ){
RyoheiHagimoto 0:52f96d297004 58 flg_ledb++;
RyoheiHagimoto 0:52f96d297004 59 syscnt_b = 0;
RyoheiHagimoto 0:52f96d297004 60 }
RyoheiHagimoto 0:52f96d297004 61 }
RyoheiHagimoto 0:52f96d297004 62
RyoheiHagimoto 0:52f96d297004 63 int main() {
RyoheiHagimoto 0:52f96d297004 64
RyoheiHagimoto 0:52f96d297004 65 ledu = LED_OFF; // LED-User Off
RyoheiHagimoto 0:52f96d297004 66
RyoheiHagimoto 0:52f96d297004 67 ledr.period_ms(10); // Set PWM Period 10ms
RyoheiHagimoto 0:52f96d297004 68 ledr = 0.0f; // Set LED-Red Duty
RyoheiHagimoto 0:52f96d297004 69
RyoheiHagimoto 0:52f96d297004 70 ledg.period_ms(10); // Set PWM Period 10ms
RyoheiHagimoto 0:52f96d297004 71 ledg = 0.0f; // Set LED-Green Duty
RyoheiHagimoto 0:52f96d297004 72
RyoheiHagimoto 0:52f96d297004 73 ledb.period_ms(10); // Set PWM Period 10ms
RyoheiHagimoto 0:52f96d297004 74 ledb = 0.0f; // Set LED-Blue Duty
RyoheiHagimoto 0:52f96d297004 75
RyoheiHagimoto 0:52f96d297004 76 flg_ledu = 0; // Initialize LED-User Flag
RyoheiHagimoto 0:52f96d297004 77 flg_ledr = 0; // Initialize LED-Red Flag
RyoheiHagimoto 0:52f96d297004 78 flg_ledg = 0; // Initialize LED-Green Flag
RyoheiHagimoto 0:52f96d297004 79 flg_ledb = 0; // Initialize LED-Blue Flag
RyoheiHagimoto 0:52f96d297004 80
RyoheiHagimoto 0:52f96d297004 81 syscnt_u = 0; // Initialize System Counter for LED-User
RyoheiHagimoto 0:52f96d297004 82 syscnt_r = 0; // Initialize System Counter for LED-Red
RyoheiHagimoto 0:52f96d297004 83 syscnt_g = 0; // Initialize System Counter for LED-Green
RyoheiHagimoto 0:52f96d297004 84 syscnt_b = 0; // Initialize System Counter for LED-Blue
RyoheiHagimoto 0:52f96d297004 85
RyoheiHagimoto 0:52f96d297004 86 unsigned int cntr = 0; // Initialize LED-Red Counter
RyoheiHagimoto 0:52f96d297004 87 unsigned int cntg = 0; // Initialize LED-Green Counter
RyoheiHagimoto 0:52f96d297004 88 unsigned int cntb = 0; // Initialize LED-Blue Counter
RyoheiHagimoto 0:52f96d297004 89
RyoheiHagimoto 0:52f96d297004 90 int cntrd = 1; // Set LED-Red Counter Direction +1
RyoheiHagimoto 0:52f96d297004 91 int cntgd = 1; // Set LED-Green Counter Direction +1
RyoheiHagimoto 0:52f96d297004 92 int cntbd = 1; // Set LED-Blue Counter Direction +1
RyoheiHagimoto 0:52f96d297004 93
RyoheiHagimoto 0:52f96d297004 94 unsigned int flg_ledu_last = 0; // Initialize LED-User Flag (last value)
RyoheiHagimoto 0:52f96d297004 95 unsigned int flg_ledr_last = 0; // Initialize LED-Red Flag (last value)
RyoheiHagimoto 0:52f96d297004 96 unsigned int flg_ledg_last = 0; // Initialize LED-Green Flag (last value)
RyoheiHagimoto 0:52f96d297004 97 unsigned int flg_ledb_last = 0; // Initialize LED-Blue Flag (last value)
RyoheiHagimoto 0:52f96d297004 98
RyoheiHagimoto 0:52f96d297004 99 flipper.attach_us(&flip, 10000); // TickerTime Set 10ms
RyoheiHagimoto 0:52f96d297004 100
RyoheiHagimoto 0:52f96d297004 101 while(1) {
RyoheiHagimoto 0:52f96d297004 102
RyoheiHagimoto 0:52f96d297004 103 //----- LED User -----
RyoheiHagimoto 0:52f96d297004 104 if(flg_ledu_last != flg_ledu) { // Has LED-User Flag been Changed?
RyoheiHagimoto 0:52f96d297004 105 flg_ledu_last = flg_ledu; // Save current value
RyoheiHagimoto 0:52f96d297004 106 ledu =!ledu; // Invert LED-User
RyoheiHagimoto 0:52f96d297004 107 }
RyoheiHagimoto 0:52f96d297004 108
RyoheiHagimoto 0:52f96d297004 109 //----- LED Red -----
RyoheiHagimoto 0:52f96d297004 110 if(flg_ledr_last != flg_ledr) { // Has LED-Red Flag been Changed?
RyoheiHagimoto 0:52f96d297004 111 flg_ledr_last = flg_ledr; // Save current value
RyoheiHagimoto 0:52f96d297004 112
RyoheiHagimoto 0:52f96d297004 113 if(cntr == 0) cntrd = 1; // Set Direction(+1)
RyoheiHagimoto 0:52f96d297004 114 if(cntr >= 127) cntrd = -1; // Set Direction(-1)
RyoheiHagimoto 0:52f96d297004 115 cntr += cntrd; // Increment/Decrement Counter
RyoheiHagimoto 0:52f96d297004 116 ledr = (float)cntr / 128; // Set LED-Red Duty
RyoheiHagimoto 0:52f96d297004 117 }
RyoheiHagimoto 0:52f96d297004 118
RyoheiHagimoto 0:52f96d297004 119 //----- LED Green -----
RyoheiHagimoto 0:52f96d297004 120 if(flg_ledg_last != flg_ledg) { // Has LED-Green Flag been Changed?
RyoheiHagimoto 0:52f96d297004 121 flg_ledg_last = flg_ledg; // Save current value
RyoheiHagimoto 0:52f96d297004 122
RyoheiHagimoto 0:52f96d297004 123 if(cntg == 0) cntgd = 1; // Set Direction(+1)
RyoheiHagimoto 0:52f96d297004 124 if(cntg >= 127) cntgd = -1; // Set Direction(-1)
RyoheiHagimoto 0:52f96d297004 125 cntg += cntgd; // Increment/Decrement Counter
RyoheiHagimoto 0:52f96d297004 126 ledg = (float)cntg / 128; // Set LED-Green Duty
RyoheiHagimoto 0:52f96d297004 127 }
RyoheiHagimoto 0:52f96d297004 128
RyoheiHagimoto 0:52f96d297004 129 //----- LED Blue -----
RyoheiHagimoto 0:52f96d297004 130 if(flg_ledb_last != flg_ledb) { // Has LED-Blue Flag been Changed?
RyoheiHagimoto 0:52f96d297004 131 flg_ledb_last = flg_ledb; // Save current value
RyoheiHagimoto 0:52f96d297004 132
RyoheiHagimoto 0:52f96d297004 133 if(cntb == 0) cntbd = 1; // Set Direction(+1)
RyoheiHagimoto 0:52f96d297004 134 if(cntb >= 127) cntbd = -1; // Set Direction(-1)
RyoheiHagimoto 0:52f96d297004 135 cntb += cntbd; // Increment/Decrement Counter
RyoheiHagimoto 0:52f96d297004 136 ledb = (float)cntb / 128; // Set LED-Blue Duty
RyoheiHagimoto 0:52f96d297004 137 }
RyoheiHagimoto 0:52f96d297004 138
1050186 1:87af4f55c068 139 wait_us(1);
RyoheiHagimoto 0:52f96d297004 140 }
RyoheiHagimoto 0:52f96d297004 141 }