I've been working on some code for a digital clock, and along the way I've had some issues with my program not working correctly due to whether Serial.begin and Serial.println were in my code. If Serial.begin(9600) wasn't in the setup() function, it would go through the code once and stop. So ever since, I have just kept it in to make it work.
Now, my program will only run if Serial.begin(9600) is in the setup() function and Serial.println(*anything*) is the first line of code in the loop() function. If it is anywhere after
num = ReadTimeDate();
it will freeze up and only run once.
Before I post all of my code, i think it
may have something to do with the SoftwareSerial or the SPI used with the time chip I'm using. Here is the relevant code:
SoftwareSerial seg = SoftwareSerial(8,7);
void setup() {
pinMode(7,OUTPUT); //set seven segment display data pin
seg.begin(9600);
RTC_init(); //initialize real-time clock
Serial.begin(9600); //this MUST be in the code for it to run for some reason
}
void loop() {
Serial.println(ReadTimeDate()); //this MUST be here as well
num = ReadTimeDate(); //get time (program stops running if Serial.println(ReadTimeDate()) goes after this line)
.
.
.
}
//THIS WAS COPY AND PASTED FROM SAMPLE CODE, NOT DONE BY ME
int RTC_init(){
pinMode(cs,OUTPUT); // chip select
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3); // both mode 1 & 3 should work
//set control register
digitalWrite(cs, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(cs, HIGH);
delay(10);
}
//Also sample code
String ReadTimeDate(){
digitalWrite(cs, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);
digitalWrite(cs, HIGH);
.
.
.
}
If more is needed, the rest is found here:
http://pastebin.com/wGdTVQc0Here is the display I'm using, and
this is the clock chip.