final project update
Dependencies: mbed
Fork of DISCO_SMT32L4_pwm_PB3 by
main.cpp
- Committer:
- carter7778
- Date:
- 2018-04-26
- Revision:
- 7:662af5825808
- Parent:
- 6:57bad75ec95b
File content as of revision 7:662af5825808:
#include "mbed.h" Serial pc(SERIAL_TX, SERIAL_RX); PwmOut mypwm(PWM_OUT); // PB3 - blue PwmOut red_pwm(LED1); // PE8-red PwmOut green_pwm(PB_6);// 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("blue 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("red 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("green 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("blue set to %.2f %%\n\r", mypwm.read() * 100); printf("red 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("blue set to %.2f %%\n\r", mypwm.read() * 100); printf("green 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("green set to %.2f %%\n\r", green_pwm.read() * 100); printf("red 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("blue set to %.2f %%\n\r", mypwm.read() * 100); printf("red pwm set to %.2f %%\n\r", red_pwm.read() * 100); printf("green 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 \n\r, Press '6' for Yellow \n\r, Press '5' for Blue/Green \n\r, Press '4' for Green \n\r, Press '3' for Red \n\r, Press '2 for Purple \n\r, Press '1' for Blue\n\r, '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 == '0') { mypwm=0; red_pwm=0; green_pwm=0; loop==0; printf("Program Completed. Have a nice day!\n\r"); printf("Program Completed. Have a nice day!\n\r"); printf("Program Completed. Have a nice day!\n\r"); printf("Program Completed. Have a nice day!\n\r"); printf("Program Completed. Have a nice day!\n\r"); } 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 \n\r, Press '6' for Yellow \n\r, Press '5' for Blue/Green \n\r, Press '4' for Green \n\r, Press '3' for Red \n\r, Press '2 for Purple \n\r, Press '1' for Blue\n\r, '0' to turn it OFF\n\r"); } } }