Function not executed

This forum is for all topics related to Simulator for Arduino v0.98. Please include sample code where possible.

Moderator: Adrian

durpex
Posts: 2
Joined: Fri Mar 08, 2013 6:47 am

Function not executed

Postby durpex » Fri Mar 08, 2013 7:00 am

Hi,

I try to execute the following code using the pait version of the simulator:

Code: Select all

// Saved by Simulator for Arduino V0.98C refer to arduino.com.au

void setup(){
Serial.begin(9600);
Serial.println("a");
 int unsorted[] = {3,2,8,6,-1,5,1,7,4};
Serial.println("b");
 int sorted[] = bubbleSort(unsorted, 9);
Serial.println("c");
}

void loop(){

}

int[] bubbleSort(int numbers[], int array_size)
{
  int i, j, temp;
    Serial.println("bubble");
  for (i = (array_size - 1); i > 0; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (numbers[j-1] > numbers[j])
      {
        temp = numbers[j-1];
        numbers[j-1] = numbers[j];
        numbers[j] = temp;
      }
    }
  }
printArray(numbers,array_size);

}

void printArray(int[] a, int n){
Serial.println(" === array === ");
for(int i=0; i<n; i++)
   Serial.println(a[i])
}

The output in the Serial Window is a,b,c
but nothing about the bubbleSort method being called.
No exception or anything. Is this a bug in the simulator?

Simulator_admin
Site Admin
Posts: 239
Joined: Thu Feb 02, 2012 6:07 pm

Re: Function not executed

Postby Simulator_admin » Fri Mar 08, 2013 8:57 am

Hi, is it possible to rewrite this sketch so that the array is a global variable. I am not sure if it is OK to pass a variable as an array and pointers are not yet supported in the Simulator.

If the brackets are removed ([]), the Simulator will jump to the subroutine. Maybe something like the code below:

Code: Select all

// Saved by Simulator for Arduino V0.98C refer to arduino.com.au
int numbers[9] = {3,2,8,6,-1,5,1,7,4};

void setup(){
Serial.begin(9600);
Serial.println("a");
 int unsorted[] = {3,2,8,6,-1,5,1,7,4};
Serial.println("b");
// int sorted[] =
bubbleSort(9);
Serial.println("c");
}

void loop(){

}

void bubbleSort( int array_size)
{
  int i, j, temp;
    Serial.println("bubble");
  for (i = (array_size - 1); i > 0; i--)
  {
    for (j = 1; j <= i; j++)
    {
      if (numbers[j-1] > numbers[j])
      {
        temp = numbers[j-1];
        numbers[j-1] = numbers[j];
        numbers[j] = temp;
      }
    }
  }
printArray(9);

}

void printArray( int n){
Serial.println(" === array === ");
for(int i=0; i<n; i++)
   Serial.println(numbers[i])
}




Here is the output
a
b
bubble
=== array ===
-1
1
2
3
4
5
6
7
8

durpex
Posts: 2
Joined: Fri Mar 08, 2013 6:47 am

Re: Function not executed

Postby durpex » Sat Mar 09, 2013 7:51 pm

Hi,

well that is bad. Is there any timeframe when passing arrays and pointers are supported?

I also noticed a new odd behaviour:

Code: Select all

int a=5;
  for(int i=0;i<a;i++){
    for(int j=0;j<a;j++){
      if((i==0) || (i==a-1)){
     Serial.print("*");
      }else{
        if((j==0) || (j==a-1)){
       Serial.print("*");
     }else{
          Serial.print(" ");
        }
      }
    }
    Serial.println();
  }

For the above code I get an error
Unknown command on Line 43: }else{ }else{
.
But this is valid code and compiles fine using the Arduino IDE.

Also it would be nice if the editor shows linenumbers, otherwise it is quite useless to
show them in the error message.

User avatar
Adrian
Super user
Posts: 56
Joined: Thu Jan 10, 2013 10:22 pm

Re: Function not executed

Postby Adrian » Fri Mar 15, 2013 8:37 pm

Hi durpex,

With regard to }else{, this is a known issue and will be fixed, however a quick workaround is to ensure that the else in preceded with a new line thus:

Code: Select all

}
else{

or

Code: Select all

}
else
}


so...

Code: Select all

    // Saved by Simulator for Arduino V0.98C refer to arduino.com.au

    void setup(){
    Serial.begin(9600);
    }

    void loop(){
    int a=5;
      for(int i=0;i<a;i++){
        for(int j=0;j<a;j++){
          if((i==0) || (i==a-1)){
         Serial.print("*");
          }
           else{                {
            if((j==0) || (j==a-1)){
           Serial.print("*");
         }
           else{
              Serial.print("-");
            }
          }
        }
        Serial.println();
      }

    }

I hope that helps in the meantime

Your point regarding line-numbers is well taken although the line numbers are shown in the status bar below the code window

Kind regards
Adrian
Apologies for being opinionated - it's nothing personal, feel free to complain.
usual system: Pro V0.98 Arduino Mega 2560 Samsung i5-3210M 2.5Ghz 6GB windows home premium SP1

Simulator_admin
Site Admin
Posts: 239
Joined: Thu Feb 02, 2012 6:07 pm

Re: Function not executed

Postby Simulator_admin » Tue Mar 19, 2013 9:27 pm

Thanks Adrian. Also, just to confirm we have finally fixed up this }else{ error and this will be updated in the new version.

With line numbers in the Editor, this has been asked for before, but at this stage it requires a lot of work. We have implemented the change to open the Editor at the same point in the sketch.

With the passing arrays as arguments to subroutines, we did try to compile this in Arduino and it gave a lot of errors. I am pretty sure arrays cannot be passed as arguments to subroutines. Pointers can but this means adding a RAM memory array, and then implementing this for all identifiers and objects.


Return to “Version 0.98”

Who is online

Users browsing this forum: Google [Bot] and 1 guest