I want to optimize my program

Hello i have a multiple plants watering robot with some sensors but the only problem is that the program is kinda slow so i just wanna know if i can optimise it.I am new to arduino so i dont know exactly what i can improve and what i cant so i wanted to put this question so i will not ruin my program.
Heres the program:

#include <Adafruit_NeoPixel.h>
#include <LcdProgressBar.h>
#include "LiquidCrystal.h"
#include <avr/power.h>
#include <dht.h>

dht DHT;
byte lcdNumCols = 16;

#define DHT11_PIN 2
int trans = 13;
int buzzerPin = 3;
int flame = 12;
int FLAME = HIGH;
int PIN = 6;
int NUMPIXELS = 8;
int vbr = 0;
int rel = 11;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
LcdProgressBar lpg(&lcd, 1, lcdNumCols);

unsigned long duration = 4000;  // 2000 milliseconds, 2 seconds
unsigned long startedMillis = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(2, lcdNumCols);
  lcd.setCursor(0, 0);
  lcd.print("Welcome!");
  delay(100);
  initLpg();
  pinMode(buzzerPin, OUTPUT);
  pinMode(vbr, INPUT);
  pinMode(flame, INPUT);
  pinMode(trans, INPUT);
  pinMode(rel, OUTPUT);
  digitalWrite(rel, HIGH);
  pixels.begin();
  pixels.setBrightness(50);
}


void initLpg() {  //sectiunea loading screen
  startedMillis = millis();
  lpg.setMinValue(startedMillis);
  lpg.setMaxValue(startedMillis + duration);
  lpg.draw(startedMillis);
}
int nr = 40, umed;
void loop() {
  digitalWrite(buzzerPin, HIGH);
  digitalWrite(rel, HIGH);


  if (nr > 0) {
    while (nr > 0) {
      unsigned long currentMillis = millis();
      lpg.draw(currentMillis);
      delay(100);
      nr--;
    }
  }


  umed = analogRead(A1);
  int umedsh = 800 - umed;
  Serial.println(umedsh);
  if (umedsh < 450 && digitalRead(trans) == HIGH) {
    digitalWrite(rel, LOW);
    delay(2000);
    digitalWrite(rel, HIGH);
  }
  //sectiunea senzor temp si umiditate

  int chk = DHT.read11(DHT11_PIN);
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(DHT.temperature);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity: ");
  lcd.print(DHT.humidity);
  lcd.print("%");


  //sectiunea senzor flacara

  FLAME = digitalRead(flame);
  pixels.clear();
  if (FLAME == LOW) {
    digitalWrite(buzzerPin, LOW);
    pixels.setPixelColor(0, pixels.Color(255, 0, 0));
    pixels.setPixelColor(1, pixels.Color(255, 0, 0));
    pixels.setPixelColor(2, pixels.Color(255, 0, 0));
    pixels.setPixelColor(3, pixels.Color(255, 0, 0));
    pixels.setPixelColor(4, pixels.Color(255, 0, 0));
    pixels.setPixelColor(5, pixels.Color(255, 0, 0));
    pixels.setPixelColor(6, pixels.Color(255, 0, 0));
    pixels.setPixelColor(7, pixels.Color(255, 0, 0));
    //pixels.show();
  } else {
    digitalWrite(buzzerPin, HIGH);
    pixels.setPixelColor(0, pixels.Color(0, 255, 0));
    pixels.setPixelColor(1, pixels.Color(0, 255, 0));
    pixels.setPixelColor(2, pixels.Color(0, 255, 0));
    pixels.setPixelColor(3, pixels.Color(0, 255, 0));
    pixels.setPixelColor(4, pixels.Color(0, 255, 0));
    pixels.setPixelColor(5, pixels.Color(0, 255, 0));
    pixels.setPixelColor(6, pixels.Color(0, 255, 0));
    pixels.setPixelColor(7, pixels.Color(0, 255, 0));
  }
  pixels.show();


  //sectiunea senzor vibratie
  long VBR = pulseIn(vbr, HIGH);
  if (digitalRead(vbr) == HIGH && digitalRead(trans) == HIGH) {
    //digitalWrite(buzzerPin,LOW);
    pixels.setPixelColor(0, pixels.Color(0, 0, 255));
    pixels.setPixelColor(1, pixels.Color(0, 0, 255));
    pixels.setPixelColor(2, pixels.Color(0, 0, 255));
    pixels.setPixelColor(3, pixels.Color(0, 0, 255));
    pixels.setPixelColor(4, pixels.Color(0, 0, 255));
    pixels.setPixelColor(5, pixels.Color(0, 0, 255));
    pixels.setPixelColor(6, pixels.Color(0, 0, 255));
    pixels.setPixelColor(7, pixels.Color(0, 0, 255));
    pixels.show();

    delay(2000);
  } else {
    digitalWrite(buzzerPin, HIGH);
    pixels.setPixelColor(0, pixels.Color(0, 255, 0));
    pixels.setPixelColor(1, pixels.Color(0, 255, 0));
    pixels.setPixelColor(2, pixels.Color(0, 255, 0));
    pixels.setPixelColor(3, pixels.Color(0, 255, 0));
    pixels.setPixelColor(4, pixels.Color(0, 255, 0));
    pixels.setPixelColor(5, pixels.Color(0, 255, 0));
    pixels.setPixelColor(6, pixels.Color(0, 255, 0));
    pixels.setPixelColor(7, pixels.Color(0, 255, 0));
  }
  digitalWrite(buzzerPin, HIGH);
  pixels.show();
}

For one thing, the indenting is atrocious. Please hit ctrl-t (or Tools --> Auto Format) in the Arduino IDE. Then repost it. That will make it much more readable and encourage people to look at it.

1 Like

Why does a plant watering system have to be fast ?

As to the speed of your sketch, maybe the delay()s in it are not helping

sorry,forgot to mention that i have some sensors with a neopixel led bar and the leds original color are green when is fire is red and there is a problem that the when the fire sensor is active but the led bar would cycle between green and red

This is called too often, in fact on every execution of loop() even if the neopixels have not been updated. The Arduino is probably spending more time doing this compared to everything else.

  pixels.setPixelColor(0, pixels.Color(0,255,0));
  pixels.setPixelColor(1, pixels.Color(0,255,0));
  pixels.setPixelColor(2, pixels.Color(0,255,0));
  pixels.setPixelColor(3, pixels.Color(0,255,0));
  pixels.setPixelColor(4, pixels.Color(0,255,0));
  pixels.setPixelColor(5, pixels.Color(0,255,0));
  pixels.setPixelColor(6, pixels.Color(0,255,0));
  pixels.setPixelColor(7, pixels.Color(0,255,0));

This could be replaced with a for loop. That wouldn't make it run faster, but it would be quicker to read!

int vbr = 0;
...
long VBR = pulseIn(vbr, HIGH);

This could be waiting for a long time for a pulse, depending what is connected to pin 0. Up to 1 second, in fact. On most Arduino, pin 0 is used for serial monitor. And then, the result of pulseIn() is not used, I think.

yeah,i know that pin 0 is used for serial but the single available pin that i have left is pin 1 and that long VBR = pulseIn(vbr, HIGH); was some leftover for the vibration sensor from other tutorial

Have you used all of the A* pins ?

should pin 1 be faster?

yup cause i have an lcd shield

I suggest you review your code carefully and remove any other "left-over" code. If you still think it's slow, post the updated version here.

Faster than what?

than pin 0

No, just the same. What kind of Arduino are you using?

arduino uno from plusivo

Then you cannot use pins 0 or 1.

What about pin 10?

pin 10 is taken by lcd shield and pin 0 is working with the vibration sensor

You cannot use pin 0 on Uno. Do you understand?

If pin 10 is used, why it is not listed in your code?

Please list what each pin is being used for or better still post a schematic of your project

the pin 10 is used for backlight control

d0 - vibration sensor
d1- nothing
d2- dht11 sensor
d3- buzzer
d4-lcd shield
d5-lcd shield
d6-lcd shield and neopixel bar
d7-lcd shield
d8-lcd shield
d9-lcd shield
d10-lcd shield
d11-relay pin
d12-flame sensor pin
d13-another arduino board for high/low transmission