Go Down

Topic: project with RGB LEDs that wont shut off. (Read 458 times) previous topic - next topic

pwrbildr

Jun 01, 2013, 08:14 pm Last Edit: Jun 01, 2013, 08:51 pm by pwrbildr Reason: 1
Hey everyone I am having trouble with my project and for some reason I cant get my RGB LEDs to shut off.  They are common anode and I am using a function to control colors when they need to be on and one to tell them to shut off. The problem is the LEDs wont shut off.  They also keep cycling through the colors.  It seems to me that the function to turn off the lights isn't being called.  Any help would be appreciated.

Code: [Select]

const int timerHigh = 1024;
const int timerLow = 0;
int startButtonPin = 22;
int stopButtonPin = 23;
long timerInterval = 15000;

int motorPinA = 12, brakePinA = 9, motorPinB = 13, brakePinB = 8;
int redPin = A8, greenPin = A9, bluePin = A10;
int musicPin = 17;
int lightCount = 1;
int brakeState = LOW;

int infantPin = 14;
int toddlerPin = 15;

long lightInterval = 5000;
long interval = 1000;
long previousMillis = 0;
long lightPrevMillis = 0;

unsigned int matBits;
unsigned int lastBits = 0xFFFF; // 0x denotes hexadecimal notation. 0xFFFF is all bits set

int alarmPin = 28;
int alarmCount = 0;
int alarmState = LOW;




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(musicPin, OUTPUT);
 pinMode(startButtonPin, INPUT);
 pinMode(stopButtonPin, INPUT);
 
 pinMode(toddlerPin, INPUT);
 pinMode(infantPin, INPUT);
 
 pinMode(31, INPUT);
 pinMode(32, INPUT);
 pinMode(33, INPUT);
 pinMode(34, INPUT);
 pinMode(35, INPUT);
 pinMode(36, INPUT);
 pinMode(37, INPUT);
 pinMode(38, INPUT);
 pinMode(39, INPUT);
 pinMode(40, INPUT);
 pinMode(41, INPUT);
 pinMode(42, INPUT);
 pinMode(43, INPUT);
 pinMode(44, INPUT);
 pinMode(45, INPUT);
 pinMode(47, INPUT);
 
}

void loop(){
 
 unsigned long timerMillis = millis();
 long prevTimerMillis = 0;
 if(digitalRead(startButtonPin) == HIGH){
 motorControlOnLoop();
 lightControlOnLoop();
     if(timerMillis - prevTimerMillis > timerInterval){
       if(digitalRead(toddlerPin) == HIGH){
         motorControlOnLoop();
         lightControlOnLoop();
       }
       if(digitalRead(infantPin) == HIGH){
         sensorControlOnLoop();
       }
       else{
         motorOffLoop();
         lightOffLoop();
         sensorOffLoop();
       }
       prevTimerMillis = timerMillis;
     }  
   }  
   if(digitalRead(stopButtonPin) == HIGH){
     motorOffLoop();
     lightOffLoop();
     sensorOffLoop();
   }
}

void motorControlOnLoop(){  
     unsigned long currentMillis = millis();
     digitalWrite (motorPinA, HIGH);  //Establishes forward direction of Channel A
     analogWrite(3,255);    //Spins the motor on Channel A at full speed

     if(currentMillis - previousMillis >= interval){
       previousMillis = currentMillis;
     
     if(brakeState == LOW) brakeState = HIGH;
     else brakeState = LOW;
     
     digitalWrite(brakePinA, brakeState);
     }      
}

void motorOffLoop(){
 digitalWrite(motorPinA, LOW);
 if(brakeState == LOW) brakeState = HIGH;
 if(brakeState == HIGH) brakeState = HIGH;
 digitalWrite(brakePinA, brakeState);
}

void lightControlOnLoop(){
 
   digitalWrite(musicPin, HIGH);
   unsigned long currentMillis = millis();
   
    if (currentMillis - lightPrevMillis >= lightInterval){
      lightCount++;
     lightPrevMillis = currentMillis;  
   }
 
 
   if(lightCount == 1) setColor(255, 0, 0);      // Red
   if(lightCount == 2) setColor(0, 255, 0);      // Gree
   if(lightCount == 3) setColor(0, 0, 255);      // Blue
   if(lightCount == 4) setColor(255, 255, 0);    // Red + Green
   if(lightCount == 5) setColor(0, 255, 255);    // Green + Blue
   if(lightCount == 6) setColor(255, 0, 255);    // Red + Blue
   if(lightCount == 7) lightCount = 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 lightOffLoop(){
 
    setColor(0,0,0);
   digitalWrite(brakePinB, LOW);
   digitalWrite(motorPinB, LOW);
   
}
 
void setColor(int red, int green, int blue){
   analogWrite(redPin, 255-red);
   analogWrite(greenPin, 255-green);
   analogWrite(bluePin, 255-blue);
 }
void sensorControlOnLoop(){
 
 for ( byte k = 31; k < 47; k++ )
 {
   bitWrite( matBits, k - 31, digitalRead( k ) );
 }

 if ( matBits != lastBits )
 {
   ++alarmCount;
   if(alarmCount > 25){
     digitalWrite(alarmPin, HIGH);
 }
 else alarmCount = 0;

 lastBits = matBits;
} // end of loop()
}

void sensorOffLoop(){
 digitalWrite(alarmPin, LOW);
}


Also I am using the Arduino mega 2560.

TanHadron

I don't think this does what you want it to do.
Code: [Select]
  long prevTimerMillis = 0;


It gets re-initialized to 0 every time the loop() function is called.  If you want the value to be persistent, you should either move it global, or declare it static.

pwrbildr

wow didnt even realize i did that. thanks ill see if it works later

PeterH

Also, it would be better to consistently hold all time values as unsigned long.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up