Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int r,g,b;
- char colors[3];
- String str;
- boolean can_change = true;
- int redPin = 9;
- int greenPin = 10;
- int bluePin = 11;
- void setup() {
- Serial.begin(115200);
- }
- void loop() {
- str = Serial.readString();
- r = getValue(str, ':', (int)0).toInt();
- g = getValue(str, ':', (int)1).toInt();
- b = getValue(str, ':', (int)2).toInt();
- setColor(r,g,b);
- Serial.println("can_send");
- }
- void setColor(int red, int green, int blue)
- {
- #ifdef COMMON_ANODE
- red = 255 - red;
- green = 255 - green;
- blue = 255 - blue;
- #endif
- analogWrite(redPin, red);
- analogWrite(greenPin, green);
- analogWrite(bluePin, blue);
- }
- String getValue(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = {0, -1};
- int maxIndex = data.length()-1;
- for(int i=0; i<=maxIndex && found<=index; i++){
- if(data.charAt(i)==separator || i==maxIndex){
- found++;
- strIndex[0] = strIndex[1]+1;
- strIndex[1] = (i == maxIndex) ? i+1 : i;
- }
- }
- return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
Advertisement
Add Comment
Please, Sign In to add comment