Pages: [1]   Go Down
Author Topic: plz help. rbg led prob  (Read 3265 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have been working on trying to make a rgb led change colors when i press a pushbutton but teh code has some errors
here code
Quote
int val = 0;
int cnt = 0;
int inPin = 0;
int ledDigitalOne[] = {9, 10, 11};
const boolean ON = LOW;    
const boolean OFF = HIGH;
const boolean RED[] = {ON, OFF, OFF};    
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};
void setup(){
  for(int i = 0; i < 3; i++){
   pinMode(ledDigitalOne, OUTPUT);
   pinMode(inPin, INPUT);  
 }
}
void loop(){
  val = digitalRead(inPin);  
  if (val == HIGH) {        
  digitalWrite(ledDigitalOne, cnt);
  } else {
  for(int cnt = 0 ; cnt <= 8; cnt+=1)
  delay(1000)
  }
  if (cnt = 0) {
   setColor(ledDigitalOne, BLACK);
  }
  if cnt = 1) {
     setColor(ledDigitalOne, RED);
  }
  if cnt = 2) {
     setColor(ledDigitalOne, GREEN);
  }
  if cnt = 3) {
     setColor(ledDigitalOne, BLUE);
  }
  if cnt = 4) {
     setColor(ledDigitalOne, YELLOW);
  }
  if cnt = 5) {
     setColor(ledDigitalOne, CYAN);
  }
  if cnt = 6) {
     setColor(ledDigitalOne, MAGENTA);
  }
  if cnt = 7) {
     setColor(ledDigitalOne, WHITE);
  }
void setColor(int* led, boolean* color){
 for(int i = 0; i < 3; i++){
   digitalWrite(led, color);
 }
}
void setColor(int* led, const boolean* color){
  boolean tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);
}

here ARE the code errors:
sketch_jan15a.cpp: In function 'void loop()':
sketch_jan15a:24: error: invalid conversion from 'int*' to 'uint8_t'
sketch_jan15a:24: error: initializing argument 1 of 'void digitalWrite(uint8_t, uint8_t)'
sketch_jan15a:28: error: expected `;' before '}' token
sketch_jan15a:32: error: expected `(' before 'cnt'
sketch_jan15a:35: error: expected `(' before 'cnt'
sketch_jan15a:38: error: expected `(' before 'cnt'
sketch_jan15a:41: error: expected `(' before 'cnt'
sketch_jan15a:44: error: expected `(' before 'cnt'
sketch_jan15a:47: error: expected `(' before 'cnt'
sketch_jan15a:50: error: expected `(' before 'cnt'
sketch_jan15a:53: error: a function-definition is not allowed here before '{' token
sketch_jan15a:61: error: expected `}' at end of input
« Last Edit: January 15, 2011, 03:23:06 pm by develo » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but teh code has some errors
Why don't you fix them?

The Arduino psychic network is closed for the weekend. Try again on Monday, or supply some details so the non-psychic among us can help you.

Are you talking about syntax errors or logic errors or hardware errors?

Code:
 if (val [glow]==[/glow] HIGH) {        
  digitalWrite(ledDigitalOne, cnt);
  } else {
  for(int cnt = 0 ; cnt <= 8; cnt+=1)
  delay(1000)
  }
  if (cnt [glow]=[/glow] 0) {
Can you spot the difference between the two if tests?

Most C statements need a semicolon on the end.

The body of the for loop consists of the single delay statement. Why you need to do that in a loop escapes me.

You have a global variable, cnt, and local variable, cnt. The local variable's scope is limited to the for loop. The global cnt is never modified.
« Last Edit: January 15, 2011, 02:27:48 pm by PaulS » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok i posted the errors now cAN someone help
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I already pointed out some things that were wrong. The first message refers to this statement:
Code:
 digitalWrite(ledDigitalOne, cnt);
ledDigitalOne is an array. You can't turn all the pins in an array on this way. If that is what you are trying to do, you need to use a for loop to turn the LEDs on one at a time.

The digitalWrite function expects the second argument to be either HIGH or LOW. The cnt variable has a value of 0, so the pins would be turned off. Is that what you want?

The rest of the errors can't get past the fact that the delay statement is missing a semicolon.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i went messin with code now i have 2 probs left:
sketch_jan15a:53: error: a function-definition is not allowed here before '{' token
sketch_jan15a:61: error: expected `}' at end of input

here teh code now
Code:
[color=#7E7E7E]/*     ---------------------------------------------------------[/color]
[color=#7E7E7E] *     |  Experimentation Kit for Arduino Example Code         |[/color]
[color=#7E7E7E] *     |  CIRC-RGB .: Colourful Light :. (RGB LED)             |[/color]
[color=#7E7E7E] *     ---------------------------------------------------------[/color]
[color=#7E7E7E] * [/color]
[color=#7E7E7E] * We've blinked an LED and controlled eight in sequence now it's time to [/color]
[color=#7E7E7E] * control colour. Using an RGB LED (actual 3 LEDs in a single housing)  [/color]
[color=#7E7E7E] * we can generate any colour our heart desires.[/color]
[color=#7E7E7E] *[/color]
[color=#7E7E7E] * (we'll also use a few programming shortcuts to make the code [/color]
[color=#7E7E7E] * more portable/readable)[/color]
[color=#7E7E7E] */[/color]


[color=#7E7E7E]//RGB LED pins[/color]
[color=#CC6600]int[/color] val = 0;
[color=#CC6600]int[/color] cnt = 0;
[color=#CC6600]int[/color] inPin = 2;
[color=#CC6600]int[/color] redPin = 9;
[color=#CC6600]int[/color] greenPin = 10;
[color=#CC6600]int[/color] bluePin = 11;
[color=#CC6600]int[/color] ledDigitalOne[] = {9, 10, 11};
                                   [color=#7E7E7E]//9 = redPin, 10 = greenPin, 11 = bluePin[/color]

const [color=#CC6600]boolean[/color] ON = [color=#006699]LOW[/color];     [color=#7E7E7E]//Define on as LOW (this is because we use a common [/color]
                            [color=#7E7E7E]//Anode RGB LED (common pin is connected to +5 volts)[/color]
const [color=#CC6600]boolean[/color] OFF = [color=#006699]HIGH[/color];   [color=#7E7E7E]//Define off as HIGH[/color]

[color=#7E7E7E]//Predefined Colors[/color]
const [color=#CC6600]boolean[/color] RED[] = {ON, OFF, OFF};    
const [color=#CC6600]boolean[/color] GREEN[] = {OFF, ON, OFF};
const [color=#CC6600]boolean[/color] BLUE[] = {OFF, OFF, ON};
const [color=#CC6600]boolean[/color] YELLOW[] = {ON, ON, OFF};
const [color=#CC6600]boolean[/color] CYAN[] = {OFF, ON, ON};
const [color=#CC6600]boolean[/color] MAGENTA[] = {ON, OFF, ON};
const [color=#CC6600]boolean[/color] WHITE[] = {ON, ON, ON};
const [color=#CC6600]boolean[/color] BLACK[] = {OFF, OFF, OFF};

[color=#7E7E7E]//An Array that stores the predefined colors (allows us to later randomly display a color)[/color]
const [color=#CC6600]boolean[/color]* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color](){
  [color=#CC6600]for[/color]([color=#CC6600]int[/color] i = 0; i < 3; i++){
   [color=#CC6600]pinMode[/color](redPin, [color=#006699]OUTPUT[/color]);   [color=#7E7E7E]//Set the three LED pins as outputs[/color]
   [color=#CC6600]pinMode[/color](greenPin, [color=#006699]OUTPUT[/color]);   [color=#7E7E7E]//Set the three LED pins as outputs[/color]
   [color=#CC6600]pinMode[/color](bluePin, [color=#006699]OUTPUT[/color]);   [color=#7E7E7E]//Set the three LED pins as outputs[/color]
   [color=#CC6600]pinMode[/color](inPin, [color=#006699]INPUT[/color]);  
   [color=#CC6600]pinMode[/color](ledDigitalOne[i], [color=#006699]OUTPUT[/color]);
 }
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color](){
  val = [color=#CC6600]digitalRead[/color](inPin);  [color=#7E7E7E]// read input value[/color]
  [color=#CC6600]if[/color] (val == [color=#006699]HIGH[/color]) {         [color=#7E7E7E]// check if the input is HIGH (button released)[/color]
  [color=#CC6600]digitalWrite[/color](redPin, [color=#006699]HIGH[/color]);
  [color=#CC6600]digitalWrite[/color](greenPin, [color=#006699]HIGH[/color]);
  [color=#CC6600]digitalWrite[/color](bluePin, [color=#006699]HIGH[/color]);
  } [color=#CC6600]else[/color] {
  [color=#CC6600]for[/color]([color=#CC6600]int[/color] cnt = 0 ; cnt <= 8; cnt+=1)
  [color=#CC6600]delay[/color](1000);
  }
  [color=#CC6600]if[/color] (cnt = 0) {
   setColor(ledDigitalOne, BLACK);
  }
  [color=#CC6600]if[/color] (cnt = 1) {
     setColor(ledDigitalOne, RED);
  }
  [color=#CC6600]if[/color] (cnt = 2) {
     setColor(ledDigitalOne, GREEN);
  }
  [color=#CC6600]if[/color] (cnt = 3) {
     setColor(ledDigitalOne, BLUE);
  }
  [color=#CC6600]if[/color] (cnt = 4) {
     setColor(ledDigitalOne, YELLOW);
  }
  [color=#CC6600]if[/color] (cnt = 5) {
     setColor(ledDigitalOne, CYAN);
  }
  [color=#CC6600]if[/color] (cnt = 6) {
     setColor(ledDigitalOne, MAGENTA);
  }
  [color=#CC6600]if[/color] (cnt = 7) {
     setColor(ledDigitalOne, WHITE);
  }

[color=#CC6600]void[/color] setColor([color=#CC6600]int[/color]* led, [color=#CC6600]boolean[/color]* color){
 [color=#CC6600]for[/color]([color=#CC6600]int[/color] i = 0; i < 3; i++){
   [color=#CC6600]digitalWrite[/color](led[i], color[i]);
 }
}

[color=#7E7E7E]/* A version of setColor that allows for using const boolean colors[/color]
[color=#7E7E7E]*/[/color]
[color=#CC6600]void[/color] setColor([color=#CC6600]int[/color]* led, const [color=#CC6600]boolean[/color]* color){
  [color=#CC6600]boolean[/color] tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);
}

« Last Edit: January 15, 2011, 09:47:00 pm by develo » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

plz someone
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ughhhhhh
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some of us like to sleep once in a while. That last code you posted is nearly impossible to read. In the IDE, just use ctrl-a (select all) and ctrl-c (copy selected) to copy the code. In the message box, press the # button, than ctrl-v (paste) to add your code.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here more easy code
Code:
/*     ---------------------------------------------------------
 *     |  Experimentation Kit for Arduino Example Code         |
 *     |  CIRC-RGB .: Colourful Light :. (RGB LED)             |
 *     ---------------------------------------------------------
 *
 * We've blinked an LED and controlled eight in sequence now it's time to
 * control colour. Using an RGB LED (actual 3 LEDs in a single housing)  
 * we can generate any colour our heart desires.
 *
 * (we'll also use a few programming shortcuts to make the code
 * more portable/readable)
 */


//RGB LED pins
int val = 0;
int cnt = 0;
int inPin = 2;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int ledDigitalOne[] = {9, 10, 11};
                                   //9 = redPin, 10 = greenPin, 11 = bluePin

const boolean ON = LOW;     //Define on as LOW (this is because we use a common
                            //Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;   //Define off as HIGH

//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};    
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, BLACK};

void setup(){
  for(int i = 0; i < 3; i++){
   pinMode(redPin, OUTPUT);   //Set the three LED pins as outputs
   pinMode(greenPin, OUTPUT);   //Set the three LED pins as outputs
   pinMode(bluePin, OUTPUT);   //Set the three LED pins as outputs
   pinMode(inPin, INPUT);  
   pinMode(ledDigitalOne[i], OUTPUT);
     setColor(ledDigitalOne, WHITE);
 }
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, HIGH);
  digitalWrite(bluePin, HIGH);
  } else {
  for(int cnt = 0 ; cnt <= 8; cnt+=1)
  delay(1000);
  }
  if (cnt = 0) {
   setColor(ledDigitalOne, BLACK);
  }
  if (cnt = 1) {
     setColor(ledDigitalOne, RED);
  }
  if (cnt = 2) {
     setColor(ledDigitalOne, GREEN);
  }
  if (cnt = 3) {
     setColor(ledDigitalOne, BLUE);
  }
  if (cnt = 4) {
     setColor(ledDigitalOne, YELLOW);
  }
  if (cnt = 5) {
     setColor(ledDigitalOne, CYAN);
  }
  if (cnt = 6) {
     setColor(ledDigitalOne, MAGENTA);
  }
  if (cnt = 7) {
     setColor(ledDigitalOne, WHITE);
  }

void setColor(int* led, boolean* color){
 for(int i = 0; i < 3; i++){
   digitalWrite(led[i], color[i]);
 }
}

/* A version of setColor that allows for using const boolean colors
*/
void setColor(int* led, const boolean* color){
  boolean tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 for(int i = 0; i < 3; i++){
   pinMode(redPin, OUTPUT);   //Set the three LED pins as outputs
   pinMode(greenPin, OUTPUT);   //Set the three LED pins as outputs
   pinMode(bluePin, OUTPUT);   //Set the three LED pins as outputs
   pinMode(inPin, INPUT);  
   pinMode(ledDigitalOne[i], OUTPUT);
     setColor(ledDigitalOne, WHITE);
 }
Why is it necessary to set the pinMode for redPin, greenPin, bluePin, and inPin in the loop?

The setColor call is still passing the whole array, not an individual pin.

Code:
else {
  for(int cnt = 0 ; cnt <= 8; cnt+=1)
  delay(1000);
  }
You are still calling nothing but delay() in this loop.

Code:
 if (cnt = 0) {
The = operator is an assignment operator. The == operator is an equaltiy operator. The are NOT interchangeable.

The cnt variable is still never assigned a new value, so only the first test will ever evaluate to true.

Code:
 if (cnt = 7) {
     setColor(ledDigitalOne, WHITE);
  }

void setColor(int* led, boolean* color){
 for(int i = 0; i < 3; i++){
   digitalWrite(led[i], color[i]);
 }
}
The closing } for the loop function is missing. You can not define a function (setColor()) inside of a function (loop()).

Do yourself a big favor. Learn to put the { on a separate line, and indent everything consistently. The auto-format feature of the IDE is terrible.
Code:
void loop()
{
   // Code goes here
}
It's much easier to see the range of a function or statement block when the { and } line up.

Also, learn to use { and } to include all if blocks and for loops, even when the body of the block contains a single statement. It's much easier to add a line inside of a block than to remember to add the block delimiters AND the new statement.

If you go back and read my previous posts, you'll see that I pointed out most of the errors in previous posts. It is up to you to pay attention to the replies you receive, and, if you don't understand what you read, or don't agree with it, to question or argue. Ignoring advice and complaining "it still doesn't work" is a good way to get ignored.
Logged

Pages: [1]   Go Up
Jump to: