From p to Arduino: wth - final? Array[][]....

edited July 2014 in Arduino

hello all,

I am just starting with arduino.

I just try to port a program from p5 to arduino and get lots of errors .

1st I use final int to declare const and use them in switch

aduino seems not to know final, but when I delete final, my switch doesn't work. So how can I declare a const? (I mean I could use 0 and 1 and 2 in the switch but urghs...)

2nd: I use lot of 2D arrays, but the [][] puzzles the Arduino compiler... how can I do a 2D array here?

Thanks a lot!

Chrisir ;-)

Tagged:

Answers

  • It's important to remember that Arduino programming isn't Processing/Java, it's C (and C++ I think). Here are a couple of pages I Googled up:

    http://www.tutorialspoint.com/cprogramming/c_constants.htm

    http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

  • edited July 2014

    thanks

    it's really a pain...

    First

    is that correct:

    int SpielfeldGlobal[intConstEnde] [intConstEnde]; // array

    Second

    I got a lot of errors I don't understand. Therefore let me ask please:

    Do tabs play any other role in arduino than in p5?

    I mean when I declare something with global scope on tab 1, is it valid in other tabs?

    Does white space play any role? Does a white space (empty space) cause an error somehow? Because some errors don't make any sense otherwise...

    Thanks a lot !

    Chrisir

    SolitaireSolver7:36: error: expected `]' before ';' token
    SolitaireSolver7:36: error: expected `)' before ';' token
    SolitaireSolver7:36: error: expected unqualified-id before ']' token
    SolitaireSolver7:36: error: expected unqualified-id before ']' token
    SolitaireSolver7:38: error: expected unqualified-id before '[' token
    SolitaireSolver7:45: error: expected unqualified-id before '[' token
    SolitaireSolver7:52: error: expected unqualified-id before '[' token
    SolitaireSolver7:95: error: expected `]' before ';' token
    SolitaireSolver7:95: error: expected unqualified-id before ']' token
    SolitaireSolver7:95: error: expected unqualified-id before ']' token
    Game1.ino: In function 'void SolveGame()':
    Game1:35: error: 'SpielfeldGlobal' was not declared in this scope
    Game1:38: error: 'SpielRekursiv' was not declared in this scope
    c:/program files (x86)/arduino/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h: In function 'void DefiniereStartUndZielstellung()':
    c:/program files (x86)/arduino/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:278: error: too few arguments to function 'void exit(int)'
    Game1:69: error: at this point in file
    Game1:76: error: 'SpielfeldGlobal' was not declared in this scope
    Game2.ino: At global scope:
    Game2:7: error: expected `]' before ';' token
    Game2:7: error: expected `)' before ';' token
    Game2:7: error: expected constructor, destructor, or type conversion before ';' token
    Game2:7: error: expected unqualified-id before ']' token
    Game2:7: error: expected unqualified-id before ']' token
    
  • The declaration looks good.

    It looks to me like Arduino does not combine multiple files as Processing does. (I've never tried it, so I can't be sure.)

    Whitespace shouldn't matter.

    I'd suggest trying the Arduino programming questions in the forum at http://forum.arduino.cc/ . You're more likely to get folks more knowledgeable than I am.

  • edited July 2014

    Closest match for Java's final for C is const. B-)
    Alternatively, we can define constants using the pre-processor's directive: #define. *-:)
    I wish Processing's own pre-processor had at least #define & #ifdef. :o3

  • Answer ✓

    int SpielfeldGlobal[intConstEnde] [intConstEnde];

    As far as I remember, C arrays are statically compiled. Therefore we can't use variables to define its length! :O
    Rather, we can #define constants like:

    #define GRID 32
    int matrix[GRID][GRID];
    

    Dunno how correct is the snippet above since it's been ages I've programmed in C! ~:>

    Another option is more advanced, using malloc() in order to separate a dynamically contiguous block of memory in the heap!

  • thanks a lot, I use #define now

    but the other bugs remain

    It worries me that the arduino IDE supports tabs but the compiler doesn't.....

    also the line numbers of the error messags are totally off (because of comments and empty lines)

  • edited September 2014

    I solved it somehow replacing the 2D array with a 1D array

    it was not as hard because I used a function that converted my former 2 indexes for 2D into 1 index for 1D

    myNew1DArray [  convertIndexes ( x,y )   ]............
    
    
    int convertIndexes ( int x,int y ) {
        return  y*widthOfFormer2DArrax + x; 
    }
    

    so when calling functions using x and y I can still use x and y

    so that made it easy to rewrite the sketch going from 2D to 1D array

    Thanks for all the help.

    Chrisir

Sign In or Register to comment.