Pages: [1]   Go Down
Author Topic: project with RGB LEDs that wont shut off.  (Read 391 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: June 01, 2013, 01:51:25 pm by pwrbildr » Logged

Offline Offline
God Member
*****
Karma: 25
Posts: 526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think this does what you want it to do.
Code:
  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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, it would be better to consistently hold all time values as unsigned long.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: