Arrays and Serial.print ()

hey guys , I’m trying to program an obstacle avoiding mobile robot which uses fuzzy logic controller to avoid obstacles.

I’m using an uno board.

in my code there are some arrays which work well except two !
lftm and rhtm !

I’ve put data into these arrays and I want to read data and print them using Serial.print () . but serial monitor doesn’t show anything !

when I try to print data inside the loops ( Where the numbers are being written into the arrays ) everything works well but outside the loops ( when all the numbers have been written into the arrays ) Serial.print () prints nothing !

Your help is really appreciated since I’m in hurry with this project and I had been trying to solve it for a long time already and still can’t get any idea.

int left_us = 37.5 ; // The output of Left Ultrasonic Sensor
int front_us = 30 ; // The output of Front Ultrasonic Sensor
int right_us = 25 ; // The output of Right Ultrasonic Sensor
//float mu[3][27] ;

void setup() {

Serial.begin(9600);

}

void loop() {

int close_left , nfar_left , far_left , close_front , nfar_front , far_front , close_right , nfar_right , far_right , minmu ;
int cur1 , cur2 , lftnum , numrht , derht , delft , centr ;
int lftm [28][101];
int rhtm [28][101];
float arrfin [101] ;

// Generate the membership function values for “Left_us”:

close_left = trapz (0 , 0 , 20 , 40 , left_us ) ; // Close
nfar_left = trapz (20 , 40 , 60 , 80 , left_us ) ; // NotFar
far_left = trapz (60 , 80 , 100 , 100 , left_us ) ; // Far

// Generate the membership function values for “Front_us”:

close_front = trapz (0 , 0 , 20 , 40 , front_us ) ; // Close
nfar_front = trapz (20 , 40 , 60 , 80 , front_us ) ; // NotFar
far_front = trapz (60 , 80 , 100 , 100 , front_us ) ; // Far

// Generate the membership function values for “Right_us”:

close_right = trapz (0 , 0 , 20 , 40 , right_us ) ; // Close
nfar_right = trapz (20 , 40 , 60 , 80 , right_us ) ; // NotFar
far_right = trapz (60 , 80 , 100 , 100 , right_us ) ; // Far

// Rule generator:

int mu[3][27] = {
{ close_left , close_left , close_left , close_left , close_left , close_left , close_left , close_left , close_left , nfar_left , nfar_left , nfar_left , nfar_left , nfar_left , nfar_left , nfar_left , nfar_left , nfar_left , far_left , far_left , far_left , far_left , far_left , far_left , far_left , far_left , far_left } ,
{ close_front , close_front , close_front , nfar_front , nfar_front , nfar_front , far_front , far_front , far_front , close_front , close_front , close_front , nfar_front , nfar_front , nfar_front , far_front , far_front , far_front , close_front , close_front , close_front , nfar_front , nfar_front , nfar_front , far_front , far_front , far_front } ,
{ close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right , close_right , nfar_right , far_right }
};

int rule [2][27] = {
{1 , 2 , 3 , 1 , 2 , 3 , 3 , 2 , 4 , 1 , 3 , 2 , 1 , 2 , 3 , 1 , 3 , 3 , 1 , 1 , 4 , 2 , 3 , 3 , 4 , 2 , 4 } , // Left_Motor Rule
{1 , 1 , 1 , 1 , 1 , 2 , 3 , 3 , 3 , 2 , 2 , 1 , 3 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 1 , 4 , 4 , 4 , 2 , 4 , 4 } // Right_Motor Rule

};

for (int i = 0 ; i <= 26 ; i++) {

minmu = func3min (mu[0] , mu[1] , mu[2]) ;
* for (int j = 0 ; j <= 100 ; j++ ) {*
_ if ((rule[0]) == 1 ) {
* cur1 = trapz(0 , 0 , 20 , 30 , j); // Low*
* }
else if ((rule[0]) == 2) {
cur1 = trapz(20 , 30 , 50 , 60 , j); // Med*

* }
else if ((rule[0]) == 3 ) {
cur1 = trapz(50 , 60 , 70 , 80 , j); // High*

* }
else {
cur1 = trapz(80 , 90 , 100 , 100 , j); // Vhigh*

* }
if ((rule[1]) == 1 ) {
cur2 = trapz(0 , 0 , 20 , 30 , j); // Low*

* }
else if ((rule[1]) == 2) {
cur2 = trapz(20 , 30 , 50 , 60 , j); // Med*

* }
else if ((rule[1]) == 3 ) {
cur2 = trapz(50 , 60 , 70 , 80 , j); // High*

* }
else {
cur2 = trapz(80 , 90 , 100 , 100 , j); // Vhigh*

* }
lftm[j] = min (minmu , cur1) ;
rhtm[j] = min (minmu , cur2) ;
// Serial.println(rhtm[j]) ; // It’s working well here !
}
}
Serial.println(lftm[0][4]) ; // I can’t read data from this array*

}
float trapz(float a , float b , float c , float d, int x) {
float result;
if ((x >= b ) && ( x < c )) {

* result = 1000 ;
}
else if ((x >= a) && (x < b)) {
result = ((x - a) / (b - a)) * 1000 ;
}
else if ((x >= c) && (x < d)) {
result = ((d - x) / (d - c)) * 1000 ;
}
else {
result = 0 ;
}
return result ;
}
float func3min (float a , float b , float c) {
float result ;
if ((a < b) && (a < c)) {
result = a ;
}
else if ((b < a) && (b < c)) {
result = b ;
}
else {
result = c ;
}
return result ;
}
Test1.ino (4.56 KB)*_

If you’re in a hurry, why not show your code to as many people as possible?

Edit: …in code tags.

 int lftm [28][101];
  int rhtm [28][101];
  float arrfin [101] ;

A uno, you say? Hmmm.

I attached the code to the post.

arash94:
I attached the code to the post.

Not everyone can open attachments.
Like people on mobile devices.

See reply #1.

How much RAM does a Uno have?

32 KB I think so ,
it's not about memory storage.

the arrays are defined as local arrays.

there are enough memory storage , this code takes about %20 of memory space .

arash94:
32 KB I think so ,

Have another guess.
Or look at the product spec.

it’s not about memory storage

I disagree.
So does your Uno.

arash94:
this code takes about %20 of memory space .

That does not include local variables.

int left_us = 37.5 ;

Do you understand what “int” is a contraction of?