final project update
Dependencies: mbed
Fork of DISCO_SMT32L4_pwm_PB3 by
main.cpp
- Committer:
- adamzakaria
- Date:
- 2018-04-25
- Revision:
- 6:57bad75ec95b
- Parent:
- 5:620125c08317
- Child:
- 7:662af5825808
File content as of revision 6:57bad75ec95b:
#include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX); PwmOut mypwm(PWM_OUT); // PB3 - blue PwmOut red_pwm(LED1); // PE8-red PwmOut green_pwm(JOYSTICK_LEFT);// PA1 GREEN #define UP 1 #define DOWN 2 void glow_blue(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { mypwm = mypwm + 0.1; if(mypwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { mypwm = mypwm - 0.1; if(mypwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", mypwm.read() * 100); } if(a =='d') { printf("Exiting loop\n\r"); loop=0; } } } void glow_red(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { red_pwm = red_pwm + 0.1; if(red_pwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { red_pwm = red_pwm - 0.1; if(red_pwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", red_pwm.read() * 100); } if(a =='d') { printf("Exiting loop"); loop=0; } } } void glow_green(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { green_pwm = green_pwm + 0.1; if(green_pwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { green_pwm = green_pwm - 0.1; if(green_pwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", green_pwm.read() * 100); } if(a =='d') { printf("Exiting loop\n\r"); loop=0; } } } void glow_purple(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { red_pwm = red_pwm + 0.1; mypwm = mypwm + 0.1; if(mypwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { red_pwm = red_pwm - 0.1; mypwm = mypwm - 0.1; if(mypwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", mypwm.read() * 100); printf("2nd pwm set to %.2f %%\n\r", red_pwm.read() * 100); } if(a =='d') { printf("Exiting loop"); loop=0; } } } void glow_yellow(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { green_pwm = green_pwm + 0.1; mypwm = mypwm + 0.1; if(mypwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { green_pwm = green_pwm - 0.1; mypwm = mypwm - 0.1; if(mypwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", mypwm.read() * 100); printf("2nd pwm set to %.2f %%\n\r", green_pwm.read() * 100); } if(a =='d') { printf("Exiting loop\n\r"); loop=0; } } } void glow_orange(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { red_pwm = red_pwm + 0.1; green_pwm = green_pwm + 0.1; if(red_pwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { red_pwm = red_pwm - 0.1; green_pwm = green_pwm - 0.1; if(mypwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", green_pwm.read() * 100); printf("2nd pwm set to %.2f %%\n\r", red_pwm.read() * 100); } if(a =='d') { printf("Exiting loop\n\r"); loop=0; } } } void glow_white(void){ int loop=1; uint8_t count_dir = UP; while(loop==1) { printf("Enter d to exit loop\n\r"); char a = pc.getc(); for(int i=0; i<10; i++) { if(count_dir == UP) { green_pwm = green_pwm + 0.1; red_pwm = red_pwm + 0.1; mypwm = mypwm + 0.1; if(mypwm == 1.00) { // if hit one, start counting down count_dir = DOWN; } } else if(count_dir == DOWN) { red_pwm = red_pwm - 0.1; mypwm = mypwm - 0.1; green_pwm = green_pwm - 0.1; if(mypwm == 0.0) { // if hit zero, start counting up count_dir = UP; } } printf("pwm set to %.2f %%\n\r", mypwm.read() * 100); printf("2nd pwm set to %.2f %%\n\r", red_pwm.read() * 100); printf("3rd pwm set to %.2f %%\n\r", green_pwm.read() * 100); } if(a =='d') { printf("Exiting loop\n\r"); loop=0; } } } int main() { uint8_t loop=1; uint8_t i; uint8_t count_dir; mypwm.period_ms(10); mypwm.pulsewidth_ms(1); red_pwm.period_ms(10); red_pwm.pulsewidth_ms(1); green_pwm.period_ms(10); green_pwm.pulsewidth_ms(1); count_dir = UP; pc.printf("Press '7' for White, Press '6' for Orange, Press '5' for Yellow, Press '4' for Green, Press '3' for Red, Press '2 for Purple, Press '1' for Blue, '0' to turn it OFF\n\r"); while(loop== 1) { /* mypwm=0; red_pwm=0; green_pwm=0; */ char c = pc.getc(); // Read hyperterminal if (c == 'o') { mypwm=0; red_pwm=0; green_pwm=0; } if (c == '1') { // ON while(c=='1') { glow_blue(); c=0; } } if (c == '2') { // ON while(c=='2') { glow_purple(); c=0; } } if (c == '3') { // ON while(c=='3') { glow_red(); c=0; } } if (c == '4') { // ON while(c=='4') { glow_green(); c=0; } } if (c == '5') { // ON while(c=='5') { glow_yellow(); c=0; } } if (c == '6') { // ON while(c=='6') { glow_orange(); c=0; } } if (c == '7') { // ON while(c=='7') { glow_white(); c=0; } } if( c== 'f') { loop=0; } if (c == 'h'){ pc.printf("Press '7' for White, Press '6' for Orange, Press '5' for Yellow, Press '4' for Green, Press '3' for Red, Press '2 for Purple, Press '1' for Blue, '0' to turn it OFF\n\r"); } } }