Hi All
Still having problems with the code for this clock.
#define SP1 7
#define SP2 8
#define SP3 10
#define SP4 11
#define SET_PIN 9
#include <OneWire.h>
#include "myPushSwitch.h"
OneWire ds(A0); // on pin A0
// Set input has internal pullup; LOW when active; no double-click; no double-click delay; use default debounce time
PushSwitch setSwitch(SET_PIN, INPUT_PULLUP, LOW, false);
const uint32_t ONE_MINUTE = 60 * 1000UL;
const uint32_t MAX_SET_STEP_INTERVAL = 1000UL;
const uint32_t MIN_SET_STEP_INTERVAL = 50UL;
const uint32_t SET_STEP_DELTA = 100UL;
void parkMotor()
{
// step 0
digitalWrite(SP1, LOW);
digitalWrite(SP2, LOW);
digitalWrite(SP3, LOW);
digitalWrite(SP4, LOW);
}
void stepMotor(uint16_t tickLength)
{
// step 0
digitalWrite(SP1, LOW);
digitalWrite(SP2, HIGH);
digitalWrite(SP3, HIGH);
digitalWrite(SP4, HIGH);
delayMicroseconds(tickLength);
// step 1
digitalWrite(SP1, LOW);
digitalWrite(SP2, HIGH);
digitalWrite(SP3, HIGH);
digitalWrite(SP4, LOW);
delayMicroseconds(tickLength);
// step 2
digitalWrite(SP1, HIGH);
digitalWrite(SP2, HIGH);
digitalWrite(SP3, HIGH);
digitalWrite(SP4, LOW);
delayMicroseconds(tickLength);
// step 3
digitalWrite(SP1, HIGH);
digitalWrite(SP2, LOW);
digitalWrite(SP3, LOW);
digitalWrite(SP4, LOW);
delayMicroseconds(tickLength);
// step 4
digitalWrite(SP1, HIGH);
digitalWrite(SP2, LOW);
digitalWrite(SP3, LOW);
digitalWrite(SP4, HIGH);
delayMicroseconds(tickLength);
// step 5
digitalWrite(SP1, LOW);
digitalWrite(SP2, LOW);
digitalWrite(SP3, LOW);
digitalWrite(SP4, HIGH);
delayMicroseconds(tickLength);
// step 6
digitalWrite(SP1, LOW);
digitalWrite(SP2, HIGH);
digitalWrite(SP3, HIGH);
digitalWrite(SP4, HIGH);
delayMicroseconds(tickLength);
// idle
parkMotor();
}
uint32_t lastStepTime;
uint32_t stepInterval;
uint32_t secondsCount;
void setup()
{
//Serial.begin(115200);
//Serial.println("program started");
pinMode(SP1, OUTPUT);
pinMode(SP2, OUTPUT);
pinMode(SP3, OUTPUT);
pinMode(SP4, OUTPUT);
parkMotor();
setSwitch.begin();
lastStepTime = secondsCount;
stepInterval = ONE_MINUTE;
}
void loop()
{
byte i;
byte present = 0;
byte data[8];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return;
}
for( i = 0; i < 8; i++) {
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return;
}
if ( addr[0] != 0x27) {
return;
}
// write!
present = ds.reset();
ds.select(addr);
ds.write(0x99,1); // write RTC - this is the write code
ds.write(0x3C); //This is the control byte. AC in hex = 10101100
//read the datasheet and you will see that this is important
//to start the internal osc's... Or to make the clock start
//counting seconds. --ril3y
present = ds.reset();
// read!
present = ds.reset();
ds.select(addr);
ds.write(0x66,1); // read RTC
for ( i = 0; i < 5; i++) {
data[i] = ds.read();
}
for ( i = 1; i < 5; i++) {
}
// convert bytes in data[] array to a more useful form
secondsCount = (((((data[4]*256UL)+data[3])*256UL)+data[2])*256UL)+data[1];
switchEvent event = setSwitch.update();
if (event == switchPressed)
{
//Serial.print("switch pressed - ");
stepInterval = MAX_SET_STEP_INTERVAL;
//Serial.println(stepInterval);
}
else if (event == switchReleased)
{
lastStepTime = millis();
stepInterval = ONE_MINUTE;
//Serial.println("switch released");
}
if (millis() - lastStepTime > stepInterval)
{
lastStepTime = millis();
//Serial.println("tick");
stepMotor(6000);
switchState state = setSwitch.read();
if (state == switchWasPressed)
{
//Serial.print("switch still pressed - ");
if (stepInterval > (MIN_SET_STEP_INTERVAL + SET_STEP_DELTA))
{
stepInterval -= SET_STEP_DELTA;
}
else
{
stepInterval = MIN_SET_STEP_INTERVAL;
}
//Serial.println(stepInterval);
}
}
}
I can't get the time for the RTC to replace the millis() function without upsetting the clock movement and setting the clock with the push button.
I'm beginning to pull my hair out with it now.
Thank you