Pages: [1]   Go Down
Author Topic: need a better code  (Read 637 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey there! i made this code for 4 rgb and 1 led on arduino uno and need code to turn the led/rgb led dimming! like it will trun on within a second but it will take time to off!  smiley

Code:

int led = 13;
int ledDigitalOne[] = {14, 15, 16};
 
int ledDigitalTwo[] = {9, 10, 11};
 
int ledDigitalThree[] = {3, 5, 6};

int ledDigitalFour[] = {0, 1, 2};
 
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(led, OUTPUT);
  
  pinMode(ledDigitalOne[i], OUTPUT);

  pinMode(ledDigitalTwo[i], OUTPUT);
 
  pinMode(ledDigitalThree[i], OUTPUT);

  pinMode(ledDigitalFour[i], OUTPUT);
}
}

void loop(){

digitalWrite(led, HIGH);
delay(150);
digitalWrite(led, LOW);
delay(580);
/* Example - 1 Set a color Set the three LEDs to any predefined color*/
setColor(ledDigitalOne, RED);
 
setColor(ledDigitalTwo, GREEN);
 
setColor(ledDigitalThree, BLUE);

setColor(ledDigitalFour, RED);

/* Exampe - 2 Go through Random Colors Set the LEDs to a random color*/
int rand = random(0, sizeof(COLORS) / 2);
 
setColor(ledDigitalOne, COLORS[rand]);
 
rand = random(0, sizeof(COLORS) / 2);
 
setColor(ledDigitalTwo, COLORS[rand]);
rand = random(0, sizeof(COLORS) / 2);
 
setColor(ledDigitalThree, COLORS[rand]);

setColor(ledDigitalFour, COLORS[rand]);
delay(1000);
}

/* Sets an led to any color led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin) color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)*/
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], color[3]};
setColor(led, tempColor);
}
HELP!   smiley-roll-sweat
THANK YOU smiley
« Last Edit: August 23, 2013, 12:20:23 pm by CrossRoads » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26242
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Again, with code tags and without italics, please.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4062
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Again, with code tags and without italics, please.

That'd be item 7 here: http://forum.arduino.cc/index.php/topic,148850.0.html
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26963
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

code tags added by moderator.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26242
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
code tags added by moderator.
I could've done that, but I don't like being SHOUTED at.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26963
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Shouted at??
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26242
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Shouted at??
Quote
HELP!
Shouted at.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26963
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Idaho, US
Offline Offline
God Member
*****
Karma: 19
Posts: 868
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey there! i made this code for 4 rgb and 1 led on arduino uno and need code to turn the led/rgb led dimming! like it will trun on within a second but it will take time to off!  smiley

Right now you are using digitalWrite to turn pins on and off. To fade, you will need to use analog pins and analogWrite:

http://arduino.cc/en/Tutorial/PWM

The problem is, you are using 12 pins (three per LED x 4 LEDs) but many Arduino boards have only 6 PWM outputs. Which Arduino board (Uno/Leonardo/Mega/Due) are you using?
Logged

Pages: [1]   Go Up
Jump to: