Having trouble with my code for lights. I am trying to run RGB LEDs and change the colors on them without using delay because i got other things going on in the code. However, the code isn’t working. I know it has to be something to do with incrementing i, but i cant see a problem . any help would be awesome. The RGB LED will go red but wont change any other color. I know i got it hooked up properly I just cant get it to increment i.
int redPin = A8, greenPin = A9, bluePin = A10;
long previousMillis = 0;
long interval = 1000;
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop(){
// digitalWrite(musicPin, HIGH);
unsigned long currentMillis = millis();
int i = 1;
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis();
++i;
}
if(i==7) i = 1;
if(i==1) setColor(255, 0, 0); // Red
if(i==2) setColor(0, 255, 0); // Gree
if(i==3) setColor(0, 0, 255); // Blue
if(i==4) setColor(255, 255, 0); // Red + Green
if(i==5) setColor(0, 255, 255); // Green + Blue
if(i==6) setColor(255, 0, 255); // Red + Blue
}
void setColor(int red, int green, int blue){
analogWrite(redPin, 255-red);
analogWrite(greenPin, 255-green);
analogWrite(bluePin, 255-blue);
}
The code worked for a bit, but once i removed the delay on the motor code it stopped working. I must of messed up something that i’m not seeing. But now i got it seperated and it still wont work.
OK thanks for the help. Got the code working great. Its crazy how simple little mistakes are sometimes the hardest to recognize. Anyways here is the finished code. It turns on motor a for a specified amount of time and then off for the same time. Also runs RGB LEDs and changes their color incrementally. No delays used so it appears everything is running at once, although we know thats not true.
int motorPinA = 12, brakePinA = 9, motorPinB = 13, brakePinB = 8;
int redPin = A8, greenPin = A9, bluePin = A10;
int musicPin = 24;
int i = 1;
int brakeState = LOW;
long lightInterval = 5000;
long interval = 1000;
long previousMillis = 0;
long lightPrevMillis = 0;
void setup() {
//Setup Channel A
pinMode(motorPinA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakePinA, OUTPUT); //Initiates Brake Channel A pin
pinMode(motorPinB, OUTPUT); //Initiates Motor Channel B pin
pinMode(brakePinB, OUTPUT); //Initiates Brake Channel B pin
pinMode(redPin, OUTPUT); // Sets Red (A8) as an output
pinMode(greenPin, OUTPUT); // Sets Green (A9) as an ouput
pinMode(bluePin, OUTPUT); // Sets Blue (A10) as an output
}
void loop(){
motorControlLoop();
lightControlLoop();
}
void motorControlLoop(){
unsigned long currentMillis = millis();
digitalWrite (motorPinA, HIGH); //Establishes forward direction of Channel A
analogWrite(3,255); //Spins the motor on Channel A at half speed
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if(brakeState == LOW) brakeState = HIGH;
else brakeState = LOW;
digitalWrite(brakePinA, brakeState);
}
}
void lightControlLoop(){
digitalWrite(musicPin, HIGH);
unsigned long currentMillis = millis();
if (currentMillis - lightPrevMillis >= lightInterval){
i++;
lightPrevMillis = currentMillis;
}
if(i==1) setColor(255, 0, 0); // Red
if(i==2) setColor(0, 255, 0); // Gree
if(i==3) setColor(0, 0, 255); // Blue
if(i==4) setColor(255, 255, 0); // Red + Green
if(i==5) setColor(0, 255, 255); // Green + Blue
if(i==6) setColor(255, 0, 255); // Red + Blue
if(i==7) i = 1;
//Motor B forward @ full speed
digitalWrite(motorPinB, HIGH); //Establishes forward direction of Channel B
digitalWrite(brakePinB, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
}
void setColor(int red, int green, int blue){
analogWrite(redPin, 255-red);
analogWrite(greenPin, 255-green);
analogWrite(bluePin, 255-blue);
}