How to run double void loop in one Arduino code?

i have no idea
i want use dht sensor and ambilight at same time in 1 arduino nano

I am trying run this not working, if i write dht code first then ambilight doesn't work and same if i write ambilght first then dht doesn't work too
this is for example

#include <Adafruit_NeoPixel.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
LiquidCrystal_I2C lcd(0x27, 16, 4); 
#define DHTPIN 2                   
#define DHTTYPE DHT11
#define pwm 3
#define LED_PIN 13
#define NUM_LEDS 8
#define serialRate 115200
DHT dht(DHTPIN, DHTTYPE);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, LED_PIN, NEO_GRB + NEO_KHZ800);
#include "FastLED.h"
#define NUM_LED 96
#define DATA_PIN 6
#define serialRate 115200
uint8_t prefix[] = { 'A', 'd', 'a' }, hi, lo, chk, i;
CRGB leds[NUM_LED];


void setup() 
{
  strip.begin();
  strip.show();
  analogWrite(pwm, 255);
  Serial.begin(9600);
  dht.begin();
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LED);

  LEDS.showColor(CRGB(255, 0, 0));
  delay(500);
  LEDS.showColor(CRGB(0, 255, 0));
  delay(500);
  LEDS.showColor(CRGB(0, 0, 255));
  delay(500);
  LEDS.showColor(CRGB(0, 0, 0));

  Serial.begin(serialRate);
  Serial.print("Ada\n");


  strip.begin();
  strip.show();
  lcd.begin(16, 2);
  lcd.backlight();
  delay(1000);
}

void loop() {
//DHT11
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) {
    lcd.setCursor(8, 0);
    lcd.print("Croot   ");
    lcd.setCursor(10, 1);
    lcd.print("Croot   ");
    return;
  }
  lcd.setCursor(0, 1);
  lcd.write(1);
  lcd.print(t);
  lcd.print("C");
  delay(500);
  lcd.setCursor(8, 1);
  lcd.print(h, 0);
  lcd.print("%");
  delay(500);
  lcd.setCursor(12, 1);
  if (t < 35.5) 
  {
    analogWrite(pwm, 0);
    lcd.print("0%  ");
    //fadeAmount = -fadeAmount;
    strip.setBrightness(50);
    for (int i = 0; i < 20; i++) {  //biru
      colorWipe(strip.Color(14, 35, 180), 50);
      colorWipe(strip.Color(16, 37, 185), 50);
      colorWipe(strip.Color(18, 39, 190), 50);
      colorWipe(strip.Color(20, 41, 195), 50);
      colorWipe(strip.Color(18, 39, 190), 50);
      colorWipe(strip.Color(16, 37, 185), 50);
      strip.show();
    }
    delay(1000);
  } else if (t == 36) {
    analogWrite(pwm, 25);
    lcd.print("10% ");
    strip.setBrightness(50);
    for (int i = 0; i < 20; i++) {  //oren
      colorWipe(strip.Color(255, 241, 235), 50);
      colorWipe(strip.Color(255, 238, 207), 50);
      colorWipe(strip.Color(253, 228, 169), 50);
      colorWipe(strip.Color(20, 41, 195), 50);
      colorWipe(strip.Color(255, 197, 125), 50);
      colorWipe(strip.Color(255, 183, 93), 50);
      colorWipe(strip.Color(255, 197, 125), 50);
      colorWipe(strip.Color(20, 41, 195), 50);
      colorWipe(strip.Color(253, 228, 169), 50);
      colorWipe(strip.Color(255, 238, 207), 50);
      strip.show();
    }
    delay(1000);
  } else if (t == 36.5) {
    analogWrite(pwm, 51);
    lcd.print("20% ");
    strip.setBrightness(50);
    for (int i = 0; i < 20; i++) {  //merah
      colorWipe(strip.Color(255, 236, 236), 50);
      colorWipe(strip.Color(255, 214, 214), 50);
      colorWipe(strip.Color(255, 186, 186), 50);
      colorWipe(strip.Color(255, 162, 162), 50);
      colorWipe(strip.Color(255, 152, 152), 50);
      colorWipe(strip.Color(255, 138, 138), 50);
      colorWipe(strip.Color(255, 152, 152), 50);
      colorWipe(strip.Color(255, 162, 162), 50);
      colorWipe(strip.Color(255, 186, 186), 50);
      colorWipe(strip.Color(255, 214, 214), 50);
      strip.show();
    }
    delay(1000);
  }
  delay(3000);
//dht code

  //AMBATULIGHT
  for (i = 0; i < sizeof prefix; ++i) {
waitLoop:
    while (!Serial.available())
      ;
    ;
    if (prefix[i] == Serial.read()) continue;
    // otherwise, start over
    i = 0;
    goto waitLoop;
  }

    while (!Serial.available())
    ;
  ;
  hi = Serial.read();
  while (!Serial.available())
    ;
  ;
  lo = Serial.read();
  while (!Serial.available())
    ;
  ;
  chk = Serial.read();

  if (chk != (hi ^ lo ^ 0x55)) {
    i = 0;
    goto waitLoop;
  }

  memset(leds, 0, NUM_LED * sizeof(struct CRGB));
  // Read the transmission data and set LED values
  for (uint8_t i = 0; i < NUM_LED; i++) {
    byte r, g, b;
    while (!Serial.available())
      ;
    r = Serial.read();
    while (!Serial.available())
      ;
    g = Serial.read();
    while (!Serial.available())
      ;
    b = Serial.read();
    leds[i].r = r;
    leds[i].g = g;
    leds[i].b = b;
  }
  FastLED.show();
}

void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
//ambilight code
}

Your code is written in blocking mode. You can't use delays if you want to do more than one thing in time.
You have to get rid all delay() statements, blocking while and for loops and replace it with millis(0 code. See the BlinkWithoutDelay example in the IDE

Hello

Welcome to the worldbest Arduino forum ever.

If you expect a realtime behaiviour of the program you have to get rid of the while() and delay() function. The delay() function blocks the execution.

	Line  31:   delay(500);
	Line  33:   delay(500);
	Line  35:   delay(500);
	Line  46:   delay(1000);
	Line  65:   delay(500);
	Line  69:   delay(500);
	Line  86:     delay(1000);
	Line 104:     delay(1000);
	Line 122:     delay(1000);
	Line 124:   delay(3000);
	Line 180:     delay(wait);

	Line 129:     while (!Serial.available())
	Line 138:     while (!Serial.available())
	Line 142:   while (!Serial.available())
	Line 146:   while (!Serial.available())
	Line 160:     while (!Serial.available())
	Line 163:     while (!Serial.available())
	Line 166:     while (!Serial.available())

Design and code your own none.blocking timer function by using the BlinkWithoutOut example of the IDE.

Have a nice day and enjoy coding in C++.

Are you trying to run them sequentially or concurrently?

You can create any number of custom loops like this:

void loop1() {
...
}
void loop2() {
...
}

void loop() {
  loop1();
  loop2();
...
}

But they will only "loop" transparently (as if there were no other loops) if you allow them to run with no blocking routines in either one. Post #3 shows that your code is chock full of those. You will have to redesign the sketch, almost from scratch.

Hi @jouchoyopyu,

just some further hints:

The led effects are programmed in a way that takes much more time than you probably may expect:

Everytime this routine is called

void colorWipe(uint32_t c, uint16_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

it consumes roughly strip.numPixels() x wait [ms] time before the controller can get to the next steps.

In your case numPixels() is 8 and wait is 50 ms -> One pass through colorWipe() therefore takes about 8 x 50 ms = 0,4 s (just ignoring the time the other functions like strip.show() etc. require).

In //merah colorWipe() is called 10 times in a row within the for loop and the whole block is repeated 20 times:

    for (int i = 0; i < 20; i++) {  //merah
      colorWipe(strip.Color(255, 236, 236), 50);
      colorWipe(strip.Color(255, 214, 214), 50);
      colorWipe(strip.Color(255, 186, 186), 50);
      colorWipe(strip.Color(255, 162, 162), 50);
      colorWipe(strip.Color(255, 152, 152), 50);
      colorWipe(strip.Color(255, 138, 138), 50);
      colorWipe(strip.Color(255, 152, 152), 50);
      colorWipe(strip.Color(255, 162, 162), 50);
      colorWipe(strip.Color(255, 186, 186), 50);
      colorWipe(strip.Color(255, 214, 214), 50);
    }

The block inside the for loop takes 10 x 0,4 s = 4 s.
The block is repeated 20 times: 20 x 4 s = 80 s.

So everytime your sketch runs into one of these for loops, your controller is occupied for 80 s.

The parts that handle serial input to control the NUM_LED leds is completely blocking if no data are available. Not to mention that it uses the "goto" statement which should be avoided if ever possible:

Serial Part
  //AMBATULIGHT
  for (i = 0; i < sizeof prefix; ++i) {
waitLoop:
    while (!Serial.available())
      ;
    ;
    if (prefix[i] == Serial.read()) continue;
    // otherwise, start over
    i = 0;
    goto waitLoop;
  }

    while (!Serial.available())
    ;
  ;
  hi = Serial.read();
  while (!Serial.available())
    ;
  ;
  lo = Serial.read();
  while (!Serial.available())
    ;
  ;
  chk = Serial.read();

  if (chk != (hi ^ lo ^ 0x55)) {
    i = 0;
    goto waitLoop;
  }

  memset(leds, 0, NUM_LED * sizeof(struct CRGB));
  // Read the transmission data and set LED values
  for (uint8_t i = 0; i < NUM_LED; i++) {
    byte r, g, b;
    while (!Serial.available())
      ;
    r = Serial.read();
    while (!Serial.available())
      ;
    g = Serial.read();
    while (!Serial.available())
      ;
    b = Serial.read();
    leds[i].r = r;
    leds[i].g = g;
    leds[i].b = b;
  }
  FastLED.show();
}

The unpleasant truth is that the structure of the sketch does not allow a reasonably quick response to a temperature change. [Edit: Unless you reduce the number of repetitions, e.g. to just 1 time and change the serial part of course...].

As mentioned by other members already the best way is to rewrite the whole sketch.

As a first step I would recommend to describe what you expect the application to perform.

Good luck!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.