RGB LED, random colour generator not working
hello all,
new both forum , arduino in general. have tried out many different sketches rgb led randomly choose , stay on colour each time button pressed. instead continuously cycles through different colours pausing briefly when button pushed. hope can me, project due next week , lost.
// variables assign outputs each color
const int ledredpin = 10;
const int ledgreenpin = 9;
const int ledbluepin = 8;
// variable assign pin button
const int buttonpin = 2;
// variables control intensity of each color
int red;
int green;
int blue;
// variable state of button (high or low)
int butstate;
void setup () {
pinmode (ledredpin, output);
pinmode (ledgreenpin, output);
pinmode (ledbluepin, output);
pinmode (buttonpin, input);
// initialize colours
red = random(256);
green = random(256);
blue = random (256);
}
void loop() {
// output led
digitalwrite (ledredpin, red);
digitalwrite (ledgreenpin, green);
digitalwrite (ledbluepin, blue);
butstate = digitalread (buttonpin);
// change colour values
if (butstate == high) {
//randomseed();
red = random(256);
green = random(256);
blue = random (256);
}
}
new both forum , arduino in general. have tried out many different sketches rgb led randomly choose , stay on colour each time button pressed. instead continuously cycles through different colours pausing briefly when button pushed. hope can me, project due next week , lost.
// variables assign outputs each color
const int ledredpin = 10;
const int ledgreenpin = 9;
const int ledbluepin = 8;
// variable assign pin button
const int buttonpin = 2;
// variables control intensity of each color
int red;
int green;
int blue;
// variable state of button (high or low)
int butstate;
void setup () {
pinmode (ledredpin, output);
pinmode (ledgreenpin, output);
pinmode (ledbluepin, output);
pinmode (buttonpin, input);
// initialize colours
red = random(256);
green = random(256);
blue = random (256);
}
void loop() {
// output led
digitalwrite (ledredpin, red);
digitalwrite (ledgreenpin, green);
digitalwrite (ledbluepin, blue);
butstate = digitalread (buttonpin);
// change colour values
if (butstate == high) {
//randomseed();
red = random(256);
green = random(256);
blue = random (256);
}
}
make sure button wired low resistor ground (not floating), , button need debounced, or able prevent being held down.
you can fill in rest , make currstate, laststate boolean @ top of sketch.
tip, if know variables not going exceed (-32,768 32,767) = int or (0 65,535) = unsigned int, can make them byte save memory. oh , analogwrite, not digital.
you can fill in rest , make currstate, laststate boolean @ top of sketch.
code: [select]
currstate = digitalread( 2 );
if( currstate != laststate)
{
laststate = currstate;
if(currstate == high)
{
// colors here
}
}
tip, if know variables not going exceed (-32,768 32,767) = int or (0 65,535) = unsigned int, can make them byte save memory. oh , analogwrite, not digital.
Arduino Forum > Using Arduino > Programming Questions > RGB LED, random colour generator not working
arduino
Comments
Post a Comment