Golang and Arduino

Small arduino and golang programs

arduino parse hex colors to numbers


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

    // get data from Serial.readStringUntil('\n'); to memory
    String data="FFFFFFFF 00FFFFFF 0000FFFF 000000FF";
    // store length of data
    int iend = data.length() - 1; 
    // current begin index
    int index=0;
    // next index
    int next_index;
    do{
        next_index=data.indexOf(' ',index);

        String hexcode=data.substring(index, next_index);

        index=next_index+1;

        unsigned long bigNumber = (unsigned long)strtoul(hexcode.c_str(), 0, 16);

        // add number to array

        Serial.println(bigNumber);

     }while((next_index!=-1)&&(next_index<iend));
}

void loop() {

}

arduino read serial command and parse to variables

You can send command without any libraries to arduino. in this example "message" ( protocol) is "2 chars + number + number" -> "GO 123 -512" 


void setup() { Serial.begin(9600); } void loop() {
// check if something on Serial if(Serial.available()) {
// define variables long number1, number2; char cmd[2]; memset(cmd, 0, 2);
// read data from Serial String s = Serial.readStringUntil('\n'); char buff[64]; memset(buff, 0, 64);
// convert string to char array s.toCharArray(buff, 64);
// parse cmd to variables sscanf(buff, "%2c %ld %ld",cmd, &number1, &number2); char pr[128]; memset(pr, 0, 128);
// print template to buffer sprintf(pr, "got 2 numbers from serial: %ld and %ld and command %s", number1, number2, cmd);
// print buffer back to Serial Serial.println(pr); } }