🧩 1. ความหมายของ Conversion

Conversion หรือ Type Casting
คือกระบวนการ “แปลงค่าจากชนิดข้อมูลหนึ่ง → ไปเป็นอีกชนิดหนึ่ง”
เพื่อให้สามารถประมวลผลหรือคำนวณร่วมกันได้อย่างถูกต้อง

เช่น

C++
int a = 10;
float b = 3;
float result = a / b;   // a (int) → แปลงเป็น float ก่อนหาร

⚙️ 2. ประเภทของ Conversion

ประเภทชื่อเรียกลักษณะตัวอย่าง
Implicit ConversionType Promotion / Automatic ConversionCompiler แปลงให้เองโดยอัตโนมัติint + float → float
Explicit ConversionType Castingผู้เขียนแปลงเองโดยเจตนา(float) a / b

🔹 3. Implicit Conversion (การแปลงอัตโนมัติ)

Compiler จะเลือกชนิดข้อมูลที่ “ใหญ่กว่า” เพื่อไม่ให้ข้อมูลสูญหาย

ตัวอย่างผลลัพธ์คำอธิบาย
int a=5; float b=2.0; float c=a/b;2.5a ถูกแปลงเป็น float อัตโนมัติ
float x=3.7; int y=x;3ทศนิยมถูกตัดทิ้ง
char c='A'; int n=c;65'A' ถูกแปลงเป็นรหัส ASCII

📘 ตัวอย่าง:

C++
void setup() {
  Serial.begin(115200);
  int a = 7;
  float b = 2;
  float result = a / b;   // a ถูกแปลงเป็น float
  Serial.println(result); // 3.50
}
void loop() {}

🔹 4. Explicit Conversion (Type Casting แบบกำหนดเอง)

เขียนบอกชัด ๆ ว่าจะให้แปลงเป็นชนิดใด
มี 2 รูปแบบที่นิยม:

C++
(float)a / b

หรือ

C++
float(a) / b

ทั้งคู่ทำงานเหมือนกัน

📘 ตัวอย่าง:

C++
int total = 7;
int count = 2;
float avg = (float) total / count;  // แปลง total เป็น float ก่อนหาร
Serial.println(avg);  // แสดง 3.50

ถ้าไม่แปลง → 7/2 จะได้ 3 เพราะเป็นการหารแบบจำนวนเต็ม


🔹 5. การแปลงชนิดใน Arithmetic Expression

ถ้าค่าร่วมกันจะถูกแปลงเป็น
int + floatfloat
float + doubledouble
byte + intint
char + intint
unsigned + signed→ signed ถูกแปลงเป็น unsigned

📘 ตัวอย่าง:

C++
byte a = 10;
int b = 300;
int sum = a + b;  // byte → int ก่อนคำนวณ

🔹 6. การแปลงชนิดของข้อความ (String Conversion)

ใน Arduino/ESP32 เรามักเจอข้อความจาก Serial, Wi-Fi, JSON ฯลฯ
ต้องแปลงจาก String → ตัวเลข หรือ ตัวเลข → String


🔸 6.1 String → Number

ฟังก์ชันการใช้งานตัวอย่างผลลัพธ์
.toInt()แปลง String เป็น intString s="123"; s.toInt();123
.toFloat()แปลง String เป็น floatString s="3.14"; s.toFloat();3.14
atoi()C-style: char[] → intatoi("456")456
atof()C-style: char[] → floatatof("2.718")2.718

📘 ตัวอย่าง:

C++
String data = "123";
int value = data.toInt();
Serial.println(value * 2); // 246

🔸 6.2 Number → String

ฟังก์ชันการใช้งานตัวอย่างผลลัพธ์
String(x)แปลงเป็น StringString(123)"123"
String(x, base)แปลงเป็น String ตามฐานString(255, HEX)"FF"
itoa()int → char[]itoa(255, buf, 10)"255"
dtostrf()float → char[] (C-style)dtostrf(3.14, 5, 2, buf)" 3.14"

📘 ตัวอย่าง:

C++
int val = 123;
String msg = "Value = " + String(val);
Serial.println(msg);  // Value = 123

🔹 7. การแปลงแบบฐาน (Base Conversion)

Arduino รองรับเลขหลายฐาน:

  • ฐาน 10 (decimal) → ปกติ
  • ฐาน 16 (hex)0xFF
  • ฐาน 2 (binary)0b1010

📘 ตัวอย่าง:

C++
int num = 255;
Serial.println(num, BIN);  // 11111111
Serial.println(num, HEX);  // FF
Serial.println(num, DEC);  // 255

🔹 8. การแปลงแบบ Bitwise (ระดับบิต)

ใช้กับ Digital I/O, Register, Sensor protocol

ตัวดำเนินการความหมายตัวอย่างผลลัพธ์
<<เลื่อนบิตซ้าย1 << 38
>>เลื่อนบิตขวา8 >> 22
&AND บิต0b1010 & 0b11000b1000
``OR บิต`0b1010
^XOR บิต0b1010 ^ 0b11000b0110
~NOT บิต~0b000111111110

📘 ตัวอย่าง:

C++
byte val = 0b1010;
val = val << 1;  // 0b10100

🔹 9. Conversion ใน Sensor และ ADC

ตัวอย่าง: แปลงค่า ADC (0–4095) → แรงดัน (0–3.3V)

C++
int adc = analogRead(34);
float voltage = (float)adc * 3.3 / 4095.0;
Serial.println(voltage);

ใช้การแปลง (float) เพื่อไม่ให้หารแบบจำนวนเต็ม


🔹 10. Conversion ระหว่างหน่วย (Unit Conversion)

ไมโครคอนโทรลเลอร์มักต้อง “แปลงหน่วย” เช่น:

  • ค่า ADC → Voltage
  • เซนเซอร์ความร้อน → °C
  • เวลา ms → s
  • องศา → เรเดียน

📘 ตัวอย่าง:

C++
int sensor = analogRead(34);
float voltage = sensor * 3.3 / 4095.0;
float temperature = voltage * 100.0; // LM35: 10 mV/°C
Serial.printf("Temp = %.2f °C\n", temperature);

🔹 11. Conversion แบบซับซ้อนใน ESP32 (เช่น String JSON)

เมื่อใช้ Wi-Fi หรือ MQTT เราอาจได้ข้อมูลเป็น String เช่น "23.5"
ต้องแปลงก่อนคำนวณ:

C++
String payload = "23.5";
float temp = payload.toFloat();
if (temp > 30.0) {
  Serial.println("Hot!");
}

📘 12. ตัวอย่างสรุป Conversion ทั่วไป

จาก → เป็นวิธีแปลงตัวอย่าง
int → float(float)x(float)a / b
float → int(int)x(int)3.7 → 3
char → intอัตโนมัติ'A' → 65
int → char(char)x(char)65 → 'A'
String → int.toInt()"123".toInt() → 123
String → float.toFloat()"3.14".toFloat() → 3.14
int → StringString(x)String(255, HEX)
char[] → intatoi()atoi("45") → 45
float → char[]dtostrf()dtostrf(3.14, 5, 2, buf)

🧠 13. เคล็ดลับ

  • ใช้ Serial Monitor ทดลองแปลงค่าทุกชนิดให้ดูผลจริง
  • อธิบายว่าทำไม 7/2 ได้ 3 แต่ (float)7/2 ได้ 3.5
  • ให้ฝึกแปลง “ข้อความจาก Serial” เป็นตัวเลขแล้วเอาไปเปิดไฟ/คำนวณ
  • ใน ESP32 ให้ลองอ่านค่า ADC แล้วแปลงเป็นแรงดัน → อุณหภูมิ → พิมพ์แสดงผล

🧾 14. สรุปแนวคิดสำคัญ

ประเด็นอธิบายสั้น
ImplicitCompiler แปลงอัตโนมัติ
Explicitผู้เขียนกำหนดเอง (float)x
Castingการบังคับเปลี่ยนชนิด
String Conversionแปลงข้อความ ↔ ตัวเลข
Base Conversionแปลงเลขฐาน (BIN, HEX, DEC)
Bitwise Conversionแปลงระดับบิต/ใช้ในฮาร์ดแวร์
Unit Conversionแปลงหน่วยจากเซนเซอร์