.

Dependencies:   RTC-DS1307 mbed

Committer:
Karrots3
Date:
Mon Apr 18 16:08:43 2016 +0000
Revision:
0:3fa629914ea5
Child:
1:134750dc8585
boh

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