voidloop

Hi, I have a request regarding programming. This is me on the "delay". If used twice, the second is delayed by the previous one. For example, I want to retrieve the value of the first thermal sensor every 2 seconds. And the second one for 5 seconds. Both have the "voidloop" and somehow it does not work. I never finished reading it if you can somehow create a second "loop." Or how to avoid the problem. Thanks for any advice.

Show us what you have done.

Don't use delay see the blink without delay example.

Mark

We show that a simple example. For some, perhaps absolutely simple, I do not know how to improve. This is logical because it is a loop but I would therefore best independent loops, maybe two or more. That I do not know how to do. Here is a clear example. LED2 will blink at intervals of 1000 and then LED1 in the interval 5000th course closes with a loop and all intervals are added. A loop is extended.

int led = 13;
int led2 = 12;

void setup() {                

  pinMode(led, OUTPUT);     
 pinMode(led2, OUTPUT); 
}


void loop() {

digitalWrite(led2, HIGH);   
  delay(1000);               
  digitalWrite(led2, LOW);    
  delay(1000); 





  digitalWrite(led, HIGH);   
  delay(1000);               
  digitalWrite(led, LOW);    
  delay(1000);               
   




}

I do not know how to improve

The IDE comes with a very simple but powerful example called "blink without delay" - play around with it.

int led = 13;
int led2 = 12;

void setup(void) 
{                
  pinMode(led, OUTPUT);     
  pinMode(led2, OUTPUT); 
}

void loop(void) 
{
  digitalWrite(led2, HIGH);   
  delay(1000);               
  digitalWrite(led2, LOW);    
  delay(1000); 

  digitalWrite(led, HIGH);   
  delay(1000);               
  digitalWrite(led, LOW);    
  delay(1000);               
}

ok I re-wrote your code to be more readable all it does is blink the two LEDs alternately

what you should do is use the "blink without delay" technology basically note the time then when 2 seconds has gone by do something when 5 seconds has gone by do something else

so

int led = 13;
int led2 = 12;

// timer for 2 second delay
unsigned long timer2;

// timer for 5 second delay
unsigned long timer5;

void setup(void) 
{                
  pinMode(led, OUTPUT);     
  pinMode(led2, OUTPUT);

  // initialise the two time counters 
  timer2 = millis();
  timer5 = timer2;
}

void loop(void) 
{
  unsigned long now;

  now = millis();

  if (now - timer2 > 2000)
  {
    // two seconds has elapsed, toggle the LED
    digitalWrite(led2, !digitalRead(led2));

    // save the new timer start
    timer2 = now;
  }
  if (now - timer5 > 5000)
  {
    // 5 seconds has elapsed, toggle the other LED
    digitalWrite(led, !digitalRead(led));

    // save the new timer start
    timer5 = now;
  }
}

Yes, now it works. More simply would not work.
How can regulate hours of sunshine a time of darkness.

sheep: Yes, now it works. More simply would not work. How can regulate hours of sunshine a time of darkness.

hee hee that's a lot harder to fix!

mmcp42:

sheep: Yes, now it works. More simply would not work. How can regulate hours of sunshine a time of darkness.

hee hee that's a lot harder to fix!

Not if you keep changing your longditude ;)