.

Dependencies:   RTC-DS1307 mbed

Committer:
Karrots3
Date:
Mon Apr 18 16:30:52 2016 +0000
Revision:
1:134750dc8585
Parent:
0:3fa629914ea5
Child:
2:d3b43d747191
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Karrots3 0:3fa629914ea5 1 #include "mbed.h"
Karrots3 0:3fa629914ea5 2 #include "Servo.h"
Karrots3 0:3fa629914ea5 3 #include "Rtc_Ds1307.h"
Karrots3 0:3fa629914ea5 4 #include "note.h"
Karrots3 0:3fa629914ea5 5
Karrots3 0:3fa629914ea5 6 DigitalIn bottone1(p23);
Karrots3 0:3fa629914ea5 7 DigitalOut ledR(p30);
Karrots3 0:3fa629914ea5 8 DigitalOut ledG(p29);
Karrots3 0:3fa629914ea5 9 DigitalOut ledB(p28);
Karrots3 0:3fa629914ea5 10 AnalogIn sensore_lum(p20);
Karrots3 0:3fa629914ea5 11 AnalogIn potenziometro(p15);
Karrots3 0:3fa629914ea5 12 PwmOut piezo(p20);
Karrots3 0:3fa629914ea5 13
Karrots3 0:3fa629914ea5 14 Serial pc(USBTX, USBRX, "pc");
Karrots3 0:3fa629914ea5 15
Karrots3 0:3fa629914ea5 16 bool flag1=0, flag2=0;
Karrots3 0:3fa629914ea5 17 int ultimo_giorno=0;
Karrots3 0:3fa629914ea5 18 double temp_lum;
Karrots3 0:3fa629914ea5 19 int orario;
Karrots3 0:3fa629914ea5 20 long long int c;
Karrots3 0:3fa629914ea5 21 /*
Karrots3 0:3fa629914ea5 22 struct orario {
Karrots3 0:3fa629914ea5 23 int ora;
Karrots3 0:3fa629914ea5 24 int minuti;
Karrots3 0:3fa629914ea5 25 };
Karrots3 0:3fa629914ea5 26 */
Karrots3 0:3fa629914ea5 27
Karrots3 0:3fa629914ea5 28 void setOrario_Pastiglia();
Karrots3 0:3fa629914ea5 29 void suono(int frequenza, int beat);
Karrots3 0:3fa629914ea5 30 void scesa_della_pastiglia();
Karrots3 0:3fa629914ea5 31 void seriale(Rtc_Ds1307::Time_rtc time);
Karrots3 0:3fa629914ea5 32
Karrots3 0:3fa629914ea5 33 int main()
Karrots3 0:3fa629914ea5 34 {
Karrots3 0:3fa629914ea5 35 Rtc_Ds1307::Time_rtc time= {};
Karrots3 0:3fa629914ea5 36 setOrario_Pastiglia();
Karrots3 0:3fa629914ea5 37 while(1) {
Karrots3 0:3fa629914ea5 38 if(ultimo_giorno!=time.date) { //if ultimo giorno è oggi
Karrots3 0:3fa629914ea5 39 if(orario == time.hour) { //if è l'ora della pappa
Karrots3 0:3fa629914ea5 40 scesa_della_pastiglia();
Karrots3 0:3fa629914ea5 41 ultimo_giorno=time.date;
Karrots3 0:3fa629914ea5 42 } else if(bottone1 && time.hour==(orario-1) && time.min>=30) { //30 min prima
Karrots3 0:3fa629914ea5 43 scesa_della_pastiglia();
Karrots3 0:3fa629914ea5 44 ultimo_giorno=time.date;
Karrots3 0:3fa629914ea5 45 } else if(bottone1 && time.hour>orario) { //l'ora è gia passata ma devo ancora prenderlo
Karrots3 0:3fa629914ea5 46 scesa_della_pastiglia();
Karrots3 0:3fa629914ea5 47 ultimo_giorno=time.date;
Karrots3 0:3fa629914ea5 48 } else {
Karrots3 0:3fa629914ea5 49 if(bottone1) { //l'ora deve ancora venire e premo bottone
Karrots3 0:3fa629914ea5 50 ledR=1;
Karrots3 0:3fa629914ea5 51 suono(NOTE_B4,1);
Karrots3 0:3fa629914ea5 52 wait(1);
Karrots3 0:3fa629914ea5 53 ledR=0;
Karrots3 0:3fa629914ea5 54 }
Karrots3 0:3fa629914ea5 55 }
Karrots3 0:3fa629914ea5 56 } else { //gia fatto oggi e premo bottone
Karrots3 0:3fa629914ea5 57 if(bottone1) {
Karrots3 0:3fa629914ea5 58 ledR=1;
Karrots3 0:3fa629914ea5 59 suono(NOTE_B4,1);
Karrots3 0:3fa629914ea5 60 }
Karrots3 0:3fa629914ea5 61 }
Karrots3 0:3fa629914ea5 62 }//while(1)
Karrots3 0:3fa629914ea5 63 }//main
Karrots3 0:3fa629914ea5 64
Karrots3 0:3fa629914ea5 65 void scesa_della_pastiglia()
Karrots3 0:3fa629914ea5 66 {
Karrots3 0:3fa629914ea5 67 ledB=1;
Karrots3 0:3fa629914ea5 68 suono(440,1);
Karrots3 0:3fa629914ea5 69 do {
Karrots3 0:3fa629914ea5 70 /*pastiglia*/
Karrots3 0:3fa629914ea5 71 c++;
Karrots3 0:3fa629914ea5 72 if(bottone1) {
Karrots3 0:3fa629914ea5 73 flag1=1;
Karrots3 0:3fa629914ea5 74 /////funzione del servo che gira per far scendere la pastiglia
Karrots3 0:3fa629914ea5 75 ledG=1;
Karrots3 0:3fa629914ea5 76 ledB=0;
Karrots3 0:3fa629914ea5 77 wait_ms(10);
Karrots3 0:3fa629914ea5 78 temp_lum=sensore_lum;
Karrots3 0:3fa629914ea5 79 c=0;
Karrots3 0:3fa629914ea5 80 } else if(c>0 && c%1000==0) {
Karrots3 0:3fa629914ea5 81 suono(NOTE_D5, 1);
Karrots3 0:3fa629914ea5 82 }
Karrots3 0:3fa629914ea5 83 } while(!flag1) ;
Karrots3 0:3fa629914ea5 84 flag1=0;
Karrots3 0:3fa629914ea5 85 do {
Karrots3 0:3fa629914ea5 86 c++;
Karrots3 0:3fa629914ea5 87 if(sensore_lum<=temp_lum/20) {
Karrots3 0:3fa629914ea5 88 flag1=1;
Karrots3 0:3fa629914ea5 89 ledG=0;
Karrots3 0:3fa629914ea5 90 suono(NOTE_A3,1);
Karrots3 0:3fa629914ea5 91 } else if(c>0 && c%1000==0) {
Karrots3 0:3fa629914ea5 92 suono(NOTE_D5, 1);
Karrots3 0:3fa629914ea5 93 }
Karrots3 0:3fa629914ea5 94 wait_ms(10);
Karrots3 0:3fa629914ea5 95 } while(!flag1);
Karrots3 0:3fa629914ea5 96 }
Karrots3 0:3fa629914ea5 97
Karrots3 0:3fa629914ea5 98 void setOrario_Pastiglia()
Karrots3 0:3fa629914ea5 99 {
Karrots3 0:3fa629914ea5 100 bool pot=0;
Karrots3 0:3fa629914ea5 101 bool seri=0;
Karrots3 0:3fa629914ea5 102 bool Fpot=0;
Karrots3 0:3fa629914ea5 103
Karrots3 0:3fa629914ea5 104 do {
Karrots3 0:3fa629914ea5 105 if(bottone1)
Karrots3 0:3fa629914ea5 106 pot=1;
Karrots3 0:3fa629914ea5 107 wait_ms(1);
Karrots3 0:3fa629914ea5 108 } while(!pot);
Karrots3 0:3fa629914ea5 109
Karrots3 0:3fa629914ea5 110 if(pot) {
Karrots3 0:3fa629914ea5 111 wait(0.2);
Karrots3 0:3fa629914ea5 112 ledR=1;
Karrots3 0:3fa629914ea5 113 ledG=1;
Karrots3 0:3fa629914ea5 114 ledB=1;
Karrots3 0:3fa629914ea5 115 suono(NOTE_A3, 2);
Karrots3 0:3fa629914ea5 116
Karrots3 0:3fa629914ea5 117 do {
Karrots3 0:3fa629914ea5 118 if(bottone1) {
Karrots3 0:3fa629914ea5 119 ledR=0;
Karrots3 0:3fa629914ea5 120 ledG=0;
Karrots3 0:3fa629914ea5 121 ledB=0;
Karrots3 0:3fa629914ea5 122 orario=(potenziometro/1024)*24;
Karrots3 0:3fa629914ea5 123 Fpot=1;
Karrots3 0:3fa629914ea5 124 suono(NOTE_A3, 1);
Karrots3 0:3fa629914ea5 125 }
Karrots3 0:3fa629914ea5 126 } while(!Fpot);
Karrots3 0:3fa629914ea5 127
Karrots3 0:3fa629914ea5 128 }
Karrots3 0:3fa629914ea5 129 }
Karrots3 0:3fa629914ea5 130
Karrots3 0:3fa629914ea5 131 void suono(int frequenza, int beat)
Karrots3 0:3fa629914ea5 132 {
Karrots3 0:3fa629914ea5 133 piezo.period(1.0 / frequenza);
Karrots3 0:3fa629914ea5 134 piezo.write(0.5);
Karrots3 0:3fa629914ea5 135 wait(1.0 / beat);
Karrots3 0:3fa629914ea5 136 piezo.write(0);
Karrots3 0:3fa629914ea5 137 wait(0.05);
Karrots3 0:3fa629914ea5 138 }
Karrots3 0:3fa629914ea5 139
Karrots3 0:3fa629914ea5 140
Karrots3 0:3fa629914ea5 141
Karrots3 0:3fa629914ea5 142 void seriale(Rtc_Ds1307::Time_rtc time) {
Karrots3 0:3fa629914ea5 143 int c;
Karrots3 0:3fa629914ea5 144
Karrots3 0:3fa629914ea5 145
Karrots3 0:3fa629914ea5 146 pc.printf("**********************************\n");
Karrots3 0:3fa629914ea5 147 pc.printf("* Menu : *\n");
Karrots3 0:3fa629914ea5 148 pc.printf("* 1 - imposta ora orologio *\n");
Karrots3 0:3fa629914ea5 149 pc.printf("* 2 - imposta ora pastiglia *\n");
Karrots3 0:3fa629914ea5 150 pc.printf("* 3 - mostra settaggi *\n");
Karrots3 0:3fa629914ea5 151 pc.scanf("%d", &c);
Karrots3 0:3fa629914ea5 152
Karrots3 0:3fa629914ea5 153 switch(c) {
Karrots3 0:3fa629914ea5 154 case 1:
Karrots3 0:3fa629914ea5 155 pc.printf("Giorno (0..31)");
Karrots3 0:3fa629914ea5 156 pc.scanf("%d", &time.date);
Karrots3 0:3fa629914ea5 157 pc.printf("Mese (1..12)");
Karrots3 0:3fa629914ea5 158 pc.scanf("%d", &time.mon);
Karrots3 0:3fa629914ea5 159 pc.printf("Anno ");
Karrots3 0:3fa629914ea5 160 pc.scanf("%d", &time.year);
Karrots3 0:3fa629914ea5 161 pc.printf("Ore (0..23)");
Karrots3 0:3fa629914ea5 162 pc.scanf("%d", &time.hour);
Karrots3 0:3fa629914ea5 163 pc.printf("Minuti (0..59)");
Karrots3 0:3fa629914ea5 164 pc.scanf("%d", &time.min);
Karrots3 0:3fa629914ea5 165 pc.printf("Secondi (0..59)");
Karrots3 0:3fa629914ea5 166 pc.scanf("%d", &time.sec);
Karrots3 0:3fa629914ea5 167 break;
Karrots3 0:3fa629914ea5 168 case 2:
Karrots3 0:3fa629914ea5 169 pc.printf("Orario pastiglia");
Karrots3 0:3fa629914ea5 170 pc.scanf("%d", &orario);
Karrots3 0:3fa629914ea5 171 break;
Karrots3 0:3fa629914ea5 172 case 3:
Karrots3 0:3fa629914ea5 173 pc.printf("Ora sono le : %02d:%02d:%02d del %02d/%02d/%04d \n", time.hour, time.min, time.sec, time.mon, time.date, time.year);
Karrots3 0:3fa629914ea5 174 break;
Karrots3 0:3fa629914ea5 175 }
Karrots3 0:3fa629914ea5 176 }