Instructables

Step 7: Program the chip

Picture of Program the chip
7B.jpg

You will need to install the RTClib library for your code to work. Instructions to do this are on Ladyada's page.

Download lunchtime_clock.zip, uncompress it and then upload the lunchtime_clock.pde code onto your chip.

If you don't feel like downloading the file, here is the code:

// Lunchtime Clock
// by Randy Sarafan
//
// Slows down 20% at 11 and speeds up 20% at 11:48 until it hits 1.
// The rest of the time the clock goes at normal speed
//
// Do what you want with this code. Just make certain that whatever you do, it is awesome.
//

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

RTC_DS1307 RTC;

int clockpin = 9;
int clockpin1 = 10;

void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
}

void loop () {

DateTime now = RTC.now();

TurnTurnTurn(1000);


if (now.hour() == 11) {
for (int i = 0; i < 1800; i++) {
TurnTurnTurn(800);
}

for (int i = 0; i < 1800; i++) {
TurnTurnTurn(1200);
}
}
}

int TurnTurnTurn(int TimeToWait){

analogWrite(clockpin, 0);
analogWrite(clockpin1, 124); // sets the value (range from 0 to 255)
delay(TimeToWait);

analogWrite(clockpin, 124);
analogWrite(clockpin1, 0);
delay(TimeToWait);

}


 
Remove these adsRemove these ads by Signing Up
RodCastler2 years ago
hi! nice project. Why are you using 50% duty cicles in your tutnturnturn function instead of digitalwrite(pin,on) ?
randofo (author)  RodCastler2 years ago
It is not a DC motor. It is a single coil stepper motor. Power it on will make it move one step and then stop.
PedroDiogo3 years ago
First of all, nice project.

However I was able to detect a flaw in your code. You are only using the RTC for checking the actual hour, however the delay between each second on the clock is controlled by the clock on the Arduino.

This could cause a mismatch between the time on the RTC and the actual wall clock.