Go Down

Topic: difficulty in timing and delaying processes (Read 71 times) previous topic - next topic

urbanmiles

Hi everyone ,, i have the setup below where :

>>at button hold:
    activate set1() function
      1. turn on playPin
      2. 500uS interval, then turn off playPin

    activate set2() function
      1. turn on ledPin1
      2. wait for 5oouS then turn on ledPin2
      3. wait for 5oouS then turn on ledPin3
      4. wait for 5oouS then turn on ledPin4
      *led1, led2, led3and led4 mus be hold to ON state as long as the button is being held

>>at button release:
     activate set1() function again 
        1. turn on playPin
        2. 500uS interval, then turn off playPin

     activate set2() function
        1. turn off ledPin1
        2. wait for 5oouS then turn off ledPin2
        3. wait for 5oouS then turn off ledPin3
        4. wait for 5oouS then turn off ledPin4
       *led1, led2, led3and led4 mus be hold to OFF state as long as the button is at rest


these two functions are of independent of each other , and the sketch is working properly . However,
I am having difficulty delaying the operation of set2 for this kind of setup without affecting the timing of the processes inside set2 and set1 .

That is, if i hold the button down, set1 will process and then set2 must wait for a period of 5s before it starts its process .

I'm just a beginner and i am in still in the process of learning things around arduino .
Thanks in advance for the help guys .

here's the code

Code: [Select]




const int playPin = 9;
const int ledPin1 = 10;
const int ledPin2 = 11;
const int ledPin3 = 12;
const int ledPin4 = 13;

const int DisplayButton = 2;
unsigned long startTime;        // will store last time LED was updated
unsigned long startTime2;


int lastButtonState = 0;
int lastButtonState2 = 0;
unsigned long interval =  500UL; // interval at which to blink (milliseconds)
unsigned long interval2 = 500UL;
int ledState;
int ledState2;

void setup()
{
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4,  OUTPUT);
  pinMode(playPin, OUTPUT);
  pinMode(DisplayButton, INPUT); 
}

void loop()
{

 
  set1();
  set2();
}
 
void set1() 

  {
        int buttonState = digitalRead(DisplayButton);
  if (buttonState != lastButtonState) // on any state change...
  {
    startTime2 = millis();// .... reset the timer
    ledState = !ledState;
   
  }
  lastButtonState = buttonState;
 
    if (millis() - startTime2 <= interval2)
    {
      digitalWrite(playPin, HIGH);
    }
    else
    {
    digitalWrite(playPin, LOW);
    }
  }   
}


void set2()
{
      int buttonState2 = digitalRead(DisplayButton);
     
     
     
  if (buttonState2 != lastButtonState2) // on any state change...
  {
    startTime = millis();// .... reset the timer
    ledState2 = !ledState2;
  }
   lastButtonState2 = buttonState2;
         
    if (ledState2)
    {
      if (millis() - startTime <= interval)
      {
        digitalWrite(ledPin1, HIGH);
      }
      else if (millis() - startTime <= interval * 2)
      {
        digitalWrite(ledPin2, HIGH);
      }
      else if (millis() - startTime <= interval * 3)
      {
        digitalWrite(ledPin3, HIGH);
      }
       else if (millis() - startTime <= interval * 4)
      {
        digitalWrite(ledPin4, HIGH);
      }
    }
 
    else
    {
      if (millis() - startTime <= interval)
      {
        digitalWrite(ledPin1, LOW);
      }
      else if (millis() - startTime <= interval * 2)
      {
        digitalWrite(ledPin2, LOW);
      }
      else if (millis() - startTime <= interval * 3)
      {
       digitalWrite(ledPin3, LOW);
      }
      else if (millis() - startTime <= interval * 4)
      {
       digitalWrite(ledPin4, LOW);
      }
    }
}



Robin2

I haven't studied your code carefully but it occurs to me that changing your code so that the buttons are all read at the same time may help. It may also be a ood idea to use a single value of currentMillis for all the timing. Stuff like
Code: [Select]
if (millis() - startTime2 <= interval2) would become
Code: [Select]
if (currentMillis - startTime2 <= interval2)

For example

void loop() {
   currentMillis = millis();
   readButtons();
   set1();
   set2();
}

Have you seen the demo several things at a time ?

...R

Go Up