Arduino did not read serial signal.[HELP]

hello.
Im using arduino mega connect to BV20 (bill acceptor device) using serial mode.

The problem is when the device send serial signal=3 arduino can read. BUT when the device send serial signal=1 arduino cant read.

this is my program. can anyone help me out with this. I'm having an horrendous time trying to figure this out.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup(){

  Serial.begin(9600);
  pinMode(42,INPUT);
  pinMode(41,INPUT);
  pinMode(29,OUTPUT);
  pinMode(40,INPUT);
  pinMode(50,OUTPUT); 
  pinMode(51,OUTPUT);
  lcd.begin(16,2);
  lcd.print("SELAMAT DATANG");
  delay(2000);
  lcd.clear(); 
  lcd.setCursor(0,0);
  lcd.print("SILA MASUKAN");
  lcd.setCursor(0,1);
  lcd.print("WANG ANDA");
  delay(2000);

}
void(* resetFunc) (void) = 0;//declare reset function @ address 0  





void loop()
{
  while(Serial.read()==3)
  {
    digitalWrite(50,HIGH);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("WANG DITERIMA");
    lcd.setCursor(0,1);
    lcd.print("RM5.00");
    delay(500);
    digitalWrite(50,LOW);
    delay(2000);
    lcd.clear();
    lcd.print("TUNGGU SEBENTAR");
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SYILING ANDA");
    lcd.setCursor(0,1);
    lcd.print("SEDANG DIPROSES");
    delay(3000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("TERIMA KASIH");
    lcd.setCursor(0,1);
    lcd.print("JUMPA LAGI");
    delay(2000);
    resetFunc();//call reset
    {
      while (Serial.read()==1)
      {
        digitalWrite(51,HIGH);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("WANG DITERIMA");
        lcd.setCursor(0,1);
        lcd.print("RM 1.00");
        delay(500);
        digitalWrite(51,LOW);
        delay(2000);
        lcd.clear();
        lcd.print("TUNGGU SEBENTAR"); 
        delay(1000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("SYILING ANDA");
        lcd.setCursor(0,1);
        lcd.print("SEDANG DIPROSES");   
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("TERIMA KASIH");
        lcd.setCursor(0,1);
        lcd.print("JUMPA LAGI");
        delay(2000);
        resetFunc();  //call reset
      }
    }
  }
}
 while(Serial.read()==3)

has

 while(Serial.read()==1)

nested in it. Both can't be true.

The way that you are reading the serial input is clumsy at best. Have a look at Serial input basics

delay(2000);
    resetFunc();//call reset

.?

If this is a serious project (rather than just a quick-and-dirty demo) you should read your serial data properly - for example using one of the examples in serial input basics.

And you should manage your timing using millis() rather than delay() as illustrated in several things at a time.

It would also be a good idea to break your code into small single-purpose functions that have meaningful names and can be tested on their own, separately from the rest of the project.

...R

Robin2:
If this is a serious project (rather than just a quick-and-dirty demo) you should read your serial data properly - for example using one of the examples in serial input basics.

And you should manage your timing using millis() rather than delay() as illustrated in several things at a time.

It would also be a good idea to break your code into small single-purpose functions that have meaningful names and can be tested on their own, separately from the rest of the project.

UKHeliBob:

 while(Serial.read()==3)

has

 while(Serial.read()==1)

nested in it. Both can't be true.

The way that you are reading the serial input is clumsy at best. Have a look at Serial input basics

Whoa im a beginner/noob using arduino. i just knew basics of programming. simpler a explaination maybe ?

AWOL:

delay(2000);

resetFunc();//call reset



.?

after delay, the program reset back to void setup()

Whoa im a beginner/noob using arduino. i just knew basics of programming. simpler a explaination maybe ?

Let's try an example that is not a program

while today is Wednesday
{
  shout "Today is Wednesday"
  
    while today is Friday
    {
      shout "Today is Friday"
    }
}

Look closely. The outer while loop will only be true when today is Wednesday so how could today ever be Friday whilst it is Wednesday ?

if today is Wednesday
{
  shout "Today is Wednesday"
}
else if today is Friday
{
  shout "Today is Friday"
}

would allow for today to be either Wednesday or Friday

UKHeliBob:
Let's try an example that is not a program

while today is Wednesday

{
  shout "Today is Wednesday"
 
    while today is Friday
    {
      shout "Today is Friday"
    }
}



Look closely. The outer while loop will only be true when today is Wednesday so how could today ever be Friday whilst it is Wednesday ?


if today is Wednesday
{
  shout "Today is Wednesday"
}
else if today is Friday
{
  shout "Today is Friday"
}


would allow for today to be either Wednesday or Friday

Thanks !!
It's work.
But arduino not read the signal=1 from the device properly. Sometimes it reads the signal, sometimes not.
O.o

Do you have any idea to overcome this problem ?

Thanks again bob !

Do you have any idea to overcome this problem ?

Without seeing your code?
Probably not.

Do you have any idea to overcome this problem ?

Reading the serial input only when a byte is available would be a start.

UKHeliBob:
Reading the serial input only when a byte is available would be a start.

I have seen many example of using byte...
i not really understand the code..
can you give me simple example to use it.

Why is ByteReceived declared as an int ?
What do you see if you print ByteReceived ?


int ByteReceived;

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

}
//--(end setup )---

void loop()
{
if (Serial.available() > 0)
{
ByteReceived = Serial.read();

if(char(ByteReceived) == '1') // Single Quote! This is a character.
{
digitalWrite(led,HIGH);
}

if(char(ByteReceived) == '0')
{
digitalWrite(led,LOW);
}
}
}

i had try apply this to my program but it did not work

Missing pieces;
byte led = 13; // onboard led

pinMode (led, OUTPUT);

UKHeliBob:
Why is ByteReceived declared as an int ?
What do you see if you print ByteReceived ?

the truth is i also don't understand how it work actually.

danialsudirman:
Whoa im a beginner/noob using arduino. i just knew basics of programming. simpler a explaination maybe ?

Have a look at planning and implementing a program

It's a good idea to develop good habits right at the start.

...R