Rise and Shine! -Arduino LDR wekker (5 / 7 stap)

Stap 5: Software: de Code

 /************************************************************************************************************ * Sunrise Alarm Clock by Bhavika Shah MakeCourse Spring 2016 * * When the LDR detects the sunrise, it will cause the servo(hammer attached) to turn 50 degrees * and hit a bell, as a wake up alarm. As a backup the code will calculate the sunrise time for the location * of the clock using latitude and longitude coordinates, and activate the alarm in case there is no sunrise * detected. This project also includes an RTC module that will keep time in case of power outage. ***********************************************************************************************************/ #include <Wire.h> #include <Servo.h> #include <TimeLib.h> //works in conjunction with DS1307RTC.h #include <TimeLord.h> //contains sunrise calculations #include <DS1307RTC.h> //RTC library #include <LiquidCrystal_I2C.h> //library to be used with LCD with a YwRobot Arduino LCM1602 IIC V1 module attached #define DS1307_ADDRESS 0x68 //RTC addressLiquidCrystal_I2C lcd(0x27,20,4); //Set the LCD address to 0x27 for a 16 chars and 2 line display Servo myservo; //Make a servo object int pos = 50; //Setting a servo position at 50 degrees int lightPin = A0; //Connect LDR to pin A0 int threshold = 500; //You set the light threshold to a value const int TIMEZONE = -5; //Set your time zone; mine is EST const float LATITUDE = 27.9506; //Set your latitude and longitude; mine are for Tampa, FL const float LONGITUDE = -82.4572; int wake = 0; // bool flag = 1; //Here, we have a boolean flag that will change when we perform our wakeup actions int sunriseMin; //minute of that days calculated sunrise to be used for comparison</p><p>void setup(){ void digitalClockDisplay(); //digital clock display of the time void printDigits(); //utility function for digital clock display: prints preceding colon and leading 0 void sunriseCalc(); //calculate sunrise void actionAM(); //function to perform wake up actions void checkPM(); //for debugging void lowLight(); //function to display sensor value if it is less than the threshold Serial.begin(9600); //begin serial communcation pinMode(13, OUTPUT); myservo.attach(9); //servo attached to pin 9 lcd.init(); //initialise the lcd lcd.backlight(); //turning on the LCD backlight setSyncProvider(RTC.get); //function to get the time from the RTC if(timeStatus()!= timeSet){ Serial.println("Unable to sync with the RTC"); } else { Serial.println("RTC has set the system time"); digitalClockDisplay(); } sunriseCalc(); }</p><p>void loop(){ if(wake = 0){ if (isAM()){ Serial.println("isAM true"); wake = 1; //if it's morning 'wake' will change from 0 to 1 flag = 0; //we will also change the flag variable to 0 Serial.println("First if is running"); //debugging Serial.print("flag: "); //for debugging Serial.println(flag); //for debugging } } if(wake = 1 && flag == 0) { //Because wake is 1, and flag is 0 , and we can begin light detection digitalClockDisplay(); //consistent readout of the the current time flag = 1; //We will change flag to 1 because the wakeup actions are being performed Serial.println(analogRead(lightPin)); //readout of the current light being detected if(analogRead(lightPin) > threshold){ //if the light detected is greater than our threshold: actionAM(); //perform wake up actions Serial.println("Second if is running"); //debugging } else{ flag = 0; printDigits(minute()); //debugging Serial.println(); //If no light is detected to perform wake up actions, we will check to see if current time has not passed sunriseMin + 10 if (sunriseMin < minute()){ //if the current minute value is greater than the sunrise minute value actionAM(); //perform wake up actions flag = 1; Serial.println("sunrise min is lower than current min"); //debug to let us know that the loop has gone through this statement } lowLight(); //if there is no light, we will check } delay(1000); } if (isPM()){ checkPM(); } } /**************************************************** FUNCTIONS ****************************************************/ void sunriseCalc(){ TimeLord tardis; tardis.TimeZone(-4 * 60); // tell TimeLord what timezone your RTC is synchronized to. You can ignore DST tardis.Position( LATITUDE, LONGITUDE ); // tell TimeLord where in the world we are byte today[] = { 0, 0, 12, 6, 4, 2016 }; // store today's date (at noon) in an array for TimeLord to use if (tardis.SunRise(today)){ // if the sun will rise today (it might not, in the [ant]arctic) Serial.print("Sunrise: "); Serial.print((int) today[tl_hour]); Serial.print(":"); Serial.print((int) today[tl_minute]); Serial.print(" AM"); Serial.println(); sunriseMin = (int) today[tl_minute] + 10; //storing the value of the sunrise time + 10 Serial.print("sunriseMin: "); Serial.println(sunriseMin); } } void digitalClockDisplay(){ // digital clock display of the time Serial.print(hour()); printDigits(minute()); printDigits(second()); Serial.print(" "); Serial.print(day()); Serial.print("/"); Serial.print(month()); Serial.print("/"); Serial.print(year()); Serial.println(); delay(200); } void printDigits(int digits){ // utility function for digital clock display: prints preceding colon and leading 0 Serial.print(":"); if(digits < 10) Serial.print('0'); Serial.print(digits); } void actionAM(){ //function that contains all actions to be performed when it is time to get up! digitalWrite(13, HIGH); //turns the onboard LED on Serial.println("High"); //we print the status of the amount of light detected lcd.setCursor(3,0); //setting the cursor position lcd.print("Wake up Bee"); //printing a wake up message! lcd.setCursor(2,1); lcd.print("It's Sunrise!"); lcd.setCursor(0,2); //servo actions for (pos = 0; pos <= 50; pos += 1) { // goes from 0 degrees to 50 degrees in steps of 1 degree myservo.write(pos); delay(5); } for (pos = 50; pos >= 0; pos -= 1) { // goes from 50 degrees to 0 degrees myservo.write(pos); delay(5); } for (pos = 0; pos <= 50; pos += 1) { //0--->50 repeat myservo.write(pos); delay(5); } for (pos = 50; pos >= 0; pos -= 1) { //50--->0 repeat myservo.write(pos); delay(5); } } void checkPM(){ //function that returns a statement on the serial monitor if it is PM; used for debugging lcd.noDisplay(); //turning off the display on the LCD to conserve power Serial.println("isPM() True"); digitalClockDisplay(); Serial.println(analogRead(lightPin)); //readout of the current light being detected delay(1000); } void lowLight(){ //if the light detected is less than the threshold, we will print it on the serial monitor to keep track of it Serial.println(analogRead(lightPin)); //readout of the current light being detected digitalWrite(13, LOW); Serial.println("Low"); myservo.write(0); //return servo to 0 degrees } 

Gerelateerde Artikelen

Eng truc met behulp van de Arduino-LDR-BlueAct app

Eng truc met behulp van de Arduino-LDR-BlueAct app

Start een grapje met je vrienden op vervelen nachten & maken vele trucs met behulp van een ding dat we allemaal hebben smartphones of tabletten met een kleine grappige truc... hier vond ik een app op PlayStore die in deze trucs en hier is een tutoria
Arduino LDR Motion Tracking

Arduino LDR Motion Tracking

Ik heb al speelde met het idee om iets te doen met het volgen van beweging. Ik had soortgelijke te bouwen een beetje beweging bijhouden Nerf gun turret (of soortgelijke) dus ik dacht ik zou beginnen met de basics, niets te flash, niets te duur dat, d
Pak Alarm met behulp van arduino LDR + piëzo zoemer

Pak Alarm met behulp van arduino LDR + piëzo zoemer

Dit kleine project leert u, hoe maak je een eenvoudige Grab alarm met behulp van een LDR (light afhankelijke resistor). Wanneer u probeert te verplaatsen van je hand over de LDR, is er een luid alarm dat gaat op en het alarm wordt uitgeschakeld zodra
Arduino MP3 wekker

Arduino MP3 wekker

Een tijdje geleden, struikelde ik over een component genaamd DFPlayer. Het is een klein onderdeel dat toelaat om te spelen mp3 van een micro SD-kaart (voor minder dat 10€!).http://www.dfrobot.com/wiki/index.php/DFPlayer_Min...Aangezien ik ziek was va
Arduino LDR snelheidsmeter

Arduino LDR snelheidsmeter

Misschien heb je een mini kabelbaan of iets anders dat u wilt weten van de snelheid.Er zijn vele manieren op HOW-TO mesure snelheid, maar vandaag zullen we werken met het meten van fundamentele snelheid. Ik was net het bouwen van mijn model trein toe
Montage "Verstandig klok 2" (Arduino gebaseerde wekker met veel extra functies)

Montage "Verstandig klok 2" (Arduino gebaseerde wekker met veel extra functies)

deze tutorial toont hoe de kit te monteren voor Wise klok 2, een open bronproject (hardware en software).Een complete Verstandig klok 2 kit kan hier worden gekocht.Kortom is dit wat Wise klok 2 kan doen (met de huidige opensource software release):-w
Lui Arduino lichtschakelaar

Lui Arduino lichtschakelaar

Hallo! Welkom bij mijn tweede instructable! Tweede? Ja, inderdaad, tweede. Klik hier om mijn eerste.Deze keer heb ik besloten voor het automatiseren van mijn lichtschakelaar. Waarom? Nou, ik ben een IT-persoon, en ik ben lui, dus waarom niet!? Het id
Regenboog van anarchistische vlag zepen

Regenboog van anarchistische vlag zepen

Ik had onlangs een kans om te gaan kamperen met meer dan duizend mensen die zelf libertariërs en anarchisten overwegen. Het woord "anarchist" wordt soms verkeerd gebruikt door leden van de nieuwsmedia te beschrijven van gewelddadige misdadigers
Zachte pompoen Pretzels met kaneel suiker!

Zachte pompoen Pretzels met kaneel suiker!

Mijn vriendin houdt van te bakken, en ik hou te helpen (met name met het eten deel.) Samen besloten hebben we om een instructable voor één van onze favoriete hapjes voor Halloween. Delicious, warm, kaneel en suiker pompoen zoutjes zijn leuk om te mak
Wekker met Tetris te bewijzen u bent wakker

Wekker met Tetris te bewijzen u bent wakker

dit is een Arduino aangedreven wekker die na het raken van tweemaal het alarm niet annuleren zal totdat de gebruiker heeft gewist 4 lijnen in het spel Tetris. U schakelt de klok zijwaarts, zodat het scherm verticaal is, Tetris spelen.  Het is nooit l
ARDUINO SOLAR CHARGE CONTROLLER (versie 2.0)

ARDUINO SOLAR CHARGE CONTROLLER (versie 2.0)

Een jaar geleden, begon ik bouw mijn eigen zonnestelsel om macht voor mijn dorpshuis. In eerste instantie maakte ik een LM317 gebaseerde heffing controller en een energie-meter voor de bewaking van het systeem. Ik eindelijk PWM heffing controller. In
Hoe te controleren DC motoren met behulp van Arduino

Hoe te controleren DC motoren met behulp van Arduino

We hebben onlangs klaar bent met het ontwerpen van onze compacte plug-and-play Arduino robot en bracht het album uit op Kickstarter. We zijn momenteel op zoek naar donateurs.Met deze robot kit, het is zeer eenvoudig aan te sluiten motoren, sensoren e
Palm Arduino V3

Palm Arduino V3

ik ontwerp en maak deze Palm Arduino V3-prototype PCB, zoals ik moe was van het herscheppen van Arduino compatibel op première boord telkens ik prototyping een nieuw project, vooral toen ik moest besteden veel tijd traceren de bedrading om te zien da
Alarmklok Arduino

Alarmklok Arduino

Arduino is een verbazingwekkende machine. Dit project is slechts een van de vele. U kunt uw project volgens de instructable bouwen of uitbreiden naar meer functies als u wilt.We zullen een volledig interactieve arduino aangedreven wekker construeren.