Craft Club / MasterSword

Dependencies:   TextLCD mbed

Fork of MIYABI_Drive by Nin_PTD_Mecha

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "math.h"
00004 PwmOut led1(LED1);
00005 PwmOut led2(LED2);
00006 PwmOut led3(LED3);
00007 PwmOut led4(LED4);
00008 
00009 PwmOut moter1(p21);
00010 PwmOut moter2(p22);
00011 DigitalIn sw1(p23);
00012 
00013 TextLCD lcd(p24, p26, p27, p28, p29, p30);      // rs, e, d0-d3
00014 
00015 void Change_Shine(bool BRIGHT){
00016 
00017     int i;
00018 
00019     int t_BecameShine;    
00020     int t_MaxShine;
00021     int t_MinShine;
00022     int t_NotShine;
00023     
00024     float rate;
00025     float rate_l;
00026     float Out1;
00027     float Out2;
00028     
00029     i = 0;
00030     
00031     Out1 = 0.0;
00032     Out2 = 0.0;
00033     t_BecameShine = 2000;
00034     t_MaxShine = 150;
00035     t_MinShine = 400;
00036     t_NotShine = 1200;
00037     rate = 0.7;
00038     rate_l = 0.4;
00039 
00040 
00041     if(BRIGHT){
00042         lcd.cls(); //clear
00043         lcd.locate(0,0); 
00044         lcd.printf("Shining now");
00045         for(i=0;i <= t_BecameShine ;i++){
00046             
00047             if(i <= t_MaxShine){
00048                 Out1 = (float)i / t_MaxShine;
00049                 Out2 = (float)i / t_MaxShine;
00050             }
00051             else if(i <= t_MinShine) {
00052                 Out1 = (float) 1 - (1 - rate_l)*(i - t_MaxShine)/(t_MinShine - t_MaxShine);
00053             }
00054             else{
00055                 Out1 = (float) 1 *(rate - rate_l)*(i - t_MinShine)/(t_BecameShine - t_MinShine) +rate_l;
00056             }
00057             
00058             lcd.locate(0,1); 
00059             lcd.printf("%f" ,Out1);
00060             
00061             moter1 = Out1;
00062             moter2 = Out1;
00063             wait(0.001);
00064         }
00065         moter1 = rate;
00066         moter2 = rate;
00067     }
00068     else{
00069         lcd.cls(); //clear
00070         lcd.locate(0,0); 
00071         lcd.printf("Not shining now");
00072         moter1 = rate;
00073         moter2 = rate;
00074         for(i=0;i<=t_NotShine;i++){
00075             Out1 = float(rate*(t_NotShine - i)/t_NotShine);
00076             Out2 = float(rate*(t_NotShine - i)/t_NotShine);
00077             lcd.locate(0,1); 
00078             lcd.printf("%f" ,Out1);
00079             moter1 = Out1;
00080             moter2 = Out2;
00081             wait(0.001);
00082         }
00083         moter1 = 0;
00084         moter2 = 0;
00085     }
00086 }
00087 
00088 int main() {
00089     
00090     int s = 0;        
00091     bool BRIGHT = false;
00092     
00093     sw1.mode( PullUp );
00094     
00095     moter1 = 0.0;
00096     moter2 = 0.0;
00097     moter1.period(0.001);
00098     moter2.period(0.001);
00099 
00100     //初期設定を光らないモードに設定
00101     lcd.cls(); //clear
00102     lcd.locate(0,0); 
00103     lcd.printf("Not shining now");
00104 
00105     while(1){
00106         //トリガのみのカウント
00107         //チャタリング防止のためタイマ有り
00108         //一度OFFになったことを検知するステータスフラグ、点灯消灯フラグを用い
00109         //1回だけ点灯消灯の関数を呼び出す。
00110         if(sw1 == 0 and s == 0) {
00111             s = 1;
00112             led1 = 1;
00113             BRIGHT = !BRIGHT;
00114             Change_Shine(BRIGHT);
00115             wait(0.001);
00116         }
00117         else if(sw1 == 1) {
00118             s = 0;
00119             led1 = 0;
00120         }
00121         wait(0.1);        
00122     }   
00123     
00124 }