Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 82 lines (66 sloc) 1.597 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/* ColourTester

Write arbitrary colour values to an RGB LCD.

To use, upload sketch, open serial monitor, and
set line endings to "Newline".
Enter three numbers between 0-255, separated by spaces.
Hit Send. The arduino will respond with the values, and
light up the LED.
*/

/* DIODER colours:
Pin 9: Green
Pin 10: Blue
Pin 11: Red
*/

int rLedPin=11;
int gLedPin=9;
int bLedPin=10;

char serialByte;
char serialBuffer[32];
int serialIndex = 0;

int rValue=0;
int gValue=0;
int bValue=0;

char *redstr;
char *greenstr;
char *bluestr;

void setup() {
  pinMode(rLedPin, OUTPUT);
  pinMode(gLedPin, OUTPUT);
  pinMode(bLedPin, OUTPUT);

  analogWrite(rLedPin, rValue);
  analogWrite(gLedPin, gValue);
  analogWrite(bLedPin, bValue);

  Serial.begin(9600);
}

void loop() {
  while (Serial.available()) {
    serialByte = Serial.read();
    if (serialByte != '\n') {
      serialBuffer[serialIndex] = serialByte;
      serialIndex++;
    }
    if (serialByte == '\n' or serialIndex == 31) {
      parseSerial();
      serialIndex = 0;
      memset(&serialBuffer, 0, 32);
    }
  }
}

void parseSerial() {
  char *s = serialBuffer;
  redstr = strtok_r(s, " ", &s);
  greenstr = strtok_r(NULL, " ", &s);
  bluestr = strtok_r(NULL, " ", &s);

  rValue = atoi(redstr);
  gValue = atoi(greenstr);
  bValue = atoi(bluestr);

  Serial.print("Red: ");
  Serial.print(rValue);
  Serial.print(" Green: ");
  Serial.print(gValue);
  Serial.print(" Blue: ");
  Serial.println(bValue);

  analogWrite(rLedPin, rValue);
  analogWrite(gLedPin, gValue);
  analogWrite(bLedPin, bValue);
}
Something went wrong with that request. Please try again.