Shop OBEX P1 Docs P2 Docs Learn Events
Transferring a 1x25 array from one stamp to another — Parallax Forums

Transferring a 1x25 array from one stamp to another

CP223CP223 Posts: 5
edited 2013-05-05 21:49 in BASIC Stamp
So i have a final project I have spent weeks on, im just about done when I get an "EEPROM Full" error, so my idea is send the array that 1x25 to a second stamp. The project is create a 5x5 grid and place two ships (1x3 and 1x2) randomly, becasue Im not a coder i used a random number seed to get a number from 0-24 and a lot of if then statements to place the ships. there is an array from 0-24 indicating a spot on the grid. Each grid is initialized at 0 meaning open, then the array is changed to 1 if the ship is placed there. I want to send this to a second stamp. THE PROJECT IS DUE TOMORROW so please help!

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-05-05 09:11
    Maybe some streamlining of your code can be done to free up some memory.

    Maybe you can make better use of the EEPROM for the 5x5. Are you using a location for each of the 25 places?
  • CP223CP223 Posts: 5
    edited 2013-05-05 09:17
    Sorry you will have to be more specific with me. I have used a variety of codes but I dont know the stamp much further than loops and if statements. the code is extreamly basic because I cant use a 2-D array/matrix as in Matlab. So honestly, Im not ever sure what the EEPROM is other than memory space or a maximum number of commands .
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-05-05 09:38
    How are you storing the 5x5 positions in the EEPROM?
  • CP223CP223 Posts: 5
    edited 2013-05-05 09:48
    FOR idx = 0 TO 24
    Grid(idx) = 0
    NEXT
    FOR idx = 0 TO 24


    NEXT








    ' runs through random numbers until player hits begin


    DO
    randval=randval+1
    randval2=randval2 +1


    DEBUG ? Ship1
    RANDOM randval
    RANDOM randval2


    'Ship1 VAR Byte
    'Dir VAR Nib


    Ship1 = randval//15
    Dir = randval//2
    Ship2 = randval2//21








    LOOP UNTIL IN10=1




    'placing ship1


    IF Ship1=0 AND Dir=0 THEN
    FOR idx=Ship1 TO (Ship1+2)
    Grid(idx) = 1
    NEXT
    Grid(11)=2
    Grid(10)=2


    ELSEIF Ship1=1 AND Dir=0 THEN
    FOR idx =Ship1 TO (Ship1+2)
    Grid(idx) = 1
    NEXT
    Grid(16)=2
    Grid(21)=2

    I had a var that indicates direction called DIR if 0 its horizontal if 1 its vertices. I would have 14 possibilities for each and two orientations, therefore 28 plus the same for the 1x2 ship.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-05-05 09:59
    The "EEPROM Full" error means that the programme you've created is too big to stuff into the Stamp.

    I thought that you were writing the 5x5 into the EEPROM.
    You are putting it in RAM.
    Store the ships in RAM as bits, don't use a whole BYTE for one position.
    Grid VAR Byte(5)
    Grid(0) = %00000000
    Grid(1) = %00000000
    Grid(2) = %00000000
    Grid(3) = %00000000
    Grid(4) = %00000000
    

    Here are how a couple of ships could be rendered:
    Grid(0) = %00001110
    Grid(1) = %00000000
    Grid(2) = %00001000
    Grid(3) = %00001000
    Grid(4) = %00000000
    
  • CP223CP223 Posts: 5
    edited 2013-05-05 10:51
    i guess im confused, my intention is to have each grid point equal a value 1-4, is EEPROM error due to not enough memory because the total, nib, byte ext amount is to high r is it a maximum number of commands you can give. Im going to play with the idea you gave me some and see if I can understand it more
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-05-05 11:17
    "The project is create a 5x5 grid and place two ships (1x3 and 1x2) randomly,"
    But wait, there's more:
    "my intention is to have each grid point equal a value 1-4"
    Still, you don't need a byte for each of those.

    "is EEPROM error due to not enough memory because the total, nib, byte ext amount is to high r is it a maximum number of commands you can give."
    The RAM size doesn't change, it's always allocated, it's separate from the EEPROM (where the programme is kept).
    EEPROM Full is telling you that your programme is too big.
  • CP223CP223 Posts: 5
    edited 2013-05-05 11:58
    6.Battleship (Difficulty: 9)
    The BASIC Stamp generates an ocean (a 5
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-05-05 12:21
    Is there a question?
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-05 21:34
    You could declare Grid as a nibble array as PJ mentioned. 25 elements (from 0-24) would provide for values from 0-15 which would accomplish what you need. That would only take 13 bytes which will fit in the limited space available in the variable's RAM (a total of 26 bytes).

    There's no magic way to write this program. It's a complex problem. You have to write it and get it to work in pieces, then optimize it so it will fit in the limited space available (2K bytes for code and 26 bytes for variables). It's not something you can do like this with an all-nighter. Adding a 2nd Stamp won't help. Usually adding a 2nd microprocessor makes a problem like this more complex, not simpler. That's why this is a Difficulty 9 problem.

    Using lots and lots of IF statements tends to make a program pretty large. Usually to save space, you have to program smarter, use tables where they make sense with the tables stored in EEPROM either as LOOKUP statements or using DATA and READ statements. Use subroutines where you can to eliminate duplicate decision making (and duplicate code).

    I don't understand why you can't use a bit array. The smaller ship will occupy 2 adjacent cells while the larger ship will occupy 3 adjacent cells and a cell is either occupied by part of a ship or unoccupied.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-05 21:49
    If you were to use a 2nd Stamp, you'd use a loop and a SEROUT statement to transmit the 25 values (0 and 1) to the 2nd Stamp. You'd probably start the sequence with a "opening" character like a "[" and terminate with a closing character like a "]" just for error checking something like this for the transmitting end

    SEROUT <pin>,<Baud>,["["]
    FOR i = 0 to 24
    SEROUT <pin>,<Baud>,[BIN1 Grid(i)]
    NEXT i
    SEROUT <pin>,<Baud,["]"]

    and this sort of thing on the receiving end

    SERIN <pin>,<Baud>,[WAIT("[")]
    FOR i = 0 to 24
    SERIN <pin>,<Baud>,[BIN1 Grid(i)]
    NEXT i
    SERIN <pin>,<Baud>,[checkChar]
    IF checkChar <> "]" THEN
    DEBUG "Error",CR,LF
    STOP
    ENDIF

    <pin> is the I/O pin number that you're using and <Baud> is the Baud constant needed. Typically you'd use normal / 2400 Baud / 8 bit and the constants are given in the Stamp Reference Manual. When you connect two Stamps together like this, you usually place a resistor between the two I/O pins to prevent damage if you make a programming error. 3.3K should work.
Sign In or Register to comment.