I can’t find the error in using if

I’m using arduino and relay module to change state of sensor(ACS712).

I’ll send value 0 and 1. If send 0 value, state data become 0 and relay stop the module. If send 1 value, state data become 1 and relay start the module.

when I run the program and enter value 0, it works good. But if I enter value 1, data changes 10 and not change to 0 even if I enter 0 value. Where is the error…? I cant find it. This is my code.

`void loop(){
Serial.println(data);
if(data=='1') {
start_time = millis();
digitalWrite(Relay,HIGH);
now_time = millis();
if(Serial.available()>0) Serial.read();
while(now_time-start_time<1000) {
now_time = millis();
if(Serial.available()>0) {
data = Serial.read();
if(data=='0') {
Serial.println("You input the OFF");
digitalWrite(Relay,LOW);
return ;
}
}
}
value = analogRead(cur_pin);
amp = abs(((value-511)5/0.185)/1024);
Serial.print(amp);
Serial.println("A");
Serial.print("P : ");
Serial.println(220
amp);
bluetooth.print("P : ");
bluetooth.println(220amp);
bluetooth.println(" ");
start_time = now_time;
if(ptr==SIZE) ptr = 0;
store[ptr++] = 220
amp;
bluetooth.print("S : ");
for(int i=0;i<ptr;i++) {
bluetooth.print(store[i]);
bluetooth.print(" ");
}
bluetooth.println();
while(Serial.available()>0) Serial.read();
}


void loop(){
Serial.println(data);
if(data=='1') {
  start_time = millis();
    digitalWrite(Relay,HIGH);
    now_time = millis();
    if(Serial.available()>0) Serial.read();
    while(now_time-start_time<1000) {
      now_time = millis();
      if(Serial.available()>0) {
        data = Serial.read();
        if(data=='0') {
          Serial.println("You input the OFF");
          digitalWrite(Relay,LOW);
          return ;
        }
      }
    }
    value = analogRead(cur_pin);    
    amp = abs(((value-511)*5/0.185)/1024);
    Serial.print(amp);
    Serial.println("A");
    Serial.print("P : ");
    Serial.println(220*amp);
    bluetooth.print("P : ");       
    bluetooth.println(220*amp);
    bluetooth.println(" ");
    start_time = now_time;       
    if(ptr==SIZE) ptr = 0;
    store[ptr++] = 220*amp;
    bluetooth.print("S : ");
    for(int i=0;i<ptr;i++) {
      bluetooth.print(store[i]);
      bluetooth.print(" ");
    }
    bluetooth.println();
    while(Serial.available()>0) Serial.read();
    }


if(Serial.available()>0) {
  data = Serial.read();
  
  if(data == '0') {
    Serial.println("LOW");
    digitalWrite(Relay, LOW);
  }else if(data=='1') {
    start_time = millis();
    digitalWrite(Relay,HIGH);
    now_time = millis();
    if(Serial.available()>0) Serial.read();
    while(now_time-start_time<1000) {
      now_time = millis();
      if(Serial.available()>0) {
        data = Serial.read();
        if(data=='0') {
          Serial.println("You input the OFF");
          digitalWrite(Relay,LOW);
          return ;
        }
      }
      now_time = millis();
    }
    value = analogRead(cur_pin);   
    amp = abs(((value-511)*5/0.185)/1024);
    Serial.print(amp);
    Serial.println("A");
    Serial.print("P : ");
    Serial.println(220*amp);
    bluetooth.print("P : ");      
    bluetooth.println(220*amp);
    bluetooth.println(" ");
    start_time = now_time;     
    if(ptr==SIZE) ptr = 0;
    store[ptr++] = 220*amp;
    bluetooth.print("S : ");
    for(int i=0;i<ptr;i++) {
      bluetooth.print(store[i]);
      bluetooth.print(" ");
    }
    bluetooth.println();
    while(Serial.available()>0) Serial.read();
    }
}

delay(100);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Please post complete code; I seem to be missing setup() and possibly other things.

also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac

Your code is truly horrible.

What are you trying to do other than turn a relay on / off?

What is the 1 second timer for?

What is the analog input you are reading, and what are you trying to do with it?

a more conventional approach would just check for and read any serial input once. your code reads it in 7 places

consider

const byte pinRelay = LED_BUILTIN;
const byte pinCur   = A0;
bool       active;

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------
void
loop (void)
{
    if (Serial.available ())  {
        char c = Serial.read ();
        if ('0' == c)  {
            active = false;
            digitalWrite (pinRelay, Off);
        }
        else if ('1' == c)  {
            active = true;
            digitalWrite (pinRelay, On);
        }
    }

    if (active)  {
        int value = analogRead (pinCur);
        int amp   = abs(((value-511)*5/0.185)/1024);

        Serial.print   (" value ");
        Serial.print   (value);
        Serial.print   (", A ");
        Serial.print   (amp, 4);
        Serial.print   (", P ");
        Serial.println (220*amp);

        delay (1000);           // report every second
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    digitalWrite (pinRelay, Off);
    pinMode      (pinRelay, OUTPUT);
}