Skip to content

Instantly share code, notes, and snippets.

@raster
Created December 10, 2016 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raster/1e053ef5dc9825e05d7c2419c3449340 to your computer and use it in GitHub Desktop.
Save raster/1e053ef5dc9825e05d7c2419c3449340 to your computer and use it in GitHub Desktop.
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