/* The circuit: Listing usual pins used, and "new" pins I used * LCD RS pin to digital pin 12 now 3 (pin 5 on the chip itself) * LCD Enable pin to digital pin 11 now 4 (pin 6 on the chip itself) * LCD D4 pin to digital pin 5 still 5 (pin 11 on the chip itself) * LCD D5 pin to digital pin 4 now 6 (pin 12 on the chip itself) * LCD D6 pin to digital pin 3 now 7 (pin 13 on the chip itself) * LCD D7 pin to digital pin 2 now 8 (pin 14 on the chip itself) * LCD R/W pin to ground * 10K potentiometer: ends to +5V and ground wiper to LCD VO pin 3 * 100 ohm from LCD BKL+ pin 15 to +V for backlight * LCD BKL- pin 16 to ground for backlight * 10K potentiometer: ends to +5V and ground wiper to A0 pin 23 for data values * 5K pull down resistor on push button input D2 pin 4 to ground. */ // include the library code: #include // initialize the library with the numbers of the interface pins LiquidCrystal lcd(3, 4, 5, 6, 7, 8); // LiquidCrystal lcd(12, 11, 10, 9, 8, 7); Alternate wiring int value,outvalue; int hours,mins; int data; // reading from pot int pot1=0; //analog channel int button = 2; // define button pin int pressed; // status of button. 1 = pressed int outletMins [9]; // int outletMins [9] = {0,1,2,3,4,0,6,7,600}; // test data to be removed later int choice; int interval=2000; // 2 second timer unsigned int interval2=60000; // 1 minute timer unsigned long currentMillis = millis(); unsigned long currentMillis2 = millis(); unsigned long previousMillis=0; unsigned long previousMillis2=0; int x; int y; int displayOutlet; int out[9] = {0,9,10,11,12,16,17,18,19}; // define output pins // since outlet 0 is a phantom outlet, it's not really active, so it's assigned to an unused pin) // 16-19 are the numeric equivalents of A2 to A5 // 9 - 12 are pins 15 to 18 on the chip itself // 16 - 19 (A2 - A5) are pins 25 - 28 on the chip itself int Power = 13; // set power on/off pin int POWER = 1; // Power on mode (1 = power on) void setup() { pinMode (Power,OUTPUT); // first things first. IMMEDIATELY set up the power relay digitalWrite (Power,POWER); // Turn on power relay // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("SuperTech-IT.COM"); lcd.setCursor(0, 1); lcd.print("POWER ON"); pinMode (Power,OUTPUT); digitalWrite (Power,POWER); // Turn on power relay for (x=1; x<9; x++) { pinMode (out[x], OUTPUT); // set output pins to output mode } } void loop() { /* data = analogRead(pot1); //read the value from the sensor pressed = digitalRead(button); value = (data); // take value of A0 and put it here for manipulation hours = (value/100); // calculate # of hours from read value // (note: line 1 is the second row, so it doesn't wipe out the title: lcd.setCursor(0, 1);// set the cursor to column 0, line 1 lcd.print(hours); lcd.print(" "); lcd.print(value); lcd.print(" "); lcd.print (pressed); lcd.print(" "); lcd.print(millis() / 1000); lcd.print(" "); */ // How much time has passed, accounting for rollover with subtraction! currentMillis = millis(); currentMillis2 = millis(); pressed = digitalRead(button); // see if button has been pressed for (x=1;x<9;x++) { outvalue = 0; if (outletMins [x] > 0) {outvalue=1;} digitalWrite (out[x],outvalue); // turn on or off appropriate output } if (pressed == 1) { getData(); } if ((unsigned long)(currentMillis2 - previousMillis2) >= interval2) { // 1 minute has passed - decrease all timers by 1 minute unless they are at 0 (off) or 10 hours (on) previousMillis2 = currentMillis2; decreaseMins(); } if ((unsigned long)(currentMillis - previousMillis) >= interval) { // 2 seconds have passed - display next outlet info displayOutlet ++; if (displayOutlet == 9) {displayOutlet = 1;} previousMillis = currentMillis; lcd.setCursor(0, 0); lcd.print ("OUTLET# "); lcd.print (displayOutlet); // lcd.print (pressed); was here for debugging lcd.print (" "); lcd.setCursor(0, 1); convertMins (displayOutlet); if ((hours > 0) && (hours < 10)) { lcd.print (hours); lcd.print (" Hours "); } if ((mins > 0 || hours > 0) && (hours < 10)) { lcd.print (mins); lcd.print (" Min "); } if (hours == 0) {lcd.print (" ");} if (outletMins[displayOutlet] == 0) { lcd.print ("OFF"); } if (hours == 10) { lcd.print ("ALWAYS ON "); // extra spaces clear old data left on the LCD line } // Use the snapshot to set track time until next event } // delay(1000); //wait one second currentMillis = millis(); // lcd.setCursor(0, 0); // lcd.print (previousMillis); // lcd.print (currentMillis); } /***************************************************** routines *****************************************************/ void decreaseMins() { POWER = 0; // set Power mode ready to go off unless an outlet is on for (x=1; x <= 8; x++) { if (outletMins[x] > 0) {POWER = 1;} // if any outlet is on, leave power on } digitalWrite (Power, POWER); for (x=1; x <= 8;x++) { y = (outletMins[x]); if ((y > 0) && (y < 600)) { outletMins [x] --; } //lcd.print (outletMins[1]); } } void convertMins(int x) { hours = (outletMins[x]/60); mins = (outletMins[x] - (hours * 60)); } void getData() { pressed = 0; // reset the button pressed flag lcd.clear(); //clear display to acknowledge button press delay (1000); // Button de-bounce while (pressed == 0) //keep getting input from pot until button press { pressed = digitalRead(button); // see if button has been pressed data = analogRead(pot1); value = (data/120); //get outlet number from none to 8 lcd.setCursor(0, 0);// set the cursor to column 0, line 1 lcd.print("Choose Outlet"); lcd.print(" "); if (value != 0){ lcd.print(value); } lcd.print(" "); } choice = value; pressed = 0; // reset the button pressed flag lcd.clear(); //clear display to acknowledge button press delay (1000); // Button de-bounce while (pressed == 0) //keep getting input from pot until button press { pressed = digitalRead(button); // see if button has been pressed data = analogRead(pot1); // read the pot value = (data/100); //get outlet number lcd.setCursor(0, 0);// set the cursor to column 0, line 1 lcd.print("Duration "); if ((value != 0) && (value < 10)){ lcd.print(value); lcd.print(" hours "); } if (value == 10){ lcd.print("ON "); } if (value == 0){ lcd.print("OFF "); } } outletMins[choice] = (60 * value); // convert hours to minutes lcd.clear(); //clear display to acknowledge button press delay (1000);// Button de-bounce pressed = 0; // reset the button pressed flag }