PARSING HEXA TO DECIMAL
Pada postingan kali ini kami akan membahas bagaimana cara pemecahan data serial pada arduino untuk memecah data hexa ke data desimal.
Berikut contoh programnya :
##===================================================================##
const int ledPin = 13;int bataskonter = 0;
int batasdelay = 0;
String datainput;
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
Serial.print("masukkan nilai konter dan delay : ");
Serial.println("");
Serial.print("");
Serial.println("");
}
void loop()
{
if (Serial.available() > 0)
{
datainput = Serial.readStringUntil('\n'); //serial dibaca hingga ditekan "enter"
bataskonter = hexToDec(datainput.substring (0,2));
batasdelay = hexToDec(datainput.substring (2,6));
//bataskonter = datainput.toInt();
//batasdelay =
Serial.print("Led Blinking: ");
Serial.print(bataskonter);
Serial.println(" times ");
Serial.print("leddelay: ");
Serial.print(batasdelay);
Serial.println(" milisecond ");
Serial.print("proses: ");
for(int konter = 1; konter <= bataskonter; konter++)
{
digitalWrite(ledPin,HIGH);
Serial.print(konter);
delay(400);
digitalWrite(ledPin,LOW);
delay(batasdelay);
}
Serial.println(" selesai!!! ");
Serial.print("");
Serial.println("");
}
}
unsigned int hexToDec(String hexString)
{
unsigned int decValue = 0;
int nextInt;
for(int i=0;i < hexString.length();i++)
{
nextInt = int (hexString.charAt (i));
if(nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if(nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if(nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain (nextInt, 0, 15);
decValue = (decValue * 16) + nextInt;
}
return decValue;
}
##===================================================================##
Pada saat program di atas kami compile :
gb1. Program Berhasil di Compile
##===================================================================##
gb2. Saat di inputkan 0A00C8
Tidak ada komentar:
Posting Komentar