Pikir dan Rasa

cogito ergo sum

Bermain dengan array [tinkering Arduino]

leave a comment »

Sekedar tinkering dengan array pada Arduino

Code:


const word BAUDRATE = 9600;

char array01[] = {'1','2','3','4','5','6'};
char array02[7];
  

void setup(){
  Serial.begin(BAUDRATE);
  
  char array03[] = {'7','8','9','1','2','3'};
  
  Serial.println();
  Serial.println("Isi array02 >> ");
  
  for(char ko = 0; ko < 5; ko++){
    array02[ko] = ko+5 ;
    Serial.print((int)(array02[ko]));
  }
  Serial.println();
  
  char *p;
  p = array01;
  
  Serial.println();
  Serial.println("Tampilan >> ");
  Serial.println();
 
  
  Serial.println("array01");
  Serial.println(array01);
  Serial.println();

  Serial.println("array01 dengan pointer");
  Serial.println(p);
  Serial.println();
  
  Serial.println("array02 dengan bantuan \"for\" ");
  tulisArray02();
  Serial.println();
  
  Serial.println("array02");
  Serial.println(array02);
  Serial.println();
  
  Serial.println("array02 casting dengan int");
  Serial.println((int)array02);
  Serial.println();
  
  / 
  Serial.println("array03");
  Serial.println(array03);
  Serial.println();  
  /
  
  delay(1000);
  
  
  printtulis(array01);
  printtulis(array02);
  printtulisint(array02);
  //printtulis(array03);
  //printtulisint(array03);
}

void loop(){ 

    
}

void printtulis(char *data){ 

  Serial.println();
  Serial.println("Fungsi printtulis:");
  Serial.println(data);
  Serial.println();
}

void printtulisint(char *data2i){
  Serial.println();
  Serial.println("Fungsi printtulisint:");
  Serial.println((long)data2i);
  Serial.println();
}

void tulisArray02(){
  Serial.println("tulisArray02:");
  const int batas1 = ((sizeof(array02)/sizeof(char)));
  Serial.print("Nilai batas: ");
  Serial.println(batas1);
  for(int i=0; i < batas1;i++){
     Serial.print((int)array02[i]); 

  }
  Serial.println();
  //Serial.println();
}

 

 

Keterangan dari situs Arduino:

For this reason you should be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size – 1) is reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult bug to track down.

Unlike BASIC or JAVA, the C compiler does no checking to see if array access is within legal bounds of the array size that you have declared.

 

 

Referensi:

http://www.arduino.cc/en/Reference/Array


Written by sunupradana

January 1, 2012 at 8:17 pm

Posted in Electronics, Embedded Systems

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: