ecoder motor semaforo

Dependencies:   mbed ssd1306_library SLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "ssd1306.h"
00004 #include "SLCD.h"
00005 
00006 SLCD slcd;
00007 SSD1306 lcd (D7,D6);
00008 InterruptIn botoncito(PTC12);
00009 InterruptIn botoncito2(PTC3);
00010 InterruptIn botoncito3(D2);
00011 PwmOut pwm1(D5);
00012 PwmOut pwm2(D8);
00013 DigitalOut mo(D9);
00014 int contador=0,a=1,c=0,r,h,q;
00015 bool s=0,k=0;
00016 Serial pc(USBTX,USBRX);
00017 Semaphore one_slot(1);
00018 Mutex mutex1;
00019 
00020 
00021 Thread thread;
00022 Thread thread1;
00023 Thread thread2;
00024 Thread thread3;
00025 Thread thread4;
00026 
00027 
00028 void herra()
00029 {
00030     one_slot.release();
00031     one_slot.wait();
00032     r=r+1;
00033     one_slot.release();
00034 }
00035 
00036 void boton1()
00037 {
00038     one_slot.release();
00039     one_slot.wait();
00040     //one_slot.try_acquire();
00041     s=!s;
00042     mo=s;
00043     one_slot.release();
00044 }
00045 void boton2()
00046 {
00047     one_slot.release();
00048     one_slot.wait();
00049     NVIC_SystemReset ();
00050     one_slot.release();
00051 }
00052 
00053 
00054 
00055 void motor1()
00056 {
00057     while(1) {
00058         if(s==1) {
00059             c++;
00060             switch(c) {
00061                 case 1:
00062                     pwm2=0.3;
00063                     q=30;
00064                     mo=s;
00065                     Thread::wait(2000);
00066                     break;
00067                 case 2:
00068                     pwm2=0.5;
00069                     q=50;
00070                     mo=s;
00071                     Thread::wait(2000);
00072                     break;
00073                 case 3:
00074                     pwm2=0.7;
00075                     q=70;
00076                     mo=s;
00077                     Thread::wait(2000);
00078                     break;
00079                 case 4:
00080                     pwm2=1;
00081                     q=100;
00082                     mo=s;
00083                     Thread::wait(6000);
00084                     break;
00085                 case 5:
00086                     pwm2=0.7;
00087                     q=70;
00088                     mo=s;
00089                     Thread::wait(2000);
00090                     break;
00091                 case 6:
00092                     pwm2=0.5;
00093                     q=50;
00094                     mo=s;
00095                     Thread::wait(2000);
00096                     break;
00097                 case 7:
00098                     q=30;
00099                     pwm2=0.3;
00100                     mo=s;
00101                     Thread::wait(2000);
00102                     c=0;
00103                     break;
00104 
00105             }
00106         }
00107     }
00108 }
00109 
00110 void oled()
00111 {
00112     while(1) {
00113         
00114 
00115         if(k==0){
00116         lcd.init();
00117         lcd.speed (SSD1306::Fast);
00118         lcd.cls();
00119         lcd.locate (1,0);
00120         time_t seconds = time(NULL);
00121         lcd.printf("Fecha= %s", ctime(&seconds));
00122         lcd.locate (4,1);
00123         lcd.printf ("nivel: %d ",q);
00124         lcd.locate (5,1);
00125         lcd.printf ("rpm: %d ",h);
00126         lcd.redraw();
00127         
00128         }else if(k==1){
00129         lcd.init();
00130         lcd.speed (SSD1306::Fast);
00131         lcd.cls();    
00132         lcd.locate (2,1);
00133         lcd.printf ("error"); 
00134         lcd.locate (5,0);
00135         lcd.printf ("por favor revise el sistema"); 
00136           lcd.redraw();  
00137             }
00138         
00139         Thread::wait(300);
00140     }
00141 }
00142 void s7()
00143 {
00144     while(1) {
00145         if(s==1 && r==0){
00146             k=1;
00147             
00148         slcd.Home();
00149         slcd.putc('e');
00150         slcd.putc('r');
00151         r=0;
00152         Thread::wait(1000);
00153         slcd.clear();
00154         
00155             }else{
00156                 k=0;
00157         slcd.Home();
00158         h=r*60;
00159         h=h/4;
00160         slcd.printf("%d",h);
00161         r=0;
00162         Thread::wait(1000);
00163         slcd.clear();
00164                 
00165                 }
00166     }
00167 }
00168 void serial()
00169 {
00170     while(1) {
00171 
00172         one_slot.release();
00173         one_slot.wait();
00174 
00175         time_t seconds = time(NULL);
00176 
00177         char buffer2[32];
00178         strftime(buffer2, 32, "%m/%d/%y", localtime(&seconds));
00179         pc.printf("%s", buffer2);
00180 
00181         char buffer1[32];
00182         strftime(buffer1, 32, "%H:%M:%S", localtime(&seconds));
00183         pc.printf(" , %s", buffer1);
00184         if(k==1) {
00185             pc.printf(" , %d ciclo, %d rpm, error",q,h);
00186             
00187         } else {
00188             pc.printf(" , %d ciclo, %d rpm, ok",q,h);
00189         }
00190         pc.printf("\n\r");
00191         
00192         Thread::wait(100);
00193         one_slot.release();
00194 
00195     }
00196 }
00197 int main()
00198 {
00199 
00200     set_time(1593041944);
00201     botoncito.mode(PullUp);
00202     botoncito.fall(&boton1);
00203     botoncito2.mode(PullUp);
00204     botoncito2.fall(&boton2);
00205     botoncito3.mode(PullUp);
00206     botoncito3.fall(&herra);
00207 
00208     thread1.start(oled);
00209     thread2.start(s7);
00210     thread3.start(serial);
00211     thread4.start((motor1));
00212     thread4.join();
00213 }