Pages: [1]   Go Down
Author Topic: arduino shuts down.. serial overflow?  (Read 555 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I am using the TVout library, though I'm not sure you need to know much about it to solve my problem. Basically the arduino program works for about 1 second then shuts off. Is this a serial overflow or something?

Code:
#include <TVout.h>
#include <video_gen.h>
#include <stdlib.h>   

//TV STUFF
TVout TV;

int x;

void setup()
{
    // init TV
    TV.begin(NTSC,128,96);
 
    Serial.begin(115200);
   
    x = 0;
}
void loop()
{
TV.fill(0);
   if (Serial.available ())
    {
    TV.draw_circle(Serial.read() - 6, Serial.read() - 6, 6, 1 ); 
   
    // onto next bodypart
    if (++x >= 2)
      {
      x = 0;
      }  // end y wrap-around
     
    } // end available
       Serial.flush();
  // do other stuff here like test switches
 
}  // end of loop
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25856
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check to see if you've received a character, then read both of them.
Not good.
What happens when serial.read returns -1 into your library?

Edit: The importance of using Serial.available was pointed out to you a couple of weeks ago.
« Last Edit: December 05, 2011, 02:56:04 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
       Serial.flush();
Why?
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6137
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So I am using the TVout library, though I'm not sure you need to know much about it to solve my problem. Basically the arduino program works for about 1 second then shuts off. Is this a serial overflow or something?
What do you mean by "shuts off"?  Does your program seem to stop running or does the Arduino board actually manage to power down?

Code:
   if (Serial.available ())
    {
    TV.draw_circle(Serial.read() - 6, Serial.read() - 6, 6, 1 ); 
You should really be checking that Serial.available() is greater than 0.  While syntax is correct, you should keep in mind that Serial.available returns the number of characters in the serial receive buffer.

As AWOL points out, you are calling Serial.read() twice.  Each call to Serial.read() removes 1 byte from the buffer.  So if there was only one byte in the buffer, the 2nd call to Serial.read() will result in -1.  

Either you meant to read 2 bytes in which case you could wait until (Serial.available() > 2), or you wanted to use the same byte twice in which case you should create a variable like char byteIncoming = Serial.read().


Code:
 Serial.flush();
There really is no reason to call Serial.flush().  And as of 1.0 flush actually flushes the transmit buffer, not the receive buffer.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 95
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What AWOL means is that Serial.available() only ensures you that there is *one* byte of data available from the serial port. Nevertheless, you are reading 2 bytes (each Serial.read() will read 1). You need to gather 2 bytes before issuing your TV command.
Logged


Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another question. What is sending data to the serial port? Is that program sending data in binary, or as strings?

If you are using the Serial Monitor to send data to the Arduino, the data is sent as a string. You must collect the whole string, and parse it, and convert the tokens to ints.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, thank you. Changed it to Serial.available() > 1, works great. As always, I love this forum and sorry for asking such basic questions.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    TV.draw_circle(Serial.read() - 6, Serial.read() - 6, 6, 1 ); 

This is really bad. How do you know that the compiler doesn't decide to put the function call for the second argument (Serial.read()) before the first argument (Serial.read() - 6)? This actually results in what's called undefined behavior source, point 4 where the compiler is allowed to do it either way, meaning that it may do what you want for this compiler, or maybe just this *version* of this compiler, or maybe even just for this particular sketch (where the registers line up just right), but may change at any point.

Use a temp variable instead:
Code:
char temp = Serial.read();
TV.draw_circle(temp - 6, Serial.read() - 6, 6, 1 );
Logged

Pages: [1]   Go Up
Jump to: