# What am I doing wrong?

Got my Arduino Uno yesterday after wanting one for months. I was not satisfied with making the onboard LED blink, so I started playing with a tri-color LED found in a starter kit. My goal was to make the LED transition through a portion of its spectrum, and I thought I’d come up with good code to do exactly that, but to no avail. The IDE says “you sick” (I am paraphrasing). Anyone care to tell a rookie where he got it wrong?

``````//this code is for driving a 3 color LED through a revolving pattern of colors

const int RED = 9;          //establish location of red pin
const int GREEN = 10;       //establish location of green pin
const int BLUE = 11;        //establish location of blue pin
int r = 255;                //set starting red value
int g = 85;                 //set starting green value
int b = 85;                 //set starting blue value
int rt = 0;                 //red directional trigger
int gt = 1;                 //green directional trigger
int bt = 0;                 //blue directional trigger

void setup () {
pinMode (RED, OUTPUT);    //set red pin for output
pinMode (GREEN, OUTPUT);  //set green pin for output
pinMode (BLUE, OUTPUT);   //set blue pin for output
}

void loop () {
analogWrite (RED, r);    //set red pin to brightness value
analogWrite (GREEN, g);  //set green pin to brightness value
analogWrite (BLUE, b);   //set blue pin to brightness value
}
if (rt == 0) {r=r-1}          //if trigger is set to dim, dim by 1
else {r=r+1}                 //if trigger is not set to dim, brighten by 1
if (gt == 0) {g=g-1}          //if trigger is set to dim, dim by 1
else {g=g+1}                 //if trigger is not set to dim, brighten by 1
if (bt == 0) {b=b-1}          //if trigger is set to dim, dim by 1
else {b=b+1}                 //if trigger is not set to dim, brighten by 1

if (r > 254) {rt=rt-1}        //increment red brightness value
if (g > 254) {gt=gt-1}        //increment green brightness value
if (b > 254) {bt=bt-1}        //increment blue brightness value
if (r < 1) {rt=rt+1}          //decrement red brightness value
if (g < 1) {gt=gt+1}          //decrement green brightness value
if (b < 1) {bt=bt+1}          //decrement blue brightness value
{
delay(10)                    //give time for observation
}
``````

Moderator edit: [code][/code] tags added. (Nick Gammon)

tricolor_roll_1.ino (1.81 KB)

Hi kenbuntu,

Welcome to the wonderful world of Arduino!

I assume that what is making the compiler sick is semi-colon deficiency. Add some and try it again.

``````if (rt == 0) {r=r-1;}          //if trigger is set to dim, dim by 1
else {r=r+1;}                 //if trigger is not set to dim, brighten by 1
``````

Pat.

It actually says:

``````sketch_dec30d:23: error: expected unqualified-id before 'if'
sketch_dec30d:24: error: expected unqualified-id before 'else'
sketch_dec30d:25: error: expected unqualified-id before 'if'
sketch_dec30d:26: error: expected unqualified-id before 'else'
sketch_dec30d:27: error: expected unqualified-id before 'if'
sketch_dec30d:28: error: expected unqualified-id before 'else'
sketch_dec30d:30: error: expected unqualified-id before 'if'
sketch_dec30d:31: error: expected unqualified-id before 'if'
sketch_dec30d:32: error: expected unqualified-id before 'if'
sketch_dec30d:33: error: expected unqualified-id before 'if'
sketch_dec30d:34: error: expected unqualified-id before 'if'
sketch_dec30d:35: error: expected unqualified-id before 'if'
sketch_dec30d:36: error: expected unqualified-id before '{' token
``````

First, move the squiggly bracket to the end:

``````void loop () {
analogWrite (RED, r);    //set red pin to brightness value
analogWrite (GREEN, g);  //set green pin to brightness value
analogWrite (BLUE, b);   //set blue pin to brightness value
}  // <------- this one, move to the end of the sketch
``````

Next, C++ statements end with a semicolon, so change:

``````if (rt == 0) {r=r-1}          //if trigger is set to dim, dim by 1
``````

to:

``````if (rt == 0) {r=r-1;}          //if trigger is set to dim, dim by 1
``````

Next, use the auto-format tool and clean up the layout, it is hard to read like that. It should look more like:

``````if (rt == 0)
{
r = r - 1;   //if trigger is set to dim, dim by 1
}
``````

There’s no reason to avoid hitting the space bar or key. Try to make the code nice and readable.

Okay, great. A lot fewer errors now, but I seem to still be within the proverbial woods.

``````//this code is for driving a 3 color LED through a revolving pattern of colors

const int RED = 9;          //establish location of red pin
const int GREEN = 10;       //establish location of green pin
const int BLUE = 11;        //establish location of blue pin
int r = 255;                //set starting red value
int g = 85;                 //set starting green value
int b = 85;                 //set starting blue value
int rt = 0;                 //red directional trigger
int gt = 1;                 //green directional trigger
int bt = 0;                 //blue directional trigger

void setup () {
pinMode (RED, OUTPUT);    //set red pin for output
pinMode (GREEN, OUTPUT);  //set green pin for output
pinMode (BLUE, OUTPUT);   //set blue pin for output

void loop () {
analogWrite (RED, r);    //set red pin to brightness value
analogWrite (GREEN, g);  //set green pin to brightness value
analogWrite (BLUE, b);   //set blue pin to brightness value
}
if (rt == 0) {
r=r-1;
}                          //if trigger is set to dim, dim by 1
else {
r=r+1;
}                          //if trigger is not set to dim, brighten by 1
if (gt == 0) {
g=g-1;
}                          //if trigger is set to dim, dim by 1
else {
g=g+1;
}                          //if trigger is not set to dim, brighten by 1
if (bt == 0) {
b=b-1;
}                          //if trigger is set to dim, dim by 1
else {
b=b+1;
}                          //if trigger is not set to dim, brighten by 1

if (r > 254) {
rt=rt-1;
}                          //increment red brightness value
if (g > 254) {
gt=gt-1;
}                          //increment green brightness value
if (b > 254) {
bt=bt-1;
}                          //increment blue brightness value
if (r < 1) {
rt=rt+1;
}                          //decrement red brightness value
if (g < 1) {
gt=gt+1;
}                          //decrement green brightness value
if (b < 1) {
bt=bt+1;
}                          //decrement blue brightness value
{delay(10);}               //give time for observation
}
``````

now giving me only 2 errors:

``````tricolor_roll_1.cpp: In function ‘void setup()’:
tricolor_roll_1.cpp:20:16: error: a function-definition is not allowed here before ‘{’ token
``````

The real moral of the story is to post the error messages verbatim, not paraphrased as in "you sick" (sic). EDIT... which I see you did while I was typing this post 8)

Here's a tip though.... if you click the cursor just behind any closing bracket like a ) or a }, a small box will appear round the corresponding opening one earlier in the code. This makes it easy to check that each pair of ( and ) or { and } actually enclose the right stuff.

You need a closing } for setup, and you have an extra one in loop. Move it up.

You haven’t closed setup() with a }

``````void setup () {     //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this is not balanced by a closing one
pinMode (RED, OUTPUT);    //set red pin for output
pinMode (GREEN, OUTPUT);  //set green pin for output
pinMode (BLUE, OUTPUT);   //set blue pin for output
}    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< need to add this one methinks

void loop () {
``````

Awesomesauce. Thanks gents. I can see by the way it's running that I still have some tweaking to do, but I know I'm on track now.