hey there, fellow arduino noob here, I have sucessfully written code for the project i'm working on, and am now going back through trying to figure out how to optimize it.
The goal: have one RGB led flash in a series of patterns, which can be changed with the push of a button, which will be later read via photocell to create interesting beats.
Here is what I have so far. In the end I want the patterns to be much more complex, but for now this just demonstrates that it works. The button interrupt could use some tweaking...
const int Rpin = 10;
const int Gpin = 9;
const int Bpin = 8;
const int debugLED = 13;
int patternmode = 0; //different modes for patterns. starts at 0
volatile int state = LOW;
void setup(){
pinMode(Rpin,OUTPUT);
pinMode(Gpin,OUTPUT);
pinMode(Bpin,OUTPUT);
pinMode(debugLED,OUTPUT);
attachInterrupt(0,checkButton,RISING); // (0 = pin 2)
}
void loop(){
switch (patternmode) {
case 0:
patternFlick();
break;
case 1:
patternBlink();
break;
case 2:
patternPulse();
break;
}
}
void checkButton(){ //This controls the LED and checks the button and sends back a value for the cases
digitalWrite(debugLED, HIGH); // make Led turn on when button is pressed
patternmode = (patternmode + 1) % 3; //INCREMENT COUNTER. is the % 3 even necessary?
delay(2500); // 1/2 second
digitalWrite(debugLED, LOW);
}
void patternFlick() {
digitalWrite(Rpin,HIGH);
digitalWrite(Gpin,LOW);
digitalWrite(Bpin,LOW);
delay(50);
digitalWrite(Rpin,LOW);
digitalWrite(Gpin,HIGH);
digitalWrite(Bpin,LOW);
delay(50);
digitalWrite(Rpin,LOW);
digitalWrite(Gpin,LOW);
digitalWrite(Bpin,HIGH);
delay(50);
}
void patternBlink(){
digitalWrite(Rpin,HIGH);
digitalWrite(Gpin,HIGH);
digitalWrite(Bpin,LOW);
delay(50);
digitalWrite(Rpin,LOW);
digitalWrite(Gpin,HIGH);
digitalWrite(Bpin,HIGH);
delay(50);
digitalWrite(Rpin,HIGH);
digitalWrite(Gpin,LOW);
digitalWrite(Bpin,HIGH);
delay(50);
}
void patternPulse(){
digitalWrite(Rpin,HIGH);
digitalWrite(Gpin,HIGH);
digitalWrite(Bpin,HIGH);
delay(500);
digitalWrite(Rpin,LOW);
digitalWrite(Gpin,HIGH);
digitalWrite(Bpin,LOW);
delay(500);
digitalWrite(Rpin,HIGH);
digitalWrite(Gpin,LOW);
digitalWrite(Bpin,HIGH);
delay(500);
}
I want to get away from using the delays so that it doesn't slow down the code.
I have found tutorials that show you how to use arrays to control patterns across multiple LED's, but I haven't found any information for delivering a string of 0's and 1's to a single LED. Have any advice on where to look?