#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int time = 2; // input from clock module one pulse every second on interupt pin
const int CLK1 = 3; // input from encoder reel1
const int CLK2 = 4; // input from encoder reel2
const int CLK3 = 5; // input from encoder reel3
// const int DT = 6;
const int RESET = 7;
int count1 = 100;
int count2 = 100;
int count3 = 100;
int A = 100; // Upper count 1 comparator
int B = 99; // Lower count 1 comparator
//int C = 6; // add extra steps comparator to bring reel back to start (reel 1) NOT REQUIRED
//int D = 5; // add extra steps comparator to bring reel back to start (reel 2)
//int E = 7; // add extra steps comparator to bring reel back to start (reel 3)
int Set1;
int Set2;
int Set3;
int Squarewave;
int hours = 0;
int tens = 0;
int mins = 0;
int seconds = 0;
int debounce1 = 0;
int debounce2 = 0;
int debounce3 = 0;
int click1; // encoder for reel1
int click2; // encoder for reel2
int click3; // encoder for reel3
int motor1 = 13;
int motor2 = 12;
int motor3 = 11;
int pulse1;
int reelsetposition1 = 8; // slot sensor to know where reel1 position is in real time
int reelsetposition2 = 9; // slot sensor to know where reel2 position is in real time
int reelsetposition3 = 10; // slot sensor to know where reel3 position is in real time
int slot1;
int slot2;
int slot3;
//const int setReel1 = 48;
//const int setReel2 = 49;
//const int setReel3 = 50;
int clockSet1 = 41;
int clockSet2 = 42;
int clockSet3 = 43;
//int Set1;
//int Set2;
//int Set3;
int clockSetLed1 = 38;
int clockSetLed2 = 39;
int clockSetLed3 = 40;
int spin1;//...................................................................................................New
int spinning1;
int a = 0;
void setup() {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
//Serial.begin(115200);
pinMode(time, INPUT_PULLUP);
pinMode(CLK1, INPUT_PULLUP);
pinMode(CLK2, INPUT_PULLUP);
pinMode(CLK3, INPUT_PULLUP);
//pinMode(DT, INPUT_PULLUP);
pinMode(RESET, INPUT_PULLUP);
pinMode(motor1, OUTPUT); // motor1 drives reel1
pinMode(motor2, OUTPUT); // motor1 drives reel2
pinMode(motor3, OUTPUT); // motor1 drives reel3
//pinMode(setReel1, INPUT_PULLUP);
//pinMode(setReel2, INPUT_PULLUP);
//pinMode(setReel3, INPUT_PULLUP);
pinMode(reelsetposition1, INPUT_PULLUP);
pinMode(reelsetposition2, INPUT_PULLUP);
pinMode(reelsetposition3, INPUT_PULLUP);
pinMode(clockSet1, INPUT_PULLUP);
pinMode(clockSet2, INPUT_PULLUP);
pinMode(clockSet3, INPUT_PULLUP);
pinMode(clockSetLed1, OUTPUT);
pinMode(clockSetLed2, OUTPUT);
pinMode(clockSetLed3, OUTPUT);
//spin1=5;
}
void loop() {
DateTime now = rtc.now();
// Positional table / code for reel 1 minutes
if ((now.minute()==0)&&(now.second()==0))
{
spin1 = 5; // spin reel1 two times before landing on number...............................................................................................NEW
count1 = 69; // spin reel to zero position 17 plus 3 steps due to inertia = 0, the slot sensor is aligned with position zero
}
if ((now.minute()==1)&&(now.second()==0))
{
count1 = 65;
}
if ((now.minute()==2)&&(now.second()==0))
{
count1 = 77;
}
if ((now.minute()==3)&&(now.second()==0))
{
count1 = 73;
}
if ((now.minute()==4)&&(now.second()==0))
{
count1 = 69;
}
if ((now.minute()==5)&&(now.second()==0))
{
count1 = 81;
}
if ((now.minute()==6)&&(now.second()==0))
{
count1 = 81;
}
if ((now.minute()==7)&&(now.second()==0))
{
count1 = 73;
}
if ((now.minute()==8)&&(now.second()==0))
{
count1 = 69;
}
if ((now.minute()==9)&&(now.second()==0))
{
count1 = 73;
}
REEL 1 code ...........................................................................................................................................................................
if (count1 < A) // power is switched off to the motor after 23 steps and the inertia continues to turn motor a further 3 steps to 26 this increments the reel by 6 steps or 3 character positions.
{
digitalWrite(motor1, HIGH);
}
if (count1 > B)
{
digitalWrite(motor1, LOW);
}
while (spin1 > 0)
{
spinning1=digitalRead(reelsetposition1);
// digitalRead(reelsetposition1);
if (spinning1== LOW)
{
spin1 --;
Serial.println("spin1");
Serial.println(spin1);
}
delay(1000);
}
click1=digitalRead(CLK1);
if ((click1==LOW)&&(debounce1==0))
{
count1 ++;
debounce1 ++;
Serial.println(count1);
}
click1=digitalRead(CLK1);
if ((click1==HIGH)&&(debounce1==1))
{
debounce1=0;
delay(50); // delay is to eliminate switch bounce 2.4 seconds per rev. / 20 revs per min = .12 seconds
}
}