This is my play code, which you will be able to tell I`m no programmer.
But it works !!!
Forget about the knight bit , thats just running light to control.
#include <Wire.h>
long clock_delay = 0;
char bcd_string[]="12:34:56";
char clock_string[]="??:??:??";
int rtc_address = B1101000;
int io_address = B100001;
int rtc_bcd[7];
boolean alarm_set[3];
int alarm_on[3][3];
int alarm_off[3][3];
int state=0;
boolean knight_run = false;
long knight_time = 0;
int knight_delay = 50;
int x = 0;
boolean forward = true;
int ledPin = 12;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
// start_ball(); // set the rtc arrary , not needed now I have a battery
// clock_save();
int_alarm();
pinMode(ledPin, OUTPUT);
alarm_set[0] = true;
set_alarm(0 , true , "16:00:00");
set_alarm(0 , false , "16:30:00");
alarm_set[1] = true;
set_alarm(1 , true , "17:00:00");
set_alarm(1 , false , "17:30:00");
alarm_set[2] = true;
set_alarm(2 , true , "18:00:00");
set_alarm(2 , false , "18:30:00");
display_alarms(0);display_alarms(1);display_alarms(2);
io_int();
Serial.print ("Bytes of available Memory = ");
Serial.println (availableMemory(), DEC);
}
void loop()
{
clock_read();
bcd2string();
if (strcmp(bcd_string , clock_string) != 0)
{
// Serial.println(clock_string);
strcpy(clock_string , bcd_string);
check_alarms();
}
if ( millis() >= knight_time )
{
knight_time = millis() + knight_delay;
if ( knight_run == true ) knight();
}
x = read_io(1);
if ( x != state and x != 0)
{
switch (x)
{
case 1:
knight_delay++ ;
break;
case 2:
knight_delay-- ;
break;
case 4:
knight_delay = 50;
break;
case 8:
Serial.println(clock_string);
}
Serial.println(knight_delay);
state=x;
}
if ( x == 0 ) state = 0;
} // end of loop
void clock_save()
{
Wire.beginTransmission(rtc_address); Wire.send(0x00); // reset register pointer
for(int i=0; i<7; i++) { Wire.send(rtc_bcd[i]);} Wire.endTransmission();
}
void clock_read()
{
Wire.beginTransmission(rtc_address);
Wire.send(0x00); Wire.endTransmission(); Wire.requestFrom(rtc_address, 7);
for(int i=0; i<7; i++) { rtc_bcd[i]=Wire.receive();}
}
void start_ball()
{
rtc_bcd[0]=B00000000; // Second -- 0 --
rtc_bcd[1]=B01000101; // Minites -- 45 --
rtc_bcd[2]=B00001010; // Hours -- 2 --
rtc_bcd[3]=B00000001; // Sunday 1 , Monday 2 , Tuesday 3 etc in BCD -- Saturday -- 7 --
rtc_bcd[4]=B00000001; // Date -- 1 --
rtc_bcd[5]=B00000001; // Month -- 1 --
rtc_bcd[6]=B00000001; // Year -- 1 --
} // end of start_ball
void string2bcd()
{
rtc_bcd[2] = (bcd_string[1]-48) + (bcd_string[0]-48 << 4); //hours 12:12:12
rtc_bcd[1] = (bcd_string[4]-48) + (bcd_string[3]-48 << 4); //mins
rtc_bcd[0] = (bcd_string[7]-48) + (bcd_string[6]-48 << 4); //sec
} // end of string2bcd
void bcd2string()
{
int rtc_dec = 0 ;int j = 0 ;int i = 2 ;
rtc_dec = (( rtc_bcd[i] & B0111000) >> 4) * 10 + (rtc_bcd[i] & B00001111); // hours 2 , mins 1 , sec 0
if ( rtc_dec <= 10 ) bcd_string[j] = 48;
bcd_string[j]=(rtc_dec / 10 )+48;
j++,bcd_string[j]=(rtc_dec % 10)+48,j++,bcd_string[j] = 58,i--, j++ ;
rtc_dec = (( rtc_bcd[i] & B1111000) >> 4) * 10 + (rtc_bcd[i] & B00001111); // hours 2 , mins 1 , sec 0
if ( rtc_dec <= 10 ) bcd_string[j] = 48;
bcd_string[j] = (rtc_dec / 10 )+48;j++;bcd_string[j]=(rtc_dec % 10)+48;j++;bcd_string[j] = 58,i--, j++ ;
rtc_dec = (( rtc_bcd[i] & B1111000) >> 4) * 10 + (rtc_bcd[i] & B00001111); // hours 2 , mins 1 , sec 0
if ( rtc_dec <= 10 ) bcd_string[j] = 48;
bcd_string[j] = (rtc_dec / 10 )+48;j++;bcd_string[j]=(rtc_dec % 10)+48;
} // end of bcd2string
int availableMemory() {
int size = 1024;
byte *buf;
while ((buf = (byte *) malloc(--size)) == NULL);
free(buf);
return size;
}
void int_alarm()
{
strcpy(bcd_string , "12:00:00");
string2bcd();
for(int j=0; j<3; j++) {
for(int i=0; i<3; i++) {
alarm_on[i][j] = rtc_bcd[i];
alarm_off[i][j] = rtc_bcd[i];
} alarm_set[j] = false; }
} // end of int_alarm
void display_alarms(int j) // 0 1 2
{ for(int i=0; i<3; i++) rtc_bcd[i] = alarm_on[i][j];
Serial.print("Alarm "),Serial.print(j,DEC);
if ( alarm_set[j] == true ) { Serial.print(" - on - "); } else { Serial.print(" - off - "); }
Serial.print("Alarm on time ");
bcd2string(),Serial.print(bcd_string);
for(int i=0; i<3; i++) rtc_bcd[i] = alarm_off[i][j];
Serial.print(" - Alarm off time ");
bcd2string(),Serial.println(bcd_string);
} // end of display_alarm
void check_alarms()
{ clock_read(); bcd2string(); strcpy(clock_string , bcd_string);
for(int j=0; j<3; j++)
{ if ( alarm_set[j] == true ) {
for(int i=0; i<3; i++) { rtc_bcd[i] = alarm_on[i][j] ; }
bcd2string();
if (strcmp ( bcd_string , clock_string ) == 0) alarm_trigger_on(j);
for(int i=0; i<3; i++) { rtc_bcd[i] = alarm_off[i][j] ; }
bcd2string();
if (strcmp ( bcd_string , clock_string ) == 0) alarm_trigger_off(j);
}}} //end of check_alarm
void set_alarm(int alarm , boolean on_alarm , char alarm_time[] )
{ strcpy(bcd_string , alarm_time); string2bcd();
for(int i=0; i<3; i++)
{ if ( on_alarm == true ) { alarm_on[i][alarm] = rtc_bcd[i];
} else { alarm_off[i][alarm] = rtc_bcd[i];
}} }// alarm_set[alarm] = true; } // end of set_alarm
void alarm_trigger_on(int alarm)
{
// switch (alarm) {
// case 0:
// do something when var == 1
// break;
// break is optional
// case 1:
// do something when var == 2
knight_run = true;
write_io(0,1);
// break;
// case 2:
// do something when var == 2
// break;
//}
}
void alarm_trigger_off(int alarm)
{
// switch (alarm) {
// case 0:
// do something when var == 1
// break;
// case 1:
// do something when var == 2
knight_run = false;
write_io(0,0);
// break;
// case 2:
// do something when var == 2
// break;
//}
}
void io_int()
{
do {
x = read_io(6),write_io(6,0);
} while ( x != 0 );
do {
x = read_io(6),write_io(6,255);
} while ( x != 255 );
do {
x = read_io(6);
write_io(6,0),write_io(7,255); // all outputs / all inputs
} while ( x != 0 );
write_io(0,0);
} // end of int_io
int read_io(int cmd_reg)
{
int tmp;
Wire.beginTransmission(io_address);
Wire.send(cmd_reg); // 0 or 1 -- GP0 or GP1
Wire.endTransmission();
Wire.requestFrom(io_address, 1);
tmp=Wire.receive();
return tmp;
} // end of read_io
void write_io(int cmd_reg, int push_this)
{
Wire.beginTransmission(io_address);
Wire.send(cmd_reg),Wire.send(push_this); // reset register pointer then set GP0 to push_this // use (0,??)
Wire.endTransmission();
} // end of write_io
void knight()
{
x = read_io(0);
if (forward == true)
{
if ( x == B10000000 )
{
// forward = true & x = B10000000
forward= false;
digitalWrite(ledPin, HIGH);
}
else
{
// forward = true & x not B10000000
x = x << 1;
write_io(0,x);
}
}
else
{
if ( x == B00000001 )
{
// forward = false & x = B00000001
forward=true;
digitalWrite(ledPin, LOW);
}
else
{
// forward = false & x not B10000000
x = x >> 1;
write_io(0,x);
}
}
} // end of knight