Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 82 lines (66 sloc) 1.597 kB
6f6c8b3 @phardy Initial commit
authored
1 /* ColourTester
2
3 Write arbitrary colour values to an RGB LCD.
4
5 To use, upload sketch, open serial monitor, and
6 set line endings to "Newline".
7 Enter three numbers between 0-255, separated by spaces.
8 Hit Send. The arduino will respond with the values, and
9 light up the LED.
10 */
11
12 /* DIODER colours:
13 Pin 9: Green
14 Pin 10: Blue
15 Pin 11: Red
16 */
17
18 int rLedPin=11;
19 int gLedPin=9;
20 int bLedPin=10;
21
22 char serialByte;
23 char serialBuffer[32];
24 int serialIndex = 0;
25
26 int rValue=0;
27 int gValue=0;
28 int bValue=0;
29
30 char *redstr;
31 char *greenstr;
32 char *bluestr;
33
34 void setup() {
35 pinMode(rLedPin, OUTPUT);
36 pinMode(gLedPin, OUTPUT);
37 pinMode(bLedPin, OUTPUT);
38
39 analogWrite(rLedPin, rValue);
40 analogWrite(gLedPin, gValue);
41 analogWrite(bLedPin, bValue);
42
43 Serial.begin(9600);
44 }
45
46 void loop() {
47 while (Serial.available()) {
48 serialByte = Serial.read();
49 if (serialByte != '\n') {
50 serialBuffer[serialIndex] = serialByte;
51 serialIndex++;
52 }
53 if (serialByte == '\n' or serialIndex == 31) {
54 parseSerial();
55 serialIndex = 0;
56 memset(&serialBuffer, 0, 32);
57 }
58 }
59 }
60
61 void parseSerial() {
62 char *s = serialBuffer;
63 redstr = strtok_r(s, " ", &s);
64 greenstr = strtok_r(NULL, " ", &s);
65 bluestr = strtok_r(NULL, " ", &s);
66
67 rValue = atoi(redstr);
68 gValue = atoi(greenstr);
69 bValue = atoi(bluestr);
70
71 Serial.print("Red: ");
72 Serial.print(rValue);
73 Serial.print(" Green: ");
74 Serial.print(gValue);
75 Serial.print(" Blue: ");
76 Serial.println(bValue);
77
78 analogWrite(rLedPin, rValue);
79 analogWrite(gLedPin, gValue);
80 analogWrite(bLedPin, bValue);
81 }
Something went wrong with that request. Please try again.