Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« on: April 12, 2012, 02:13:04 pm » |
Hi everyone! Could really use some help with a small project me & my friend are doing, which shall be finished by tomorrow  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!
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 0
Posts: 182
|
 |
« Reply #2 on: April 12, 2012, 03:06:08 pm » |
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..
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« Reply #3 on: April 12, 2012, 03:20:39 pm » |
Thanks!
Is my circuit ok?
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9393
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #4 on: April 12, 2012, 03:21:52 pm » |
nice solution steen, could be really refactored !! void green() { analogWrite(redLed, 0); analogWrite(greenLed, 255); analogWrite(blueLed, 0); } ==> 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); }
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2916
I only know some basic electricity....
|
 |
« Reply #5 on: April 12, 2012, 03:23:14 pm » |
--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.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« Reply #6 on: April 12, 2012, 03:30:43 pm » |
You can download Fritzing for free @ ; http://fritzing.org/download/
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« Reply #7 on: April 12, 2012, 03:41:36 pm » |
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 :/...
|
|
|
|
|
Logged
|
|
|
|
|
Forum Administrator
MMX
Offline
Edison Member
Karma: 37
Posts: 1138
hallo kompis
|
 |
« Reply #8 on: April 12, 2012, 03:41:56 pm » |
@inicko: take a screenshot of your Fritzing file and post it ... it will make the commenting much easier.
/d
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« Reply #9 on: April 12, 2012, 03:44:33 pm » |
screenshot
|
|
|
|
|
Logged
|
|
|
|
|
Belgium
Offline
Full Member
Karma: 0
Posts: 182
|
 |
« Reply #10 on: April 12, 2012, 03:53:48 pm » |
i see only one button there ö weren't there 2 buttons? one for stopping and one for pausing?
|
|
|
|
|
Logged
|
|
|
|
|
Forum Administrator
MMX
Offline
Edison Member
Karma: 37
Posts: 1138
hallo kompis
|
 |
« Reply #11 on: April 12, 2012, 03:59:23 pm » |
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: 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; }
}
|
|
|
|
|
Logged
|
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« Reply #12 on: April 12, 2012, 04:05:04 pm » |
Thanks David! and yes there's supposed to be two buttons, but I forgot to add the 2nd one 
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2916
I only know some basic electricity....
|
 |
« Reply #13 on: April 12, 2012, 04:09:01 pm » |
If it's not running off a PC then button 1 should also be known as the power switch on the power strip.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Sweden
Offline
Newbie
Karma: 0
Posts: 25
Extremely rookie
|
 |
« Reply #14 on: April 12, 2012, 04:16:21 pm » |
How should I connect the buttons?
from pin 8 or 12 to the button, but then what?
|
|
|
|
|
Logged
|
|
|
|
|
|