Strange problem with int array and arduino-tvout library

i have an issue that makes no sense at that uses a int array and the http://code.google.com/p/arduino-tvout/ arduino tvout library.

i have this method below which creates a space background(like space invaders etc) which displays it on the tv by using random x/y coordinates from the random method. it works great.

void GenSpace()
{
  int x1;
  int y1;
  for (int a=0; a<100; a++)
  {
    x1=random(TVresW); //TVresW = tv resolution  width
    y1=random(TVresH);  //tv resolution height
    TV.set_pixel(x1,y1,2); //draw pixel on tv 

    delay(35);
  }
}

but then i also want to store all the X/Y points so that i can redisplay this “space” later in the program:

void GenSpace()
{
  int x1;
  int y1;
  for (int a=0; a<100; a++)
  {
    x1=random(TVresW);
    y1=random(TVresH);
    TV.set_pixel(x1,y1,2);

    //put space background in storage
    Space[a] = x1;
    Serial.print(Space[a]);

    Serial.print(",");
    a++; //go to next value to store y1

    Space[a] = y1;
    Serial.println(Space[a]);

    delay(35);
  }
}

after running this, with no other changes besides the Space Array to the program, the arduino doesn’t display anything on the TV, but the sketch is still functioning(didnt freeze + reset) according to serial output. i also read back all the values from the Space Array without any issues to the serial terminal, so its not an array issue. Somehow the array prevents the TVout from working?! it doesnt make sense…if i comment out this:

Space[a] = y1;
Serial.println(Space[a]);
Space[a] = x1;
Serial.print(Space[a]);

it works fine

this is what i used to print the Space Array values back to terminal and redisplay the “space” on the TV.

void DrawSpace()
{
  for (int a=0; a<100; a++)
  {   
    //draw previously stored space  
    TV.set_pixel(Space[a],Space[a+1],1);
    
    Serial.print(Space[a]);
    Serial.print(",");
    a++;
    Serial.println(Space[a]);
  }
}

so basically, the program works exactly how it should except it deosnt display anything on the TV when the array part is implemented…

thanks for any input!

the whole code:

/*
RCA video plug:
 Sync(Pin 9) -> 1k -> center prong
 Video(pin 7) -> 470ohm -> center prong
 Gnd -> outside casing
 */

#include <TVout.h>
#include <fontALL.h>

TVout TV;
#define TVresW 120
#define TVresH 96

#define MaxRight (TVresW-8)
#define MaxDown (TVresH -7)

//8 pixels wide,7 pixels high space ship bitmap
PROGMEM const unsigned char Ship[] = {
  8,7,
  0x18,
  0x18,
  0x3C,
  0xBD,
  0xBD,
  0xFF,
  0xFF};

int CurXPos = (TVresW-8)/2; //centers ship horizontally
int CurYPos = TVresH -7; //(total height) - 7(ship height), moves ship to bottom

#define XaxisPin A0
int Xval=0;
#define YaxisPin A1
int Yval=0;

int Space[200];

void setup()
{
  Serial.begin(115200);
  TV.begin(NTSC,TVresW,TVresH);
  randomSeed(analogRead(A3)); //for random numbers
  delay(1000); //wait for TV to detect input

  TV.select_font(font8x8); //each char is 8x8 pixels
  TV.print((TVresW-80)/2,(TVresH-8)/2,"Ship Test!"); //80 pixel long message, 8 tall
  GenSpace();
  TV.bitmap(CurXPos,CurYPos,Ship);
  delay (1000);

  /*
   for (int i=0; i<128; i++)
   {
   TV.clear_screen();
   TV.bitmap(i,30,Ship);
   delay(250);
   }
   */
}
void loop()
{  
  GetSticks();
  MoveShip();   
}

void GetSticks()
{
  Yval = analogRead(YaxisPin);
  Xval = analogRead(XaxisPin);

  if(Xval < 512) //move left
  {
    Xval = map(Xval,512,0,0,-4);
  }
  else//its >= 512
  {
    Xval = map(Xval,512,1023,0,4); //move right
  }

  if(Yval < 512) //move down
  {
    Yval = map(Yval,512,0,0,4);
  }
  else//its >= 512 //move up
  {
    Yval = map(Yval,512,1023,0,-4); 
  }
}

void MoveShip()
{
  //get new pos
  CurXPos += Xval;
  CurYPos += Yval;

  if (CurXPos > MaxRight) //check horizontal limits 
  {
    CurXPos = MaxRight;
  }
  else if(CurXPos < 0)
  {
    CurXPos = 0;
  }

  if (CurYPos > MaxDown) //check vertical limits 
  {
    CurYPos = MaxDown;
  }
  else if(CurYPos < 0)
  {
    CurYPos = 0;
  }
  //clear screen + display new ship
  TV.clear_screen();
  //DrawSpace();
  TV.bitmap(CurXPos,CurYPos,Ship);

  Serial.print(CurXPos);
  Serial.print(" ");
  Serial.println(CurYPos);

}

void GenSpace()
{
  int x1;
  int y1;
  for (int a=0; a<100; a++)
  {
    x1=random(TVresW);
    y1=random(TVresH);
    TV.set_pixel(x1,y1,2);

    //put space background in storage
    Space[a] = x1;
    Serial.print(Space[a]);

    Serial.print(",");
    a++; //go to next value to store y1

    Space[a] = y1;
    Serial.println(Space[a]);

    delay(35);
  }
}

void DrawSpace()
{
  for (int a=0; a<100; a++)
  {   
    //draw previously stored space  
    TV.set_pixel(Space[a],Space[a+1],1);
    
    Serial.print(Space[a]);
    Serial.print(",");
    a++;
    Serial.println(Space[a]);
  }
}

Why are you incrementing a in the body of the for loop, in GenSpace? a is the for index. Incrementing it multiple times will cause the loop to end after writing only 100 values to the 200 element array.

Your 200 element array is using 400 bytes of SRAM. You may not have 400 bytes to spare. The values being stored in the array are byte sized. Why is the array not byte sized?

Why are you incrementing a in the body of the for loop?

because i use two spots in each loop(one for x and one for y)

Your 200 element array is using 400 bytes of SRAM.

it was 200 from a previous test, i just didn't change it.

You may not have 400 bytes to spare.

ah, im guessing its a memory issue!

so ill change it to a byte array and try that.

because i use two spots in each loop(one for x and one for y)

Sure, but you are ending the loop when a is 100, not 200.

Like Paul states, you should not change your for loop index within the for loop.

Change it to something like:

    Space[a+1] = y1;
    Serial.println(Space[a+1]);

You may need to adjust your for loop to account for a+1, when a=100.

If you really need to increment a, assign it to another variable, and increment it instead:

  int b = a;
  b++;

I'd like to see the final code. I need to test my TVout cable again. 8)

-transfinite

Sure, but you are ending the loop when a is 100, not 200.

it was 200 from a previous test, i just didn’t change it. ← i said that :slight_smile:

but thanks for the help! i totally forgot about memory requirements!

'd like to see the final code. I need to test my TVout cable again

sure thing!