Pages: [1]   Go Down
Author Topic: Delay help?  (Read 790 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, for my project i have three led's, red, green, blue and a push button. When the button is pressed (high) all three led's work and I control them with a potentiometer. The problem comes as when i released the button (low) i want the red and blue led's to switch off straight away but for the green to stay of for an extra 10 seconds.

What do I need to put into my code as a simple delay doesn't seem to be working?

Thank
agau
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1024
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the easy, but limited option is to turn off the 2 LEDs and then delay(10000) which doesn't allow anything else to happen or use a variable and millis().

Look at the reference under the Time heading
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Can you post the code you have sofar?

The blink without delay example is the basis of clock driven events. You should check out that technique
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

Thanks, this is the code, i have added a couple of more led but its still only the green i need left on for a time after the switch is released.



int potPin = A0; // Potentiometer output connected to analog pin A0
double potVal = 0;// Variable to store the input from the potentiometer

const int buttonPin = 2;

int greenPin = 3;
int whitePin = 11;   
int bluePin = 10; 
int yellowPin = 9; 
int redPin = 6;
int flashPin = 5;


// Program variables
int buttonState = 0;
int greenVal = 0;
int whiteVal = 0; 
int blueVal = 0; 
int yellowVal = 0; 
int redVal = 0;
int flashVal = 0;



int DEBUG = 1;          // Set to 1 to turn on debugging output

void setup()
{
  pinMode (buttonPin, INPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(whitePin, OUTPUT);   // sets the pins as output
  pinMode(bluePin, OUTPUT);   
  pinMode(yellowPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(flashPin, OUTPUT);



  if (DEBUG) {           // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput in 0004 format
  }
}

// Main program
void loop(){
  potVal = analogRead(potPin);


  //potVal = analogRead(potPin);
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH)
  {

    digitalWrite (greenPin, HIGH);



    potVal = analogRead(potPin);   // read the potentiometer value at the input pin

      if (potVal < 341)  // Lowest third of the potentiometer's range (0-340)
    {                 
      potVal = (potVal * 3) / 4; // Normalize to 0-255

      whiteVal = 255;  // Red from full to off
      blueVal = 0;        // blue from off to full
      yellowVal = 0;
      redVal = 0;
      flashVal = 0;


    }
    else if (potVal < 682) // Middle third of potentiometer's range (341-681)
    {
      potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255

      whiteVal = 255- potVal;            // Red off
      blueVal = potVal; // Green from full to off
      yellowVal = potVal;       // Blue from off to full
      redVal = 0;
      flashVal = 0;


    }
    else if  (potVal < 900)// Upper third of potentiometer"s range (682-1023)
    {
      potVal = ( (potVal-682) * 3) / 4; // Normalize to 0-255

      whiteVal = 0;       // Red from off to full
      blueVal = 255 - potVal;            // Green off
      yellowVal = 255 - potVal; // Blue from full to off
      redVal = potVal;
      flashVal = 0;

    }

    else
    {
      potVal = ( (potVal-900) * 3) / 4; // Normalize to 0-255

      whiteVal = 0;       // Red from off to full
      blueVal = 0;            // Green off
      yellowVal = 0; // Blue from full to off
      redVal = 255l;
      flashVal = potVal;
      analogWrite (flashPin, flashVal);
      delay(100);
      flashVal = 0;
      analogWrite (flashPin, flashVal);
      delay(100);
      flashVal = potVal;
      analogWrite (flashPin, flashVal);
      delay(100);
      flashVal = 0;
      analogWrite (flashPin, flashVal);
      delay(100);

    }
    analogWrite (whitePin, whiteVal);            // Green off
    analogWrite (bluePin, blueVal);
    analogWrite (yellowPin, yellowVal);
    analogWrite (redPin, redVal);
    analogWrite (flashPin, flashVal);
    digitalWrite (greenPin, greenVal);

  }
  else
  {

    analogWrite (whitePin, 0);            // Green off
    analogWrite (bluePin, 0);
    analogWrite (yellowPin, 0);
    analogWrite (redPin, 0);
    analogWrite (flashPin, 0);
    digitalWrite (greenPin, LOW);

  }


  /*
  if (DEBUG) { // If we want to read the output
   DEBUG += 1;      // Increment the DEBUG counter
   if (DEBUG > 100) // Print every hundred loops
   {
   DEBUG = 1;     // Reset the counter
   // Serial output using 0004-style functions
   Serial.print("w:");    // Indicate that output is red value
   Serial.print(whiteVal);  // Print red value
   Serial.print("\t");    // Print a tab
   Serial.print("b:");    // Repeat for grn and blu...
   Serial.print(blueVal);
   Serial.print("\t");   
   Serial.print("bb:");   
   Serial.println(bblueVal);
   Serial.print("\t");
   Serial.print("r:");    // Indicate that output is red value
   Serial.print(redVal);  // Print red value
   Serial.print("\t");
   Serial.print("rr:");    // Indicate that output is red value
   Serial.print(redVal);
   Serial.print("\t");  // Print red value
   }
   }
   
   
   else {
   
   analogWrite (whitePin, 0);            // Green off
   analogWrite (bluePin, 0);
   analogWrite (bbluePin, 0);
   analogWrite (redPin, 0);
   analogWrite (rredPin, 0);
   analogWrite (flashPin, 0);
   analogWrite (greenPin, 0);
   
   
   }
   */
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems unlikely that the problem is in the commented out code. Why did you post all that stuff? Incorrectly, at that.

Modify your code. Remove all the commented out stuff. Modify your post. Post the amended code, using the # icon.
Logged

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

Code:


int potPin = A0; // Potentiometer output connected to analog pin 3
double potVal = 0;// Variable to store the input from the potentiometer

const int buttonPin = 2;

int greenPin = 3;
int whitePin = 11;   
int bluePin = 10; 
int yellowPin = 9; 
int redPin = 6;
int flashPin = 5;





// Program variables
int buttonState = 0;
int greenVal = 0;
int whiteVal = 0; 
int blueVal = 0; 
int yellowVal = 0; 
int redVal = 0;
int flashVal = 0;




int DEBUG = 1;          // Set to 1 to turn on debugging output

void setup()
{
  pinMode (buttonPin, INPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(whitePin, OUTPUT);   // sets the pins as output
  pinMode(bluePin, OUTPUT);   
  pinMode(yellowPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(flashPin, OUTPUT);




  if (DEBUG) {           // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput in 0004 format
  }
}

// Main program
void loop(){
  potVal = analogRead(potPin);


  //potVal = analogRead(potPin);
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH)
  {

    digitalWrite (greenPin, HIGH);




    potVal = analogRead(potPin);   // read the potentiometer value at the input pin

      if (potVal < 341)  // Lowest third of the potentiometer's range (0-340)
    {                 
      potVal = (potVal * 3) / 4; // Normalize to 0-255

      whiteVal = 255;  // Red from full to off
      blueVal = 0;        // blue from off to full
      yellowVal = 0;
      redVal = 0;
      flashVal = 0;


    }
    else if (potVal < 682) // Middle third of potentiometer's range (341-681)
    {
      potVal = ( (potVal-341) * 3) / 4; // Normalize to 0-255

      whiteVal = 255- potVal;            // Red off
      blueVal = potVal; // Green from full to off
      yellowVal = potVal;       // Blue from off to full
      redVal = 0;
      flashVal = 0;


    }
    else if  (potVal < 900)// Upper third of potentiometer"s range (682-1023)
    {
      potVal = ( (potVal-682) * 3) / 4; // Normalize to 0-255

      whiteVal = 0;       // Red from off to full
      blueVal = 255 - potVal;            // Green off
      yellowVal = 255 - potVal; // Blue from full to off
      redVal = potVal;
      flashVal = 0;

    }

    else
    {
      potVal = ( (potVal-900) * 3) / 4; // Normalize to 0-255

      whiteVal = 0;       // Red from off to full
      blueVal = 0;            // Green off
      yellowVal = 0; // Blue from full to off
      redVal = 255l;
      flashVal = potVal;
      analogWrite (flashPin, flashVal);
      delay(100);
      flashVal = 0;
      analogWrite (flashPin, flashVal);
      delay(100);
      flashVal = potVal;
      analogWrite (flashPin, flashVal);
      delay(100);
      flashVal = 0;
      analogWrite (flashPin, flashVal);
      delay(100);

    }
    analogWrite (whitePin, whiteVal);            // Green off
    analogWrite (bluePin, blueVal);
    analogWrite (yellowPin, yellowVal);
    analogWrite (redPin, redVal);
    analogWrite (flashPin, flashVal);
    digitalWrite (greenPin, greenVal);

   

  }
  else
  {

    analogWrite (whitePin, 0);            // Green off
    analogWrite (bluePin, 0);
    analogWrite (yellowPin, 0);
    analogWrite (redPin, 0);
    analogWrite (flashPin, 0);
    digitalWrite (greenPin, LOW);

 

  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Snipping a bunch of code (and a lot of useless white space), you have this:
Code:
void loop(){
  potVal = analogRead(potPin);
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH)
  {
    digitalWrite (greenPin, HIGH);
    potVal = analogRead(potPin);   // read the potentiometer value at the input pin
  }
  else
  {
    digitalWrite (greenPin, LOW);
  }
}
So, the switch gets pressed, and you turn the green pin on. The switch gets released, so you turn the green pin off. Where is the attempt to keep the green pin on for a while?

Why are you reading the potentiometer twice?
Logged

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

Yeah thats the plan, I had a delay put in but I never worked.

Code:
digitalWrite (greenPin, HIGH);
delay(3000)
digitalWrite (greenPin, LOW);

oops just released that, thanks for pointing that out!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I had a delay put in but I never worked.
And this is somehow the Arduino's fault?  smiley
Logged

Pages: [1]   Go Up
Jump to: