I need to process commands that look like this is serial:
P001 (001-199)
and
V31 (0-31)
V01
The first number is a command, and the second part are values. I need to convert those values to a single byte.
Bonus if I can also recognize P1, P01 as the same thing as P001.
What is the cleanest way to do this? I am messing around with a Union data type, but all I get is 0.
#include <serMP3.h>
union serial_data {
unsigned long number;
byte read_byte[3];
} data;
serMP3 MP3(11,10);
void setup(){
MP3.begin(31);
Serial.begin(9600);
}
void loop(){
byte sc = 0;
byte n =0;
if(Serial.available()){
sc = Serial.read();
}
if(sc == 'p' || sc == 'P'){
while(Serial.available() <=0); // wait for incoming serial data
if (Serial.available() >= 3) // wait for four bytes
{
for(int i=0;i <=3; i++) data.read_byte[i]=Serial.read();
}
n=byte(data.number);
// Serial.print(Serial.available());
Serial.print(n, DEC);
MP3.play(n);
}
if(sc == 's' || sc == 'S'){
MP3.stop();
}
}