Showing posts with label Arduino. Show all posts
Showing posts with label Arduino. Show all posts

Friday, September 28, 2012

Arduino Controlled Computer Startup / Shutdown


Update 10/1/2012
After being unsatisfied with not having the RTC included in the build I dug through the both the  Adafruit & Arduino forums looking for someone who may have had the same issues. Sure enough I wasn't alone when dealing with the issues I was having and found the solution looking me right in thee face. Below is the corrected code minus the code for the LCD display which I still have to order, but after consideration will probably be skipping and leaving in just a project box with some light modification such as LED status lights. I have also removed the directly soldered wires going from the PC to the shield and replaced them with pinheaders as seen above (see pictures below for comparison) to make disconnecting the wires from the shield easier then leaving them soldered in.

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

int led = 2; //5v signal to relay
int led2 = 8; //5v signal to reed relay

RTC_DS1307 RTC;

void setup()
{
  Serial.begin(57600);
  Wire.begin();
    RTC.begin();
   
     if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
   DateTime now = RTC.now();
  setTime(hour(), minute(), second(), day(), month(), year());
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); 
  
  // Alarms
  Alarm.alarmRepeat(6,15,0, WakeAlarm);  // 6:15am Computers On
  Alarm.alarmRepeat(9,15,0, WorkAlarm);  // 9:15am Computers Off
  Alarm.alarmRepeat(15,20,0, HomeAlarm); // 3:20pm Computers On
  Alarm.alarmRepeat(2,0,0, NightAlarm);  // 2:00am Computers Off
   
}

void  loop(){ 
  DateTime now = RTC.now();
  setTime(now.hour(),now.minute(),now.second(),now.day(),now.month(),now.year()); // set time & date
  Alarm.delay(1000); //clock display delay
 
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
}

// Alarm Functions
void WakeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");   
}

void WorkAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
 
}
void HomeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");
}

void NightAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
}




Originally Posted 9/28/12





Like many folks all over the world my household's entertainment runs on XBMC. For those of you reading this who may have never heard of XBMC or what it can do in short XBMC is "One Media Player To Rule Them All". Our movie & TV show library is on our Network Attached Storage PC's located in our basement streaming to our XBMC compatible devices in our home. While this approach is nothing new for many already using XBMC or just those who use a at home general file servers, the electric cost for keeping things running 24hrs a day can be costly and wasteful. Looking at software solutions to turn off the PC's at a certain time of day there is plenty, but none to turn the PC back on.

Through out the life the Arduino platform and other microcontrollers we have seen countless DIY clocks, timers, alarm, & even garden watering cycles .... well why not an Arduino controlled computer? Anyone who has built a PC in the past 15yrs knows that the power button on a case does nothing more then complete a circuit. The situation screamed two things to me when looking into a Auto PC timer,  an Arduino & a reed relay.



Hooking up the Adafruit RTC DS1307 breakout board, two 5VDC reed relays, and a Seed Studio protoboard from Radio Shack to the Arduino I connected the #2 & #8 pin to the reed relays & the use of the LED comment to send 5v to the relay closing the N.O. (normally open) connection joining the two wires together just like the power button does on the PC case & simulating a button press.

 Inside the PC I added a simple PCB add-on to allow the power switch terminals to connect to the pin headers on the PCB, then from the PCB to the two pins on the motherboard. The power switch on the case still functions as normal and required no case modification.

I originally intended to use 1 reed relay to operate both machines but after it showed signs of failure I added a second relay for the 2nd PC  (see F.A.Q) to allow the systems to turn on and off without any issues in separate relays.

 As the schedule now stands the 2 NAS PC's turn on at 6AM at the start of everyone's day, off at 9:00AM back on at 3:00PM and off again at 2AM cutting 10 hours off of wasteful non use.

While many could say that it's just as easy to turn them on when I need them , this kind of is a problem to my 5yr old who I don't want walking down the stairs at 6:30AM on a Sunday cause she wants to be able to watch a movie on her XBMC device in her room.



As this was only a test run or more of a proof of concept so to speak the LCD screen hooked up was not running cause I feel the screen is just to small for the size of the project box and overall feel the display needs to be much larger in size for it to be effective as a display. Also I am redoing the overall protoboard to make a full shield add-on specifically designed for handling more then one machine possibly with a wireless Xbee solution to remove the wire aspect. 


 
Code Used

#include <Time.h>
#include <TimeAlarms.h>

int led = 2; //5v signal to relay
int led2 = 8; //5v signal to reed relay

void setup()
{
  Serial.begin(57600);
  setTime(12,0,0,9,29,12); // Set Time
  // create the alarms
  Alarm.alarmRepeat(6,15,0, WakeAlarm);  // 6:15am Computers On
  Alarm.alarmRepeat(9,15,0, WorkAlarm);  // 9:15am Computers Off
  Alarm.alarmRepeat(15,20,0, HomeAlarm); // 3:20pm Computers On
  Alarm.alarmRepeat(2,0,0, NightAlarm);  // 2:00am Computers Off
   
}

void  loop(){ 
  digitalClockDisplay();
  Alarm.delay(1000); //clock display delay
 
 
}

// Alarm Functions
void WakeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");   
}

void WorkAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
 
}
void HomeAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer ON");
}

void NightAlarm(){
  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  digitalWrite(led, HIGH);  
  digitalWrite(led2, HIGH); 
  delay(5000);              
  digitalWrite(led, LOW);   
  digitalWrite(led2, LOW);  
  delay(5000);              
  Serial.println("Computer Off");
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}


Arduino Controlled Computer Startup / Shutdown in Action





F.A.Q
(Based on questions asked by friends during build)

Q. Why 2 relays?
A. When hooking up a single PC with both sets of wires hooked up to one relay it worked fine , but hooking up both PC's to the single reed relay immediately turned off both PC's and would not allow them to turn back on without first removing the one of the hook ups from the other machine. The solution to this was to have a single relay for each machine, however a double pole single throw relay I am assuming would work seeing as both connections would continue to remain separate.





 

Friday, April 27, 2012

Arduino Based Book Report Diorama



     My 10yr old daughter recently had to do a diorama for a school book report as a presentation. Seeing examples of diorama's from museums, libraries and other kids projects at school she asked if we could make it cool. Asking her what she meant by "make it cool" she asked if we could add lights or moving parts to it, in which I was more then glad to accommodate as it would be a great way to introduce her to micro controllers and earn her another badge for the Adafruit "Hack Scouts".

In wanting her to learn just what micro controllers are used for I lent her my copy of "Getting Stared with Arduino" which even though is a little technical for younger readers just getting started, was a great reference tool to show her just a few examples of what people can do with an Arduino and explain the coding involved within Arduino.


Her design (as seen below) called for some simple things that were easy to accomplish with the Arduino platform. She wanted to have a yellow glowing light shining from inside the dresser drawer as described in the book, a light on the ceiling in the boys room, the boy, a bed, and a dresser.


Originally when helping her plan things out she wanted to have the dresser drawer open & close but unfortunately due to time & cost restraints we had to skip that moving part of the diorama and leave it fully open with just the LED's lighting up.

Taking a trip to our local Radio Shack we picked up a Servo motor, a 10mm white LED, & 2 yellow 3mm LED's for less then $20. Starting work on her project on April 22nd Earth Day I thought it would also be good to incorporate the use of recycled materials from parts & wire we already had here at the house from our parts pile which usually consists of broken computers, printers, scanners, etc.

To prevent any trips to the hospital I cut and shaped out the wooden box for the diorama along with the dresser, & bed. She picked the colors she wanted to paint things and we got to work on the projects appearance. Next we positioned everything were it needed to be along with hot gluing down things like the dresser, bed & LED's in their respective locations.








 After all the eye candy was done we went to work on the back of the box where the wires and everything else would be housed at. Already working towards earning her multimetter skill badge we talked about the importance of wiring and why Ohms law was so important when dealing with electronics, though while she didn't grasp all parts of Ohm's law she knows with out the use of a resistor in certain parts of electronic you might damage the micro controller.






When it came time to make the Ardunio work it's magic we used the "Sweep" example by BARRAGAN found within the Arudino example library and modified it to match our specifications as well as add the function to turn on the LED's.
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo myservo;   // create servo object to control a servo
                            // a maximum of eight servo objects can be created

int pos = 0;          // variable to store the servo position

void setup()
{
 pinMode(13, OUTPUT);    
  pinMode(12, OUTPUT);

  myservo.attach(7);  // attaches the servo on pin 7 to the servo object
}


void loop() {
 digitalWrite(13, HIGH);   // turn on the LED
  digitalWrite(12, HIGH);   //turn on the LED
 
  for(pos = 0; pos < 40; pos += 1)    // goes from 0 degrees to 40 degrees
  {                                                     // in steps of 1 degree
    myservo.write(pos);                     // tell servo to go to position in variable 'pos'
    delay(15);                                     // waits 15ms for the servo to reach the position
  }
  for(pos = 40; pos>=1; pos-=1)       // goes from 40 degrees to 0 degrees
  {                               
    myservo.write(pos);                     // tell servo to go to position in variable 'pos'
    delay(15);                                     // waits 15ms for the servo to reach the position
  }
}
The video below is my 10yr old daughter showing off her project and giving a "shout out" to the open source hardware community.




While we won't know her final grade until next week, she has told me after the comments from YouTube, Twitter & seeing her project appear on the Adafruit blog , she knows it's an A+.

On a side note a very big THANK YOU to everyone who has left comments on her project. A very big thank you to the Arduino community & to Adafruit for their idea of "Hack Scouts" and everyone else doing work with the Open Source Hardware Community.