-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClockDriver.cpp
124 lines (113 loc) · 2.89 KB
/
ClockDriver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <EEPROM.h>
#include "Arduino.h"
#include "ClockDriver.h"
#define NUM_STEPS 843
void ClockDriver::setup() {
this->setup(4,5,6,7,30);
}
void ClockDriver::_readSteps() {
int address = 0;
int memorySteps = 0;
int value = 0;
while( value = EEPROM.read(address) ) {
memorySteps = (memorySteps * 255) + value;
#ifdef DEBUG
Serial.print(address);
Serial.print("\t");
Serial.print(value);
Serial.print("\t");
Serial.print(memorySteps);
Serial.println("");
#endif
++address;
}
currentSteps = memorySteps;
#ifdef DEBUG
Serial.print("read currentSteps from memory: ");
Serial.println(currentSteps);
#endif
}
void ClockDriver::writeSteps() {
int address = 0;
int memorySteps = currentSteps;
while( memorySteps > 255 ) {
EEPROM.write(address,memorySteps / 255);
memorySteps = memorySteps % 255;
++address;
}
EEPROM.write(address,memorySteps);
EEPROM.write(address+1,0);
#ifdef DEBUG
Serial.print("written currentSteps to memory: ");
Serial.println(currentSteps);
#endif
}
void ClockDriver::stepTo(int step, boolean allowBackwards) {
int direction = true;
if( allowBackwards ) {
int backwards = ( currentSteps - step ) % maxSteps;
int forwards = ( step - currentSteps ) % maxSteps;
if( backwards < 0 )
backwards += maxSteps;
if( forwards < 0 )
forwards += maxSteps;
if( backwards < forwards ) {
direction = false;
}
}
/*
while( currentSteps != step ) {
this->step(direction);
}
*/
Serial.print("step = ");
Serial.println(step);
Serial.print("currentSteps = ");
Serial.println(currentSteps);
int steps = step - currentSteps;
Serial.print("steps = ");
Serial.println(steps);
if( steps < 0 )
steps += (NUM_STEPS*12);
steps = - steps;
Serial.print("steps = ");
Serial.println(steps);
Serial.print("stepper = ");
Serial.println((int)(this->stepper));
while( steps < -100 ) {
stepper->step(-100);
steps += 100;
}
stepper->step(steps);
// Now turn off the output to the H-bridge to help reduce power consumption and
// stop the H-bridge from getting too hot
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
digitalWrite(pin3,LOW);
digitalWrite(pin4,LOW);
currentSteps = step;
this->writeSteps();
}
void ClockDriver::setClockHands( int bigHand, int smallHand ) {
if( bigHand < 0 || bigHand >= 12 || smallHand < 0 || smallHand >= 12 )
return;
int newStep = NUM_STEPS * smallHand + ( bigHand * ( NUM_STEPS / 12 ) );
stepTo( newStep, false );
}
boolean ClockDriver::isActive() {
return active;
}
int ClockDriver::getCurrentStep() {
return currentSteps;
}
void ClockDriver::setup(int a, int b, int c, int d, int e) {
pin1 = a; pin2 = b; pin3 = c; pin4 = d;
stepper = new Stepper(NUM_STEPS,a,b,c,d);
stepper->setSpeed(e);
maxSteps = NUM_STEPS*12;
currentSteps = 0;
this->_readSteps();
targetStep = -1;
targetDirection = true;
active = false;
}