Pages: [1]   Go Down
Author Topic: What am I doing wrong?  (Read 319 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
//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 - downloaded 3 times.)
« Last Edit: December 29, 2012, 11:45:06 pm by Nick Gammon » Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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.
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It actually says:

Code:
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:

Code:
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:

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

to:

Code:
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:


Code:
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 <return> key. Try to make the code nice and readable.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, great. A lot fewer errors now, but I seem to still be within the proverbial woods.
Code:
//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:
Code:
tricolor_roll_1.cpp: In function ‘void setup()’:
tricolor_roll_1.cpp:20:16: error: a function-definition is not allowed here before ‘{’ token
Logged

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-cool

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.
Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

There are 10 types of people in the world, those that understand binary, and those that don't.

Johannesburg UTC+2
Offline Offline
Faraday Member
**
Karma: 60
Posts: 3242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You haven't closed setup() with a }

Code:
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 () {

Logged

Have you looked for answers in the Learning, Reference, Products and Playground tabs on http://arduino.cc/en/ ?
Look here http://electronicsclub.info/ for basic electronics

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: