#define G 9
#define B 10
#define R 11
#define interruptPin 2
#define alternate_delay 100
#define rand_delay 200
int mode;
void setup() {
mode=1;
pinMode(interruptPin, INPUT);
//pinMode(R, OUTPUT);
//pinMode(G, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), mode_change_isr, RISING);
}
void pwm_anim()
{
// fade in from min to max in increments of 5 points:
analogWrite(R, 0);
analogWrite(G, 0);
analogWrite(B, 0);
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(R, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(G, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(B, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(R, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(G, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(B, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
}
void alternate_flash()
{
digitalWrite(R, HIGH);
delay(alternate_delay);
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
delay(alternate_delay);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
delay(alternate_delay);
digitalWrite(B, LOW);
}
void two_color_anim()
{
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(R, fadeValue);
analogWrite(G, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(R, fadeValue);
analogWrite(G, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(R, fadeValue);
analogWrite(B, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(R, fadeValue);
analogWrite(B, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(B, fadeValue);
analogWrite(G, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(B, fadeValue);
analogWrite(G, fadeValue);
// wait for 40 milliseconds to see the dimming effect
delay(40);
}
}
void loop()
{
interrupts();
switch(mode)
{
case 1: pwm_anim();
break;
case 2: alternate_flash();
break;
case 3: two_color_anim();
break;
case 4: rand_anim();
break;
case 5: only_red();
break;
case 6: only_green();
break;
case 7: only_blue();
break;
case 8: red_green();
break;
case 9: red_blue();
break;
case 10:green_blue();
break;
case 11:all();
break;
}
}
void rand_anim()
{
switch(random(1, 4))
{
case 1: digitalWrite(R, HIGH);
delay(rand_delay);
digitalWrite(R,LOW);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
break;
case 2: digitalWrite(R, LOW);
digitalWrite(G, HIGH);
delay(rand_delay);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
break;
case 3: digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
delay(rand_delay);
digitalWrite(B, LOW);
break;
}
}
void only_red()
{
digitalWrite(R, HIGH);
digitalWrite(G, LOW);
digitalWrite(B, LOW);
}
void only_green()
{
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
}
void only_blue()
{
digitalWrite(R, LOW);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
}
void red_green()
{
digitalWrite(R, HIGH);
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
}
void red_blue()
{
digitalWrite(R, HIGH);
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
}
void green_blue()
{
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
digitalWrite(B, HIGH);
}
void all()
{
digitalWrite(R, HIGH);
digitalWrite(G, HIGH);
digitalWrite(B, HIGH);
}
void mode_change_isr()
{
noInterrupts();
if(mode < 11)
{
++mode;
}
else if(mode >= 11)
{
mode=1;
}
/*if(a==1)
{
digitalWrite(R, HIGH);
digitalWrite(G, LOW);
a=2;
}
else
{
digitalWrite(R, LOW);
digitalWrite(G, HIGH);
a=1;
}
interrupts();*/
}
Here is the code.
There is also a button on the board which can also be used to change the mode, on press which will interrupt the arduino to change the mode