Go Down

Topic: Program only runs if Serial.begin and Serial.println are present (Read 721 times) previous topic - next topic

lolwut

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
Code: [Select]
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:
Code: [Select]

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/wGdTVQc0
Here is the display I'm using, and this is the clock chip.

bubulindo

If you don't start the Serial port (by using begin()), the serial port will never work. Have you read the manual entry for it?

Are you sure your code stops only when you don't put the serial.print() in it? Couldn't this be a coincidence? Can the test for the light be interfering? Have you tested this with both true and false conditions?

By the way, if you go through the ASCII table you'll realize that what " -'0' " does.
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

lolwut


If you don't start the Serial port (by using begin()), the serial port will never work.

I don't need the Serial to work in the final project, it's just there for debugging. I want to get rid of it for the final version, but I can't or it else stops working.

Quote
Are you sure your code stops only when you don't put the serial.print() in it? Couldn't this be a coincidence? Can the test for the light be interfering? Have you tested this with both true and false conditions?

It stops when Serial.println() and Serial.begin() are gone as well, which makes it seem like the program is somehow relying on it for something. All that the test for light does is reads an analog value and returns a boolean, so I'm not sure how that could be an issue. As for testing true/false conditions, it works correctly when the serial function are in the right place, otherwise, the program locks up (time stops being read, sensor doesn't respond).

el_supremo

Quote
Here is the relevant code

The relevant code is always the bit you left out.

In your case, your mods to ReadTimeDate will cause trouble.
This is the relevant bit:
Code: [Select]

String ReadTimeDate(){
String temp;
.
.
.
return(temp);
}

You can't declare a string locally in the function and then return it as the value of the function. It is created on the stack when you enter the function and it is destroyed when you leave. The pointer to the string will point to random garbage.
It is really not a good idea to use the String library. It has one or more known bugs which make it a losing proposition. You will be much better off to do the string manipulation your self.
So, don't use the string library at all and don't pass temporary variables as the value of a function.

Pete

lolwut


You can't declare a string locally in the function and then return it as the value of the function. It is created on the stack when you enter the function and it is destroyed when you leave. The pointer to the string will point to random garbage.
It is really not a good idea to use the String library. It has one or more known bugs which make it a losing proposition. You will be much better off to do the string manipulation your self.
So, don't use the string library at all and don't pass temporary variables as the value of a function.

Pete


Wow, I would have never figured that out. I just declared temp globally and assigned/concatenated the values in the function to compensate. I can now remove the Serial functions, or put println() wherever. Thank you!

el_supremo

Excellent.
But, it would still be a good idea not to use the String library at all. It's a "gotcha" just waiting to creep up on you when it is least convenient.

Pete

Go Up