Go Down

Topic: RGB Led, School-project, need help! (Read 2203 times) previous topic - next topic

inicko

Hi everyone!

Could really use some help with a small project me & my friend are doing, which shall be finished by tomorrow  :smiley-sad-blue:

In short we want to make a rgb-led flash in random colors, approx 9 different, and make the led stop randomizing when a button is pressed.

We also need a on/off switch, preferably a simple button in lack of hardware...

So the whole process should be;

1. turn on (button 1)
2. press (button 2) to make the led light up with random colors
3. press (button 2) again for led to fix the color
4. press (button 2) to make the led start randomizing again...
5. turn off

You get the point.  AND by the way, we don't want any fading between the colors.

We have tried ¨hacking¨ some code, but we can't get the buttons to work as we intended.


Check the Fritz-file to see how I wired it up. Feel free to make changes if it sucks.

Thanks in advance!

inicko

We used this code for making the led light up in different colours: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285904797

Steen

Code: [Select]

int redLed = 11;
int greenLed = 10;
int blueLed = 9;

int btn1 = 4;
int btn2 = 5;

boolean random = true;
boolean turnOn = true;

long randNumb;
long randTime;

void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}

void red()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}

void green()
{
analogWrite(redLed, 0);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
}

void blue()
{
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 255);
}

void yellow()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
}

void orange()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 35);
analogWrite(blueLed, 255);
}

void pink()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 162);
}

void purple()
{
analogWrite(redLed, 88);
analogWrite(greenLed, 0);
analogWrite(blueLed, 112);
}

void bluegreen()
{
analogWrite(redLed, 0);
analogWrite(greenLed, 144);
analogWrite(blueLed, 255);
}

void white()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 255);
analogWrite(blueLed, 255);
}

void loop()
{
if (random && turnOn) {
randNumb = random(1, 10); // Generate a random number between 1 and 10
randTime = random(250, 1001);  // Generate a random number between 250 and 1000

if(randNumb == 1){
 red();
}
if(randNumb == 2){
 green();
}
if(randNumb == 3){
 blue();
}
if(randNumb == 4){
 yellow();
}
if(randNumb == 5){
 orange();
}
if(randNumb == 6){
 pink();
}
if(randNumb == 7){
 purple();
}
if(randNumb == 8){
 bluegreen();
}
if(randNumb == 9){
 white();
}

delay(randTime);
}
else if (not turnOn) {
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
else if (not random) {
analogWrite(redLed, 50);
analogWrite(greenLed, 50);
analogWrite(blueLed, 50);
}

if (digitalRead(btn1) == HIGH) {
random = not random;
}

if (digitalRead(btn2) == HIGH) {
turnOn = not turnOn;
}

}


something like this? i did not test it yet, but i think it should work..

inicko


robtillaart

nice solution steen,

could be really refactored !!  

Code: [Select]

void green()
{
analogWrite(redLed, 0);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
}


==>
Code: [Select]

void green()  // and all others
{
 color(0,255,0);
}

void color(int R, int G, int B)
{
analogWrite(redLed, R);
analogWrite(greenLed, G);
analogWrite(blueLed, B);
}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

GoForSmoke

--Cripes, I can't type a post without others sliding in ahead and changing the field!--

Are you debouncing your buttons?  That last example uses no debounce.

Also, random colors... you need only generate sets of 3 0-255 randoms and pass them to 1 routine that uses 3 PWM pins to power the led. The real hard part is getting good 'randoms'. If you can feed it a seed now and then it would help otherwise perhaps reading an unterminated analog pin for the least significant digits might do for randoms or seeds.

If/when you post your code, use the # key (above the smilies) to make code tags for it. Quote tags won't do and that tool in the IDE to post your code is even worse. Just select all, copy, set up the code tags and paste in between.

I dunno what it takes to show the Fritzing in your post but other people do it. I don't have anything to view the file with otherwise so I guess you must have wired everything up right.

Done by tomorrow? It could be.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

inicko

You can download Fritzing for free @ ; http://fritzing.org/download/

inicko

A comment to the post by Steen.

It says ¨error: in argument to unary¨, for boolean random = true;

I don't know anything about boolean :/...

David Cuartielles

@inicko: take a screenshot of your Fritzing file and post it ... it will make the commenting much easier.

/d

inicko


Steen

i see only one button there ö weren't there 2 buttons? one for stopping and one for pausing?

David Cuartielles

First thing:

remove the button from pin 13 and place it in pin 12 instead. Pin 13 contains a resistor and an LED that will mess up with the values of your input.

Then:

add your second button to pin 8

Finally, use the code proposed by @Steen with some small modifications as shown here:

Code: [Select]
int redLed = 11;
int greenLed = 10;
int blueLed = 9;

int btn1 = 12;
int btn2 = 8;

boolean rnd = true;
boolean turnOn = true;

long randNumb;
long randTime;

void setup()
{
 pinMode(redLed, OUTPUT);
 pinMode(greenLed, OUTPUT);
 pinMode(blueLed, OUTPUT);
 
 pinMode(btn1, INPUT);
 digitalWrite(btn1, HIGH);
 pinMode(btn2, INPUT);
 digitalWrite(btn2, HIGH);
}

void red()
{
 analogWrite(redLed, 255);
 analogWrite(greenLed, 0);
 analogWrite(blueLed, 0);
}

void green()
{
 analogWrite(redLed, 0);
 analogWrite(greenLed, 255);
 analogWrite(blueLed, 0);
}

void blue()
{
 analogWrite(redLed, 0);
 analogWrite(greenLed, 0);
 analogWrite(blueLed, 255);
}

void yellow()
{
 analogWrite(redLed, 255);
 analogWrite(greenLed, 255);
 analogWrite(blueLed, 0);
}

void orange()
{
 analogWrite(redLed, 255);
 analogWrite(greenLed, 35);
 analogWrite(blueLed, 255);
}

void pink()
{
 analogWrite(redLed, 255);
 analogWrite(greenLed, 0);
 analogWrite(blueLed, 162);
}

void purple()
{
 analogWrite(redLed, 88);
 analogWrite(greenLed, 0);
 analogWrite(blueLed, 112);
}

void bluegreen()
{
 analogWrite(redLed, 0);
 analogWrite(greenLed, 144);
 analogWrite(blueLed, 255);
}

void white()
{
 analogWrite(redLed, 255);
 analogWrite(greenLed, 255);
 analogWrite(blueLed, 255);
}

void loop()
{
 if (rnd && turnOn) {
   randNumb = random(1, 10); // Generate a random number between 1 and 10
   randTime = random(250, 1001);  // Generate a random number between 250 and 1000

   if(randNumb == 1){
     red();
   }
   if(randNumb == 2){
     green();
   }
   if(randNumb == 3){
     blue();
   }
   if(randNumb == 4){
     yellow();
   }
   if(randNumb == 5){
     orange();
   }
   if(randNumb == 6){
     pink();
   }
   if(randNumb == 7){
     purple();
   }
   if(randNumb == 8){
     bluegreen();
   }
   if(randNumb == 9){
     white();
   }

   delay(randTime);
 }
 else if (not turnOn) {
   analogWrite(redLed, 0);
   analogWrite(greenLed, 0);
   analogWrite(blueLed, 0);
 }
 else if (not rnd) {
   analogWrite(redLed, 50);
   analogWrite(greenLed, 50);
   analogWrite(blueLed, 50);
 }

 if (digitalRead(btn1) == HIGH) {
   rnd = not rnd;
 }

 if (digitalRead(btn2) == HIGH) {
   turnOn = not turnOn;
 }

}

inicko

Thanks David!

and yes there's supposed to be two buttons, but I forgot to add the 2nd one  XD

GoForSmoke

If it's not running off a PC then button 1 should also be known as the power switch on the power strip.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

inicko

How should I connect the buttons?

from pin 8 or 12 to the button, but then what?

Go Up