You are on page 1of 21

Data Type Optimization,

Variable Modifier & Interupt

Sistem Tertanam
Program Studi Teknik Informatika
Institut Teknologi Sumatera
Apa kesalahan pada code ini ?
Static int LED = 10;

void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);

}
Bagaimana jika diubah menjadi
seperti ini ?
Const int LED = 10;

void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
digitalWrite(LED, HIGH);
delay(1000);
delay(1000);
}
Bagaimana jika diubah menjadi
seperti ini ?
Const byte LED = 10;

void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
digitalWrite(LED, HIGH);
delay(1000);
delay(1000);
}
Byte vs Integer
Byte Integer
1 Byte (8 bit) 2 Byte (16 Bit)
0 to 255 -32,768 to 32,767
Data Type
1. boolean (8 bit) - simple logical true/false
2. byte (8 bit) - unsigned number from 0-255
3. char (8 bit) - signed number from -128 to 127. The
compiler will attempt to interpret this data type as a
character in some circumstances, which may yield
unexpected results
4. unsigned char (8 bit) - same as 'byte'; if this is what
you're after, you should use 'byte' instead, for
reasons of clarity
5. word (16 bit) - unsigned number from 0-65535
6. unsigned int (16 bit)- the same as 'word'. Use 'word'
instead for clarity and brevity
Data Type
7. int (16 bit) - signed number from -32768 to 32767. This is
most commonly what you see used for general purpose
variables in Arduino example code provided with the IDE
8. unsigned long (32 bit) - unsigned number from 0-
4,294,967,295. The most common usage of this is to store
the result of the millis() function, which returns the number
of milliseconds the current code has been running
9. long (32 bit) - signed number from -2,147,483,648 to
2,147,483,647
10. float (32 bit) - signed number from -3.4028235E38 to
3.4028235E38. Floating point on the Arduino is not native;
the compiler has to jump through hoops to make it work. If
you can avoid it, you should. We'll touch on this later.
Fixed Width Integer
Memory
• Flash memory (program space), is where the Arduino
sketch is stored.
• SRAM (static random access memory) is where the
sketch creates and manipulates variables when it
runs.
• EEPROM is memory space that programmers can use
to store long-term information.

Flash 32k bytes (of which .5k is used for the


bootloader)
SRAM 2k bytes
EEPROM 1k byte
Variable Modifier : Volatile

Terdapat jenis tipe data volatile yang berfungsi


untuk menyimpan nilai variabel pada RAM bukan
register
volatile double suhu = 0;

Variabel suhu bertipe double dengan keyword


volatile menyebabkan 4 byte data untuk suhu
disimpan di RAM, bukan memori register.
Variable Modifier : Progmem

Dengan perintah PROGMEM, maka data akan


disimpan di memori flash.
Berguna ketika menggunakan data dummy
sebagain input
const dataType variableName[] PROGMEM = {data0, data1, data3…};
const unsigned int suhu[10] PROGMEM =
{40,50,60,55,45,53,34,44,25,46};
F() Macro
When an instruction like :
Serial.print("Write something on the Serial Monitor");
Serial.print(F("Write something on the Serial
Monitor that is stored in FLASH"));
is used, the string to be printed is normally saved in
RAM. If your sketch prints a lot of stuff on the Serial
Monitor, you can easily fill the RAM. If you have free
FLASH memory space, you can easily indicate that
the string must be saved in FLASH using the
syntax:
Timer Interupt
• Timer interrupt adalah salah satu dari ISR (Interrupt
Service Routine) yang merupakan interupsi yang
dilakukan berdasarkan pewaktu atau timer
• Bagian program yang dieksekusi sebagai interupsi
akan dianggap lebih penting daripada program yang
sedang berjalan.
• Oleh sebab itu, bagian program yang sedang berjalan
akan dijeda sementara sehingga alur program
dialihkan ke bagian program yang diinterupsikan.
Setelah program interupsi selesai, maka bagian
program yang tadinya dijeda, akan dilanjutkan lagi.
Timer Interupt
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

• Arduino menggunakan dasar pemrograman c


secara sekuensial
• Interupt memungkinkan adanya perubahan
eksekusi sekuensial
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

• Arduino menggunakan dasar pemrograman c


secara sekuensial
• Interupt memungkinkan adanya perubahan
eksekusi sekuensial
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

• pin: the Arduino pin number.


• ISR: the ISR to call when the interrupt occurs; this
function must take no parameters and return nothing.
This function is sometimes referred to as an interrupt
service routine.
• mode: defines when the interrupt should be triggered.
Four constants are predefined as valid values:
• LOW to trigger the interrupt whenever the pin is low,
• CHANGE to trigger the interrupt whenever the pin changes value
• RISING to trigger when the pin goes from low to high,
• FALLING for when the pin goes from high to low.
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

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

void loop() {
Serial.println("kode 1");
delay(1000);
Serial.println("kode 2");
delay(1000);
Serial.println("kode 3");
delay(1000);
Serial.println("kode 4");
delay(1000);
Serial.println("kode 5");
delay(1000);
Serial.println("kode 6");
delay(1000);
}
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

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

void loop() {
Serial.println("kode 1");
delay(1000);
Serial.println("kode 2");
delay(1000);
Serial.println("kode 3");
delay(1000);
Serial.println("kode 4");
delay(1000);
Serial.println("kode 5");
delay(1000);
Serial.println("kode 6");
delay(1000);
}
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

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

void loop() { if (digitalRead(2) == LOW)


Serial.println("kode 1"); { Serial.println("sisipkan");
delay(1000); delay(1000); }
Serial.println("kode 2"); }
delay(1000);
Serial.println("kode 3");
delay(1000);
Serial.println("kode 4");
delay(1000);
Serial.println("kode 5");
delay(1000);
Serial.println("kode 6");
delay(1000);
attachInterupt()
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/

void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2),fungsiSisip,FALLING);

}
void loop() {
Serial.println("kode 1");
void fungsiSisip()
delay(1000);
{
Serial.println("kode 2");
Serial.println("sisipan");
delay(1000);
delay(1000);
Serial.println("kode 3");
}
delay(1000);
Serial.println("kode 4");
delay(1000);
Serial.println("kode 5");
delay(1000);
Serial.println("kode 6");
delay(1000);
}

You might also like