DC Motor control using current time

Dependencies:   4DGL-uLCD-SE EthernetNetIf Motor NTPClient_NetServices mbed

Fork of Internet_Nokia_LCD_Clock by jim hamblen

Committer:
nturakhia
Date:
Mon Mar 24 14:55:59 2014 +0000
Revision:
2:944077f530be
Parent:
Internet_LCD_Clock.cpp@1:ebe96d5a5825
Child:
3:dc6edb51a46e
Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:da7f4b6d2b7c 1 #include "mbed.h"
4180_1 0:da7f4b6d2b7c 2 #include "EthernetNetIf.h"
4180_1 0:da7f4b6d2b7c 3 #include "NTPClient.h"
4180_1 0:da7f4b6d2b7c 4 #include "NokiaLCD.h"
nturakhia 1:ebe96d5a5825 5 #include "Motor.h"
nturakhia 1:ebe96d5a5825 6 #include "uLCD_4DGL.h"
nturakhia 1:ebe96d5a5825 7 #include<string>
nturakhia 1:ebe96d5a5825 8 #include<iostream>
nturakhia 1:ebe96d5a5825 9 #include<sstream>
nturakhia 1:ebe96d5a5825 10 using namespace std;
nturakhia 1:ebe96d5a5825 11
nturakhia 1:ebe96d5a5825 12 Serial pc(USBTX,USBRX);
nturakhia 1:ebe96d5a5825 13 Motor m(p22, p28, p21); // pwm, fwd, rev
nturakhia 1:ebe96d5a5825 14 DigitalIn pin_up(p13);
nturakhia 1:ebe96d5a5825 15 DigitalIn pin_down(p14);
nturakhia 1:ebe96d5a5825 16 DigitalIn pin_menu(p15);
nturakhia 1:ebe96d5a5825 17 DigitalIn pin_enter(p16);
nturakhia 1:ebe96d5a5825 18 uLCD_4DGL lcd(p9, p10, p11); // create a global lcd object
4180_1 0:da7f4b6d2b7c 19 // Internet of Things clock example: LCD time is set via internet NTP time server
4180_1 0:da7f4b6d2b7c 20 EthernetNetIf eth;
nturakhia 1:ebe96d5a5825 21 DigitalOut led(LED1);
4180_1 0:da7f4b6d2b7c 22 NTPClient ntp;
nturakhia 1:ebe96d5a5825 23 DigitalIn pin(p20);
4180_1 0:da7f4b6d2b7c 24
4180_1 0:da7f4b6d2b7c 25 int main() {
nturakhia 1:ebe96d5a5825 26 pc.printf("beginning");
nturakhia 1:ebe96d5a5825 27 m.speed(1);
nturakhia 1:ebe96d5a5825 28 pin_up.mode(PullUp);
nturakhia 1:ebe96d5a5825 29 pin_down.mode(PullUp);
nturakhia 1:ebe96d5a5825 30 pin_menu.mode(PullUp);
nturakhia 1:ebe96d5a5825 31 pin_enter.mode(PullUp);
nturakhia 1:ebe96d5a5825 32
4180_1 0:da7f4b6d2b7c 33 //system time structure
4180_1 0:da7f4b6d2b7c 34 time_t ctTime;
4180_1 0:da7f4b6d2b7c 35 char *time_str;
4180_1 0:da7f4b6d2b7c 36 //clear LCD
4180_1 0:da7f4b6d2b7c 37 lcd.cls();
4180_1 0:da7f4b6d2b7c 38 lcd.locate(0,2);
4180_1 0:da7f4b6d2b7c 39 // lcd.printf prints to LCD display;
4180_1 0:da7f4b6d2b7c 40 lcd.printf("Get IP addr...");
4180_1 0:da7f4b6d2b7c 41 EthernetErr ethErr = eth.setup();
4180_1 0:da7f4b6d2b7c 42 //Get an Internet IP address using DHCP
4180_1 0:da7f4b6d2b7c 43 if (ethErr) {
4180_1 0:da7f4b6d2b7c 44 //error or timeout getting an IP address
4180_1 0:da7f4b6d2b7c 45 lcd.cls();
4180_1 0:da7f4b6d2b7c 46 lcd.locate(0,2);
4180_1 0:da7f4b6d2b7c 47 lcd.printf("Net Error %d",ethErr);
4180_1 0:da7f4b6d2b7c 48 return -1;
4180_1 0:da7f4b6d2b7c 49 }
4180_1 0:da7f4b6d2b7c 50 lcd.locate(0,3);
4180_1 0:da7f4b6d2b7c 51 lcd.printf("Reading Time...");
4180_1 0:da7f4b6d2b7c 52 //specify time server URL
4180_1 0:da7f4b6d2b7c 53 Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
4180_1 0:da7f4b6d2b7c 54 //Read time from server
4180_1 0:da7f4b6d2b7c 55 ntp.setTime(server);
4180_1 0:da7f4b6d2b7c 56 lcd.locate(0,4);
4180_1 0:da7f4b6d2b7c 57 lcd.printf("Time set");
4180_1 0:da7f4b6d2b7c 58 //Delay for human time to read LCD display
4180_1 0:da7f4b6d2b7c 59 wait(1);
nturakhia 1:ebe96d5a5825 60 float motor_speed=1.0;
nturakhia 1:ebe96d5a5825 61 int start_time_hr=14;
nturakhia 1:ebe96d5a5825 62 int start_time_min=0;
nturakhia 1:ebe96d5a5825 63 int end_time_hr=21;
nturakhia 1:ebe96d5a5825 64 int end_time_min=0;
4180_1 0:da7f4b6d2b7c 65 while (1) {
4180_1 0:da7f4b6d2b7c 66 // loop and periodically update the LCD's time display
nturakhia 1:ebe96d5a5825 67
nturakhia 1:ebe96d5a5825 68 if(pin_menu==0)
nturakhia 1:ebe96d5a5825 69 {
nturakhia 1:ebe96d5a5825 70 lcd.cls();
nturakhia 1:ebe96d5a5825 71 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 72 lcd.printf("Start time:\n%d hour ",start_time_hr);
nturakhia 1:ebe96d5a5825 73 while(1)
nturakhia 1:ebe96d5a5825 74 {
nturakhia 1:ebe96d5a5825 75 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 76 if(pin_up==0)
nturakhia 1:ebe96d5a5825 77 {
nturakhia 1:ebe96d5a5825 78
nturakhia 1:ebe96d5a5825 79 start_time_hr=(start_time_hr+1) % 24;
nturakhia 1:ebe96d5a5825 80 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 81 lcd.printf("%d hour ",start_time_hr);
nturakhia 1:ebe96d5a5825 82 wait(2);
nturakhia 1:ebe96d5a5825 83 }
nturakhia 1:ebe96d5a5825 84 else if(pin_down==0)
nturakhia 1:ebe96d5a5825 85 {
nturakhia 1:ebe96d5a5825 86 if(start_time_hr==0)
nturakhia 1:ebe96d5a5825 87 start_time_hr=23;
nturakhia 1:ebe96d5a5825 88 else
nturakhia 1:ebe96d5a5825 89 start_time_hr=(start_time_hr-1) % 24;
nturakhia 1:ebe96d5a5825 90 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 91 lcd.printf("%d hour ",start_time_hr);
nturakhia 1:ebe96d5a5825 92 wait(2);
nturakhia 1:ebe96d5a5825 93 }
nturakhia 1:ebe96d5a5825 94 else if(pin_enter==0)
nturakhia 1:ebe96d5a5825 95 {
nturakhia 1:ebe96d5a5825 96 wait(2);
nturakhia 1:ebe96d5a5825 97 break;
nturakhia 1:ebe96d5a5825 98 }
nturakhia 1:ebe96d5a5825 99 }
nturakhia 1:ebe96d5a5825 100 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 101 lcd.printf("%d min ",start_time_min);
nturakhia 1:ebe96d5a5825 102 while(1)
nturakhia 1:ebe96d5a5825 103 {
nturakhia 1:ebe96d5a5825 104 if(pin_up==0)
nturakhia 1:ebe96d5a5825 105 {
nturakhia 1:ebe96d5a5825 106 start_time_min=(start_time_min+1) % 60;
nturakhia 1:ebe96d5a5825 107 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 108 lcd.printf("%d min ",start_time_min);
nturakhia 1:ebe96d5a5825 109 wait(2);
nturakhia 1:ebe96d5a5825 110 }
nturakhia 1:ebe96d5a5825 111 else if(pin_down==0)
nturakhia 1:ebe96d5a5825 112 {
nturakhia 1:ebe96d5a5825 113 if(start_time_min==0)
nturakhia 1:ebe96d5a5825 114 start_time_min=59;
nturakhia 1:ebe96d5a5825 115 else
nturakhia 1:ebe96d5a5825 116 start_time_min=(start_time_min-1) % 60;
nturakhia 1:ebe96d5a5825 117 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 118 lcd.printf("%d min ",start_time_min);
nturakhia 1:ebe96d5a5825 119 wait(2);
nturakhia 1:ebe96d5a5825 120 }
nturakhia 1:ebe96d5a5825 121 else if(pin_enter==0)
nturakhia 1:ebe96d5a5825 122 {
nturakhia 1:ebe96d5a5825 123 wait(2);
nturakhia 1:ebe96d5a5825 124 break;
nturakhia 1:ebe96d5a5825 125 }
nturakhia 1:ebe96d5a5825 126 }
nturakhia 1:ebe96d5a5825 127 lcd.cls();
nturakhia 1:ebe96d5a5825 128 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 129 lcd.printf("End time:\n%d hour",end_time_hr);
nturakhia 1:ebe96d5a5825 130 while(1)
nturakhia 1:ebe96d5a5825 131 {
nturakhia 1:ebe96d5a5825 132 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 133 if(pin_up==0)
nturakhia 1:ebe96d5a5825 134 {
nturakhia 1:ebe96d5a5825 135 end_time_hr=(end_time_hr+1) % 24;
nturakhia 1:ebe96d5a5825 136 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 137 lcd.printf("%d hour ",end_time_hr);
nturakhia 1:ebe96d5a5825 138 wait(2);
nturakhia 1:ebe96d5a5825 139 }
nturakhia 1:ebe96d5a5825 140 else if(pin_down==0)
nturakhia 1:ebe96d5a5825 141 {
nturakhia 1:ebe96d5a5825 142 if(end_time_hr==0)
nturakhia 1:ebe96d5a5825 143 end_time_hr=23;
nturakhia 1:ebe96d5a5825 144 else
nturakhia 1:ebe96d5a5825 145 end_time_hr=(end_time_hr-1) % 24;
nturakhia 1:ebe96d5a5825 146 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 147 lcd.printf("%d hour ",end_time_hr);
nturakhia 1:ebe96d5a5825 148 wait(2);
nturakhia 1:ebe96d5a5825 149 }
nturakhia 1:ebe96d5a5825 150 else if(pin_enter==0)
nturakhia 1:ebe96d5a5825 151 {
nturakhia 1:ebe96d5a5825 152 wait(2);
nturakhia 1:ebe96d5a5825 153 break;
nturakhia 1:ebe96d5a5825 154 }
nturakhia 1:ebe96d5a5825 155 }
nturakhia 1:ebe96d5a5825 156 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 157 lcd.printf("%d min ",end_time_min);
nturakhia 1:ebe96d5a5825 158 while(1)
nturakhia 1:ebe96d5a5825 159 {
nturakhia 1:ebe96d5a5825 160 if(pin_up==0)
nturakhia 1:ebe96d5a5825 161 {
nturakhia 1:ebe96d5a5825 162 end_time_min=(end_time_min+1) % 60;
nturakhia 1:ebe96d5a5825 163 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 164 lcd.printf("%d min ",end_time_min);
nturakhia 1:ebe96d5a5825 165 wait(2);
nturakhia 1:ebe96d5a5825 166 }
nturakhia 1:ebe96d5a5825 167 else if(pin_down==0)
nturakhia 1:ebe96d5a5825 168 {
nturakhia 1:ebe96d5a5825 169 if(end_time_min==0)
nturakhia 1:ebe96d5a5825 170 end_time_min=59;
nturakhia 1:ebe96d5a5825 171 else
nturakhia 1:ebe96d5a5825 172 end_time_min=(end_time_min-1) % 60;
nturakhia 1:ebe96d5a5825 173 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 174 lcd.printf("%d min ",end_time_min);
nturakhia 1:ebe96d5a5825 175 wait(2);
nturakhia 1:ebe96d5a5825 176 }
nturakhia 1:ebe96d5a5825 177 else if(pin_enter==0)
nturakhia 1:ebe96d5a5825 178 {
nturakhia 1:ebe96d5a5825 179 wait(2);
nturakhia 1:ebe96d5a5825 180 break;
nturakhia 1:ebe96d5a5825 181 }
nturakhia 1:ebe96d5a5825 182 }
nturakhia 1:ebe96d5a5825 183 lcd.cls();
nturakhia 1:ebe96d5a5825 184 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 185 lcd.printf("Motor speed: \n%2.1f (normalized)",motor_speed);
nturakhia 1:ebe96d5a5825 186 while(1)
nturakhia 1:ebe96d5a5825 187 {
nturakhia 1:ebe96d5a5825 188 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 189 if(pin_up==0)
nturakhia 1:ebe96d5a5825 190 {
nturakhia 1:ebe96d5a5825 191 if(motor_speed==1.0)
nturakhia 1:ebe96d5a5825 192 motor_speed=1.0;
nturakhia 1:ebe96d5a5825 193 else
nturakhia 1:ebe96d5a5825 194 motor_speed=motor_speed+0.1;
nturakhia 1:ebe96d5a5825 195
nturakhia 1:ebe96d5a5825 196 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 197 lcd.printf("%2.1f (normalized)",motor_speed);
nturakhia 1:ebe96d5a5825 198 wait(2);
nturakhia 1:ebe96d5a5825 199 }
nturakhia 1:ebe96d5a5825 200 else if(pin_down==0)
nturakhia 1:ebe96d5a5825 201 {
nturakhia 1:ebe96d5a5825 202 if(motor_speed==0.0)
nturakhia 1:ebe96d5a5825 203 motor_speed=0.0;
nturakhia 1:ebe96d5a5825 204 else
nturakhia 1:ebe96d5a5825 205 motor_speed=motor_speed-0.1;
nturakhia 1:ebe96d5a5825 206
nturakhia 1:ebe96d5a5825 207 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 208 lcd.printf("%2.1f (normalized)",motor_speed);
nturakhia 1:ebe96d5a5825 209 wait(2);
nturakhia 1:ebe96d5a5825 210 }
nturakhia 1:ebe96d5a5825 211 else if(pin_enter==0)
nturakhia 1:ebe96d5a5825 212 {
nturakhia 1:ebe96d5a5825 213 wait(2);
nturakhia 1:ebe96d5a5825 214 break;
nturakhia 1:ebe96d5a5825 215 }
nturakhia 1:ebe96d5a5825 216 }
nturakhia 1:ebe96d5a5825 217 }
nturakhia 1:ebe96d5a5825 218
nturakhia 1:ebe96d5a5825 219
nturakhia 1:ebe96d5a5825 220 led=pin;
4180_1 0:da7f4b6d2b7c 221 lcd.cls();
4180_1 0:da7f4b6d2b7c 222 ctTime = time(NULL);
4180_1 0:da7f4b6d2b7c 223 lcd.locate(0,1);
nturakhia 1:ebe96d5a5825 224 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 225 lcd.printf("Current time(UTC):");
nturakhia 1:ebe96d5a5825 226 lcd.color(GREEN);
4180_1 0:da7f4b6d2b7c 227 lcd.locate(0,3);
4180_1 0:da7f4b6d2b7c 228 time_str = ctime(&ctTime);
4180_1 0:da7f4b6d2b7c 229 time_str[strlen(time_str)-1] = 0;
nturakhia 1:ebe96d5a5825 230 lcd.printf("%s\n", time_str);
nturakhia 1:ebe96d5a5825 231 string s(time_str);
nturakhia 1:ebe96d5a5825 232 int ind=s.find(":");
nturakhia 1:ebe96d5a5825 233 char* bh;
nturakhia 1:ebe96d5a5825 234 bh[0]=s[ind-2];
nturakhia 1:ebe96d5a5825 235 bh[1]=s[ind-1];
nturakhia 1:ebe96d5a5825 236 bh[2]=0;
nturakhia 1:ebe96d5a5825 237 int hour=atoi(bh);
nturakhia 1:ebe96d5a5825 238 char* b;
nturakhia 1:ebe96d5a5825 239 b[0]=s[ind+1];
nturakhia 1:ebe96d5a5825 240 b[1]=s[ind+2];
nturakhia 1:ebe96d5a5825 241 b[2]=0;
nturakhia 1:ebe96d5a5825 242 int min=atoi(b);
nturakhia 1:ebe96d5a5825 243 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 244 lcd.printf("\nMotor run time:\n");
nturakhia 1:ebe96d5a5825 245 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 246 lcd.printf("%d:%d to %d:%d (UTC)\n \n",start_time_hr,start_time_min,end_time_hr,end_time_min);
nturakhia 1:ebe96d5a5825 247 time(&ctTime);
nturakhia 1:ebe96d5a5825 248 struct tm *tmp = gmtime(&ctTime);
nturakhia 1:ebe96d5a5825 249 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 250 pc.printf("hour: %d min: %d",hour,min);
nturakhia 1:ebe96d5a5825 251 if((hour>start_time_hr && hour<end_time_hr))
nturakhia 1:ebe96d5a5825 252 {
nturakhia 1:ebe96d5a5825 253 m.speed(motor_speed);
nturakhia 1:ebe96d5a5825 254 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 255 lcd.printf("Motor running \n");
nturakhia 1:ebe96d5a5825 256 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 257 lcd.printf("Speed=%2.1f\n",motor_speed);
nturakhia 1:ebe96d5a5825 258 }
nturakhia 1:ebe96d5a5825 259 else if((hour==start_time_hr && hour==end_time_hr && min>=start_time_min && min<=end_time_min))
nturakhia 1:ebe96d5a5825 260 {
nturakhia 1:ebe96d5a5825 261 m.speed(motor_speed);
nturakhia 1:ebe96d5a5825 262 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 263 lcd.printf("Motor running \n");
nturakhia 1:ebe96d5a5825 264 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 265 lcd.printf("Speed=%2.1f\n",motor_speed);
nturakhia 1:ebe96d5a5825 266 }
nturakhia 1:ebe96d5a5825 267 else if(hour==start_time_hr && hour<end_time_hr && min>=start_time_min)
nturakhia 1:ebe96d5a5825 268 {
nturakhia 1:ebe96d5a5825 269 m.speed(motor_speed);
nturakhia 1:ebe96d5a5825 270 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 271 lcd.printf("Motor running \n");
nturakhia 1:ebe96d5a5825 272 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 273 lcd.printf("Speed=%2.1f\n",motor_speed);
nturakhia 1:ebe96d5a5825 274 }
nturakhia 1:ebe96d5a5825 275 else if(hour==end_time_hr && hour>start_time_hr && min<=end_time_min)
nturakhia 1:ebe96d5a5825 276 {
nturakhia 1:ebe96d5a5825 277 m.speed(motor_speed);
nturakhia 1:ebe96d5a5825 278 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 279 lcd.printf("Motor running \n");
nturakhia 1:ebe96d5a5825 280 lcd.color(GREEN);
nturakhia 1:ebe96d5a5825 281 lcd.printf("Speed=%2.1f\n",motor_speed);
nturakhia 1:ebe96d5a5825 282 }
nturakhia 1:ebe96d5a5825 283 else
nturakhia 1:ebe96d5a5825 284 {
nturakhia 1:ebe96d5a5825 285 m.speed(0);
nturakhia 1:ebe96d5a5825 286 lcd.color(LGREY);
nturakhia 1:ebe96d5a5825 287 lcd.printf("Motor stopped\n");
nturakhia 1:ebe96d5a5825 288
nturakhia 1:ebe96d5a5825 289 }
nturakhia 1:ebe96d5a5825 290
nturakhia 1:ebe96d5a5825 291 wait(5);
4180_1 0:da7f4b6d2b7c 292 }
4180_1 0:da7f4b6d2b7c 293 }