Automated Blinds Controller, can be activated with pushbutton, or at time set by a slider switch

Dependencies:   Motor TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Sweep the motor speed from full-speed reverse (-1.0) to full speed forwards (1.0)
00002  
00003 #include "mbed.h"
00004 #include "Motor.h"
00005 #include "TextLCD.h"
00006 
00007 Motor m(p21, p23, p28); // pwm, fwd, rev
00008 DigitalOut led(LED1);
00009 TextLCD lcd(p14, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
00010 DigitalOut high(p22);
00011 DigitalIn pb(p6);
00012 DigitalOut high2(p29); 
00013 AnalogIn slider(p15);
00014 Serial pc(USBTX, USBRX);
00015 void open();
00016 void close();
00017 void printTime(double);
00018 int state;
00019 char buffer[32];
00020 time_t seconds;
00021 double setTime;
00022 struct tm* timeInfo;
00023 
00024 int main() {
00025     set_time(    702000900    );  
00026     state = 1; //Start with blinds closed (0 is open)
00027     pb.mode(PullUp);
00028     int lastPb = pb;
00029     lcd.cls();
00030     wait(1);
00031     while (1){
00032         seconds = time(NULL);
00033         if (abs(slider-setTime)>.01){
00034             setTime = slider;
00035         }
00036         printTime(setTime);
00037         high = 1;
00038         high2 = 1;
00039         if (!pb){
00040             if (state == 1){
00041                 open();
00042                 led = 1;
00043                 pc.printf("open");
00044             }
00045             else {
00046                 close();
00047                 led = 0;
00048                 pc.printf("close");
00049             }
00050         }
00051         m.speed(0);
00052         wait(.2);
00053     }
00054 }
00055 
00056 void open() {
00057     m.speed(-1);
00058     wait(2.6);
00059     state = 0;
00060 }
00061 
00062 void close() {
00063     m.speed(1);
00064     wait(2.6);
00065     state = 1;
00066 }
00067 
00068 // Takes analog input from 0 to 1 and displays a time based on that
00069 void printTime(double in) {
00070     int totMins = in*1440; //maps to integer based on mins in a day
00071     int hrs = totMins/60;
00072     int mins = totMins%60;
00073     timeInfo = localtime(&seconds);
00074     lcd.locate(0,0);
00075     lcd.printf("%d:%d",timeInfo->tm_hour,timeInfo->tm_min);
00076     lcd.locate(0,1);
00077     lcd.printf("%d:%d",hrs,mins);
00078     if ((state == 1) && (hrs == timeInfo->tm_hour) && (mins == timeInfo->tm_min)){
00079         open();
00080     }
00081 }