Golang and Arduino

Small arduino and golang programs

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); } }
June 26, 2020