Create a gist now

Instantly share code, notes, and snippets.

Get comma separated values returned from a function
// RandomColor.ino
void setup() {
Serial.begin(9600);
randomSeed(analogRead(A0));
}
void loop() {
char* rgb = returnColors();
int r, g, b;
if (sscanf(rgb, "%d,%d,%d", &r, &g, &b) == 3) {
Serial.print(r);
Serial.print(", ");
Serial.print(g);
Serial.print(", ");
Serial.println(b);
}
delay(500);
}
char* returnColors() {
char* myColors[6];
myColors[0] = "128,0,0";
myColors[1] = "0,128,0";
myColors[2] = "0,0,128";
myColors[3] = "255,0,0";
myColors[4] = "0,255,0";
myColors[5] = "0,0,255";
int colorIndex = random(0,6);
char* result = myColors[colorIndex];
return (result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment