After just press keypad aduoino start 1cycle without signal

Good day.

I typed this code

  1. 44 keypad, 162 LCD I2C
  2. Keypad 1-9 setting delay time of pin 12
  3. Keypad A(1sec),B(2sec),C,D,*,#(6sec) setting 'on time 'of pin 12
  4. Ex) Press 1, Press A is delay 1sec + on 1sec setted.
  5. Must 2pin 5v signal receive cycle start.
  6. But but it started just keypad button, 1time without signal. Not even received 2pin signal.

Is there anyone know this reason?

#include <Keypad.h>
#include <Wire.h> // 원본코드에 추가 i2C 통신을 위한 라이브러리
#include <LiquidCrystal_I2C.h> // 원본코드에 추가 LCD 1602 I2C용 라이브러리
LiquidCrystal_I2C lcd(0x27,16,2); // 원본코드에 추가 접근주소: 0x3F or 0x27

const byte ROWS = 4; // Define ROWS on keypad
const byte COLS = 4; // Define COLS on keypad

char hexaKeys[ROWS][COLS] = { // Define each buttons of keypad
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[ROWS] = {11, 10, 9, 8}; //connect the row pinouts of the keypad to Arduino
byte colPins[COLS] = {7, 6, 5, 4}; //connect the column pinouts of the keypad to Arduino

// Generate instance base on keypad library
Keypad mykeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

#define led 13 // Working Indicator
#define relay 12 // Output to control the Valve System
#define button 2 // Interrupt Pin (Can add pulldown input switch)

int off_time =0;
int on_time = 0;
int keyValue = 0;
int numState = 0;
int charState = 0;
boolean interruptState = false;
int startState=0;

void setup(){
Serial.begin(9600);
pinMode(relay, OUTPUT);
pinMode(button, INPUT);
attachInterrupt(0, setInterrupt, RISING);
lcd.init(); // LCD 초기화
// Print a message to the LCD.
lcd.backlight(); // 백라이트 켜기
lcd.setCursor(0,0); // 1번째, 1라인
lcd.print(“JUWONSOLUTION”);
lcd.setCursor(0,1); // 1번째, 2라인
lcd.print(“010-XXX-XXXX”);
}

void loop(){
if(interruptState==true){
if(startState==1){
controlRelay();
}
}
else{
char key = mykeypad.getKey();
if((key>=‘0’ && key <=‘9’) || (key>=‘A’ && key<=‘D’) || (key==’’||key==’#’)){
if(key<=‘9’&&key>=‘0’){
keyValue = key-48; // change char to int
lcd.print(keyValue);
off_time = keyValue
1000+50; // off_time
// on_time = 3000; // Default Value, not used here
lcd.setCursor(0,0); //Check point1
lcd.print(“Delay (ms)=”);
lcd.print(off_time);
numState=1;
}

if((key>=‘A’ && key<=‘D’) || (key==’’||key==’#’)){
if(key==‘A’) on_time = 1000;
if(key==‘B’) on_time = 2000;
if(key==‘C’) on_time = 3000;
if(key==‘D’) on_time = 4000;
if(key==’#’) on_time = 5000;
if(key==’
’) on_time = 6000;

lcd.setCursor(0,1);
lcd.print(“Solvalv(ms)=”);
lcd.print(on_time);
charState = 1;
}

if(numState==1&&charState==1){
digitalWrite(relay,LOW);
// digitalWrite(led,HIGH);
delay(off_time);
digitalWrite(relay,HIGH);
delay(on_time);
digitalWrite(relay,LOW);
// digitalWrite(led,LOW);
numState=0;
charState=0;
startState=1;
}
}

else {
digitalWrite(relay, LOW);
// digitalWrite(led, LOW);
}
}
}

void setInterrupt(){
interruptState = true;
}

void controlRelay(){
lcd.setCursor(0,0);
lcd.print("Delaying = ");
lcd.print(off_time);
lcd.setCursor(0,1);
lcd.print(“Setted Sol =”);
lcd.print(on_time);

digitalWrite(relay,LOW);
digitalWrite(led,HIGH);
delay(off_time);
digitalWrite(relay,HIGH);
delay(on_time);
digitalWrite(relay,LOW);
digitalWrite(led,LOW);

numState=0;
charState=0;
interruptState = false;
lcd.setCursor(0,0);
lcd.print("Finished 1 Cycle ");
lcd.setCursor(0,1);
lcd.print(“Wait next signal”);
}

Please read the sticky post at the top of the forum and learn how to properly post your code using code tags. It helps people help you.

You do not need an interrupt for your code, just read the button. Also, how is your button wired up? The way your code is now requires an external pull-down resistor. Is one present?

#include <Keypad.h>
#include <Wire.h>                        // 원본코드에 추가 i2C 통신을 위한 라이브러리
#include <LiquidCrystal_I2C.h>        // 원본코드에 추가 LCD 1602 I2C용 라이브러리
LiquidCrystal_I2C lcd(0x27, 16, 2);   // 원본코드에 추가 접근주소: 0x3F or 0x27

const byte ROWS = 4;               // Define ROWS on keypad
const byte COLS = 4;               // Define COLS on keypad

char hexaKeys[ROWS][COLS] = {      // Define each buttons of keypad
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {11, 10, 9, 8}; //connect the row pinouts of the keypad to Arduino
byte colPins[COLS] = {7, 6, 5, 4};   //connect the column pinouts of the keypad to Arduino

// Generate instance base on keypad library
Keypad mykeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

#define led 13                     // Working Indicator
#define relay 12                   // Output to control the Valve System
#define button 2                   // Interrupt Pin (Can add pulldown input switch)
int previousButtonState;

int off_time = 0;
int on_time = 0;


void setup() {
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(button, INPUT);
  lcd.init();                      // LCD 초기화
  // Print a message to the LCD.
  lcd.backlight();                // 백라이트 켜기
  lcd.setCursor(0, 0);            // 1번째, 1라인
  lcd.print("JUWONSOLUTION");
  lcd.setCursor(0, 1);            // 1번째, 2라인
  lcd.print("010-XXX-XXXX");
  previousButtonState = digitalRead(button);
}

void loop() {
  char key = mykeypad.getKey();
  switch (key) {
    case '0'...'9':
      off_time = (key - '0') * 1000 + 50; // off_time
      break;

    case 'A':
      on_time = 1000;
      break;

    case 'B':
      on_time = 2000;
      break;
    case 'C':
      on_time = 3000;
      break;
    case 'D':
      on_time = 4000;
      break;
    case '#':
      on_time = 5000;
      break;
    case '*':
      on_time = 6000;
      break;
  }
  lcd.setCursor(0, 0); //Check point1
  lcd.print("Delay (ms)=");
  lcd.print(off_time); lcd.print("   ");
  lcd.setCursor(0, 1);
  lcd.print("Solvalv(ms)=");
  lcd.print(on_time); lcd.print("   ");

  int buttonState = digitalRead(button);
  if (buttonState != previousButtonState) {
    if ( buttonState == HIGH ) {
      // button pressed
      controlRelay();
    }
    delay(50);  // debounce
  }
  previousButtonState = buttonState;
}

void controlRelay() {
  lcd.setCursor(0, 0);
  lcd.print("Delaying = ");
  lcd.print(off_time);
  lcd.setCursor(0, 1);
  lcd.print("Setted Sol =");
  lcd.print(on_time);

  digitalWrite(relay, LOW);
  digitalWrite(led, HIGH);
  delay(off_time);
  digitalWrite(relay, HIGH);
  delay(on_time);
  digitalWrite(relay, LOW);
  digitalWrite(led, LOW);

  lcd.setCursor(0, 0);
  lcd.print("Finished 1 Cycle ");
  lcd.setCursor(0, 1);
  lcd.print("Wait next signal");
}

beginner83kr: Good day.

I typed this code

  1. 4*4 keypad, 16*2 LCD I2C
  2. Keypad 1-9 setting delay time of pin 12
  3. Keypad A(1sec),B(2sec),C,D,*,#(6sec) setting 'on time 'of pin 12
  4. Ex) Press 1, Press A is delay 1sec + on 1sec setted.
  5. Must 2pin 5v signal receive cycle start.
  6. But but it started just keypad button, 1time without signal. Not even received 2pin signal.

Is there anyone know this reason?

Just change this part:

      if (numState == 1 && charState == 1)
      {
        digitalWrite(relay, LOW);
        //    digitalWrite(led,HIGH);
        delay(off_time);
        digitalWrite(relay, HIGH);
        delay(on_time);
        digitalWrite(relay, LOW);
        //    digitalWrite(led,LOW);
        numState = 0;
        charState = 0;
        startState = 1;
      }

to this:

      if (numState == 1 && charState == 1)
      {
        numState = 0;
        charState = 0;
        startState = 1;
      }