delay in input scanning

hello sir/guys,
iam new to this forum and arduino so please assist me even if my question is silly to you guys

Iam using arduino mega 2560 and i have configured 6 digital inputs, interfaced p10 display to display the temperature of a room.

My project is to set the upper and lower limit for the temperature and if the current temperature exceeds the defined limit then a configured digital output will enable the alarm. And i have used six digital inputs to increase/decrease,navigate through settings and current temperature display

my problem is, if i press a digital input controller takes much time to scan and i have to keep pressing atleast for a second to change the state of the input (i have configured my digital inputs with internal pullups enable).

what should i do, so that my digital inputs are scanned at once by the controller if i press my inputs.

i have configured my digital inputs with internal pullups enable.

please help me to solve this problem sir/guys

What you need to do really is show us the code (between code brackets, press</> in the format toolbar)

There is no reason why reading 2 temperatures or buttons would take any human noticeable time for what you want to do, so your code is the problem.

thanks for ur reply sir i will post my code here

#include <SPI.h>


#include <Timer.h>
Timer t;

//byte value;
int ledState = LOW;
const int WIDTH = 1;
const uint8_t *FONT = Arial14;
long previousMillis = 0;
long previousMillis1 = 0;// 
long interval = 10000;
long interval1 = 500;
int voltage0 = 00, voltage1 = 00;



#define in1 2
#define in2 3
#define in3 4
#define in4 22
#define in5 5
#define out1 24
int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorValue0 = 0, sensorValue1 = 0;
int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, valout;

int a, h, x, y, b, c, d, e, f, g, j, i, q = 0, l;

void setup() {
  Serial.begin(9600);
 
 
  pinMode(in1, INPUT_PULLUP);
  pinMode(in2, INPUT_PULLUP);
  pinMode(in3, INPUT_PULLUP);
  pinMode(in4, INPUT_PULLUP);
  pinMode(in5, INPUT_PULLUP);
  pinMode(out1, OUTPUT);

}

int incr(int x)
{
  h = x;
  h = h + 1;
  delay(100);
  if (d == 0 && g == 0)
  { hi = h;
    return hi;
  }
  if (d == 0 && g == 1)
  { lo = h;
    return lo;
  }
  if (d == 1 && g == 0)
  { hit = h;
    return hit;
  }
  if (d == 1 && g == 1)
  { lot = h;
    return lot;
  }
}
int decr(int y)
{
  h = y;
  h = h - 1;
  delay(100);
  if (d == 0 && g == 0)
  { hi = h;
    return hi;
  }
  if (d == 0 && g == 1)
  { lo = h;
    return lo;
  }
  if (d == 1 && g == 0)
  { hit = h;
    return hit;
  }
  if (d == 1 && g == 1)
  { lot = h;
    return lot;
  }
}

void loop()
{


  val1 = digitalRead(in1);
  val2 = digitalRead(in2);
  val3 = digitalRead(in3);
  val4 = digitalRead(in4);
  val5 = digitalRead(in5);
  sensorValue0 = analogRead(sensorPin0);
  Serial.print(sensorValue0);
  sensorValue0 *= 0.098;
 sensorValue0 += 3.5;


  sensorValue1 = analogRead(sensorPin1);
  sensorValue1 *= 0.098;
  sensorValue1 -= 37;
// sensorValue1 += 3;
  if (val1 == 0 && b == 0)
  { a = 1;
    delay(25);
  }
  if (val1 == 1 && a == 1)
  { b = 1;
    delay(25);
  }
  if (val1 == 0 && b == 1)
  {
    c = 1;
    delay(25);
  }
  if (val1 == 1 && c == 1)
  { a = b = c = 0;
    delay(25);
  }

  if (val2 == 0 && e == 0)
  { d = 1;
    delay(25);
  }
  if (val2 == 1 && d == 1)
  { e = 1;
    delay(25);
  }
  if (val2 == 0 && e == 1)
  {
    f = 1;
    delay(25);
  }
  if (val2 == 1 && f == 1)
  { d = e = f = 0;
    delay(25);
  }

  if (val3 == 0 && j == 0)
  { g = 1;
    delay(25);
  }
  if (val3 == 1 && g == 1)
  { j = 1;
    delay(25);
  }
  if (val3 == 0 && j == 1)
  {
    i = 1;
    delay(25);
  }
  if (val3 == 1 && i == 1)
  { g = j = i = 0;
    delay(25);
  }
  if (a == 0)
  { d = g = 0;
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        dmd.clearScreen();
        String stringOne1 =  String(sensorValue0, DEC);
        if (sensorValue0 <= 9 && sensorValue0 >= 0) {
          dmd.drawString( 1, 2, "H0", GRAPHICS_OR);
          dmd.drawString( 16, 2, "  ");
          dmd.drawString( 16, 2, stringOne1);
        }
        else
        { dmd.drawString( 0, 2, "H", GRAPHICS_OR);
          dmd.drawString( 8, 2, "  ");
          dmd.drawString( 8, 2, stringOne1);
        }
        dmd.selectFont(Droid_Sans_12);
        dmd.drawString( 23, 4, "%");
        dmd.selectFont(Arial14);
        Serial.print(sensorValue0);
        ledState = HIGH;
      }

      else {
        dmd.clearScreen();
        String stringOne2 =  String(sensorValue1, DEC);
        if (sensorValue1 <= 9 && sensorValue1 >= 0) {
          dmd.drawString( 2, 2, "T0", GRAPHICS_OR);
          dmd.drawString( 18, 2, "  ");
          dmd.drawString( 18, 2, stringOne2);
        }
        else
        { dmd.drawString( 2, 2, "T", GRAPHICS_OR);
          dmd.drawString( 10, 2, "  ");
          dmd.drawString( 10, 2, stringOne2);
        }
        //  dmd.drawString( 23, 0, ".", GRAPHICS_OR);
        dmd.drawString( 25, 2, "c");
        Serial.print(sensorValue1);
        ledState = LOW;
      }

    }

  }
  if (val1 == 0)
  {
    EEPROM.update(address0, hi);
    EEPROM.update(address1, hit);
    EEPROM.update(address2, lo);
    EEPROM.update(address3, lot);
    EEPROM.update(address4, lot);
    EEPROM.update(address5, lot);
  }


  if (a == 1 && d == 0 && g == 0)

  { // void drawLine(0, 0, 0, 32, GRAPHICS_NOR);
    unsigned long currentMillis1 = millis();

   
    String stringid =  String(hi, DEC);
    //dmd.clearScreen();
    if (hi <= 9 && hi >= 0) {
      dmd.drawString( 1, 2, "HH0");
      dmd.drawString( 24, 2, "  ");
      dmd.drawString( 24, 2, stringid, GRAPHICS_NOR);
    }
    else
    { dmd.drawString( 1, 2, "HH");
      dmd.drawString( 17, 2, "  ");
      dmd.drawString( 17, 2, stringid, GRAPHICS_NOR);
    }

    delay(250);

    if (val4 == 1)
    {
      incr(hi);
    }
    if (val5 == 1)
    {
      decr(hi);
    }
  }
  if (a == 1 && d == 0 && g == 1)
  { String stringid =  String(lo, DEC);
    if (lo <= 9 && lo >= 0) {
      dmd.drawString( 1, 2, "HL0");
      dmd.drawString( 24, 2, "  ");
      dmd.drawString( 24, 2, stringid);
    }
    else
    { dmd.drawString( 1, 2, "HL");
      dmd.drawString( 17, 2, "  ");
      dmd.drawString( 17, 2, stringid);
    }
    delay(250);

    if (val4 == 1)
    {
      incr(lo);
    }

    if (val5 == 1)
    {
      decr(lo);
    }
  }



  if (a == 1 && d == 1 && g == 0)
  { String stringid =  String(hit, DEC);
    if (hit <= 9 && hit >= 0) {
      dmd.drawString( 1, 2, "TH0");
      dmd.drawString( 24, 2, "  ");
      dmd.drawString( 24, 2, stringid);
    }
    else
    { dmd.drawString( 1, 2, "TH");
      dmd.drawString( 17, 2, "  ");
      dmd.drawString( 17, 2, stringid);
    }

    delay(250);

    if (val4 == 1)
    {
      incr(hit);
    }
    if (val5 == 1)
    {
      decr(hit);
    }
  }
  if (a == 1 && d == 1 && g == 1)
  { String stringid =  String(lot, DEC);
    if (lot <= 9 && lot >= 0) {
      dmd.drawString( 1, 2, "TL0");
      dmd.drawString( 24, 2, "  ");
      dmd.drawString( 24, 2, stringid);
    }
    else
    { dmd.drawString( 1, 2, "TL");
      dmd.drawString( 17, 2, "  ");
      dmd.drawString( 17, 2, stringid);
    }

    delay(250);
    if (val4 == 1)
    {
      incr(lot);
    }

    if (val5 == 1)
    {
      decr(lot);
    }
  }
  if ( sensorValue0 > hi || sensorValue0 < lo || sensorValue1 > hit || sensorValue1 < lot )
  { digitalWrite(out1, HIGH);

  }
  else
  { 
    digitalWrite(out1, LOW);
  }

}

sry for the rough use of the variables (etc a,b,c....)

below os the code to use a single input to both on and off used and declared some letters

 if (val1 == 0 && b == 0)
  { a = 1;
    delay(25);
  }
  if (val1 == 1 && a == 1)
  { b = 1;
    delay(25);
  }
  if (val1 == 0 && b == 1)
  {
    c = 1;
    delay(25);
  }
  if (val1 == 1 && c == 1)
  { a = b = c = 0;
    delay(25);
  }

  if (val2 == 0 && e == 0)
  { d = 1;
    delay(25);
  }
  if (val2 == 1 && d == 1)
  { e = 1;
    delay(25);
  }
  if (val2 == 0 && e == 1)
  {
    f = 1;
    delay(25);
  }
  if (val2 == 1 && f == 1)
  { d = e = f = 0;
    delay(25);
  }

  if (val3 == 0 && j == 0)
  { g = 1;
    delay(25);
  }
  if (val3 == 1 && g == 1)
  { j = 1;
    delay(25);
  }
  if (val3 == 0 && j == 1)
  {
    i = 1;
    delay(25);
  }
  if (val3 == 1 && i == 1)
  { g = j = i = 0;
    delay(25);
  }

please assist me with the codes

You are using millis() to manage the timing in one place. You need to use that everywhere and never use the delay() function if you want a responsive program.

IMHO you have far too much of your code in the loop() function. Have a look at how Planning and Implementing a Program puts the code in lots of short single-purpose functions that can each be tested on their own.

Using meaningful variable names will also be a big help for you as well as for us.

This line

sensorValue1 *= 0.098;

probably gives 0 as an answer because sensorValue1 is an int rather than a float. And try to avoid using float variables because they make the code slow.

...R

Thanks for ur reply sir
i still have some doubts in configuring digital inputs.....

can i use an timer interupt to check the input status every 50ms or 60 or any ...
if i do will it affect the efficiency of my program since my controller is checking for the inputs more often. if it is so what shall be the solution or the perfect solution to scan the inputs in real time without delay

And about the hardware part should i connect the capacitor to reduce the debounce... (if i use the internal pullups i think they are not required).

should i use the debouce delay in the program (is that really required )

and finally i need simple description about the efficient way of programming more number of digital inputs which can scan them in real time without a delay

if possible can i get the example program of them :slight_smile:

thank in advance for ur help

muralid:
...
and finally i need simple description about the efficient way of programming more number of digital inputs which can scan them in real time without a delay

if possible can i get the example program of them :slight_smile:
...

Look at this: Demonstration code for several things at the same time