Go Down

Topic: I want my void loop to start when the lightsensor sais so and stop the instant (Read 508 times) previous topic - next topic

SuperSlimJ

I want my void loop to start when the lightsensor sais so and stop the instant is sais to without continuing the loop, i have an rgb led that does a cross fade when it gets dark and is suppused to stop when the light goes on but it just continues untile it finishes the loop please help me
 
  int photoPin = 1; // designate pin 1 as the analog in for the photo resitor
  int val = 0;
 
  int redPin = 9;   // Red LED,   connected to digital pin 9
  int grnPin = 10;  // Green LED, connected to digital pin 10
  int bluPin = 11;  // Blue LED,  connected to digital pin 11
  int black[3]  = { 0, 0, 0 };
  int white[3]  = { 100, 100, 100 };
  int red[3]    = { 100, 0, 0 };
  int green[3]  = { 0, 100, 0 };
  int blue[3]   = { 0, 0, 100 };
  int yellow[3] = { 40, 95, 0 };
  int dimWhite[3] = { 30, 30, 30 };
  int redVal = black[0];
  int grnVal = black[1];
  int bluVal = black[2];
  int wait = 3;      // 10ms internal crossFade delay; increase for slower fades
  int hold = 0;       // Optional hold when a color is complete, before the next crossFade
  int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
  int loopCount = 60; // How often should DEBUG report?
  int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
  int j = 0;          // Loop counter for repeat 
  int prevR = redVal;
  int prevG = grnVal;
  int prevB = bluVal;

  void setup()                    // run once, when the sketch starts
  {
    Serial.begin(9600);
    pinMode(photoPin,INPUT); // set the pin
    pinMode(redPin,OUTPUT); // set the pin
    pinMode(grnPin,OUTPUT); // set the pin
    pinMode(bluPin,OUTPUT); // set the pin
  }

  void loop()                     // run over and over again
  {

    val = analogRead(photoPin); // set val equal to the resitor input

    if (analogRead(photoPin) <20) { // if nothing from photo resistor (low light), turn on led

    crossFade(red);
    crossFade(green);
    crossFade(blue);
    crossFade(yellow);
   
    if (repeat) { // Do we loop a finite number of times?
      j += 1;
      if (j >= repeat) { // Are we there yet?
        exit(j);         // If so, stop.
    }
  }   

;if (analogRead(photoPin) >20)
    digitalWrite(redPin, LOW);
    digitalWrite(grnPin, LOW);
    digitalWrite(bluPin, LOW);
  }
  Serial.println(val);
}

int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?
  if (step) {                      // If its non-zero,
    step = 1020/step;              //   divide by 1020
  }
  return step;
}

/* The next function is calculateVal. When the loop value, i,
*  reaches the step size appropriate for one of the
*  colors, it increases or decreases the value of that color by 1.
*  (R, G, and B are each calculated separately.)
*/

int calculateVal(int step, int val, int i) {

  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;           
    }
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    }
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  }
  else if (val < 0) {
    val = 0;
  }
  return val;
}

/* crossFade() converts the percentage colors to a
*  0-255 range, then loops 1020 times, checking to see if 
*  the value needs to be updated each time, then writing
*  the color values to the correct pins.
*/

void crossFade(int color[3]) {
  // Convert to 0-255
  int R = (color[0] * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G);
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);     
    analogWrite(bluPin, bluVal);

    delay(wait); // Pause for 'wait' milliseconds before resuming the loop

    if (DEBUG) { // If we want serial output, print it at the
      if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
        Serial.print("Loop/RGB: #");
        Serial.print(i);
        Serial.print(" | ");
        Serial.print(redVal);
        Serial.print(" / ");
        Serial.print(grnVal);
        Serial.print(" / "); 
        Serial.println(bluVal);
      }
      DEBUG += 1;
    }
  }
  // Update current values for next loop
  prevR = redVal;
  prevG = grnVal;
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}

marco_c

You cannot actually 'stop' loop() but you just exit if you don't need to do anything

Code: [Select]
void loop()
{
  if (condition_to_exit)
    return;

  ... put your code to execute here
}
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

johnwasser

Put this in your fade loop:

Code: [Select]
  if (analogRead(photoPin) >= 20) return;

and check before each fade() call:
Code: [Select]

     if (analogRead(photoPin) < 20) crossFade(red);
     if (analogRead(photoPin) < 20) crossFade(green);
     if (analogRead(photoPin) < 20) crossFade(blue);
     if (analogRead(photoPin) < 20) crossFade(yellow);
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up