counting problem while using ir sensor

Hello ,
i made a project in which it has to count the number of papers, So i have use ir sensor to detect the presence of paper.
every thing is going well but the problem is with the counting
actually it has to count only once when there is a signal from sensor
but what happening is arduino keeps on counting while the sensor is giving signal to the Arduino

i tried various loops and statement but there is no solution.

can anyone help me out in this problem?

here is the code below

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int s1, s2, s3, strt, stp, play, reset, pre, total, error,i,x=0;
void count(void);
void button(void);
void sensor(void);
void recount(void);
void setup()
{
pinMode(A1, INPUT); //SENSOR PANEL
pinMode(A2, INPUT); //SENSOR PANEL
pinMode(A5, INPUT); //feeding tray
pinMode(6, INPUT); //START
pinMode(7, INPUT); //stop
pinMode(8, INPUT); //play
pinMode(9, INPUT); //reset
pinMode(10, OUTPUT); //MOTOR signal
lcd.begin(16, 2);
lcd.clear();
lcd.print(“Insert Papers”);
delay(200);
Serial.begin(9600);
}

void loop()
{
buttons();
sensor();

}
void buttons()
{
strt = digitalRead(6);
stp = digitalRead(7);
play = digitalRead(8);
reset = digitalRead(9);
if (strt == 1)
error = 1;
if (stp == 1)
error = 2;
if (play == 1)
error = 3;
if (reset == 1)
error = 4;
}
void sensor()
{
s1 = digitalRead(A1);
s2 = digitalRead(A2);
s3 = digitalRead(A5);
if (error == 1)
{

if (s3 == 1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Processing…”);
delay(200);
digitalWrite(10, HIGH);
if(s2==1)
{
for(i=0;i<1;i++)
{
total=total+1;
break;
}
}

}
}
else if (s3 == 0)
{
delay(800);
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Insert Papers”);
lcd.setCursor(0, 1);
lcd.print(“Total=”);
delay(200);
lcd.setCursor(7, 1);
lcd.print(total);
delay(200);
Serial.println(total);
}
if (error == 2)
{
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“No. of Papers=”);
delay(200);
lcd.setCursor(0, 1);
lcd.print(total);
delay(500);
Serial.println(total);
}
if (error == 3)
{

if (s3 == 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Processing…”);
delay(200);
digitalWrite(10, HIGH);
count();
}
else if (s3 == 0)
{
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Insert Papers”);
delay(200);
lcd.setCursor(0, 1);
lcd.print(“Total=”);
delay(200);
lcd.setCursor(7, 1);
lcd.print(total);
delay(200);
}
}
if (error == 4)
{
digitalWrite(10,LOW);
recount();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Insert Papers”);
delay(200);
}
}

void recount()
{
pre = total;
total= 0;
}

Please read this and then re-post your code (get rid of the smilies) and we can discuss.

Thejesh:
Hello ,
i made a project in which it has to count the number of papers, So i have use ir sensor to detect the presence of paper.
every thing is going well but the problem is with the counting
actually it has to count only once when there is a signal from sensor
but what happening is arduino keeps on counting while the sensor is giving signal to the Arduino

i tried various loops and statement but there is no solution.

can anyone help me out in this problem?

here is the code below

You call the function 'count()' in your code but you don't define it.

(deleted)

DKWatson:
Please read this and then re-post your code (get rid of the smilies) and we can discuss.

i’m reposting the code

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int s1, s2, s3, strt, stp, play, reset, pre, total, error,i,x=0;
void count(void);
void button(void);
void sensor(void);
void recount(void);
void setup()
{
pinMode(A1, INPUT); //SENSOR PANEL
pinMode(A2, INPUT); //SENSOR PANEL
pinMode(A5, INPUT); //feeding tray
pinMode(6, INPUT); //START
pinMode(7, INPUT); //stop
pinMode(8, INPUT); //play
pinMode(9, INPUT); //reset
pinMode(10, OUTPUT); //MOTOR signal
lcd.begin(16, 2);
lcd.clear();
lcd.print(“Insert Papers”);
delay(200);
Serial.begin(9600);
}

void loop()
{
buttons();
sensor();

}
void buttons()
{
strt = digitalRead(6);
stp = digitalRead(7);
play = digitalRead(8);
reset = digitalRead(9);
if (strt == 1)
error = 1;
if (stp == 1)
error = 2;
if (play == 1)
error = 3;
if (reset == 1)
error = 4;
}
void sensor()
{
s1 = digitalRead(A1);
s2 = digitalRead(A2);
s3 = digitalRead(A5);
if (error == 1)
{

if (s3 == 1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Processing…”);
delay(200);
digitalWrite(10, HIGH);
count();

}
else if (s3 == 0)
{
delay(800);
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Insert Papers”);
lcd.setCursor(0, 1);
lcd.print(“Total=”);
delay(200);
lcd.setCursor(7, 1);
lcd.print(total);
delay(200);
Serial.println(total);
}
if (error == 2)
{
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“No. of Papers=”);
delay(200);
lcd.setCursor(0, 1);
lcd.print(total);
delay(500);
Serial.println(total);
}
if (error == 3)
{

if (s3 == 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Processing…”);
delay(200);
digitalWrite(10, HIGH);
count();
}
else if (s3 == 0)
{
digitalWrite(10, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Insert Papers”);
delay(200);
lcd.setCursor(0, 1);
lcd.print(“Total=”);
delay(200);
lcd.setCursor(7, 1);
lcd.print(total);
delay(200);
}
}
if (error == 4)
{
digitalWrite(10,LOW);
recount();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Insert Papers”);
delay(200);
}
}
}
void count()
{
if(s1==1)
{
total=total+1;
}
}
void recount()
{
pre = total;
total= 0;
}

johnwasser:
You call the function 'count()' in your code but you don't define it.

i defined it in the code which i posted for the second time...
still the problem pursues

Try this version. It should be functionally identical except the count() function. The changes in the count() function are an attempt to fix your counting problem.

#include <LiquidCrystal.h>

// Assign digital pins
const byte LCD_d7 = 2;
const byte LCD_d6 = 3;
const byte LCD_d5 = 4;
const byte LCD_d4 = 5;
const byte StartButtonPin = 6;
const byte StopButtonPin = 7;
const byte PlayButtonPin = 8;
const byte ResetButtonPin = 9;
const byte MotorPin = 10;
const byte LCD_en = 11;
const byte LCD_rs = 12;
// Assign analog pins
const byte PaperSensorPin = A1;
const byte OtherSensorPin = A2;
const byte FeedTrayPin = A5;

LiquidCrystal lcd(LCD_rs, LCD_en, LCD_d4, LCD_d5, LCD_d6, LCD_d7);

// Global variables
boolean PaperSensorState, PreviousPaperSensorState, OtherSensorState, FeedTrayState;
boolean StartButtonState, StopButtonState, PlayButtonState, ResetButtonState;

int PageCount;
int PreviousPageCount;

enum {START = 1, STOP, PLAY, RESET} LastButtonPressed = START;


void setup()
{
  pinMode(PaperSensorPin, INPUT); //SENSOR PANEL
  pinMode(OtherSensorPin, INPUT); //SENSOR PANEL
  pinMode(FeedTrayPin, INPUT); //feeding tray
  pinMode(StartButtonPin, INPUT); //START
  pinMode(StopButtonPin, INPUT); //stop
  pinMode(PlayButtonPin, INPUT); //PlayButtonState
  pinMode(ResetButtonPin, INPUT); //ResetButtonState
  pinMode(MotorPin, OUTPUT); //MOTOR signal
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("Insert Papers");
  delay(200);
  Serial.begin(9600);
}


void loop()
{
  buttons();
  sensor();
}


void buttons()
{
  StartButtonState = digitalRead(StartButtonPin);
  StopButtonState = digitalRead(StopButtonPin);
  PlayButtonState = digitalRead(PlayButtonPin);
  ResetButtonState = digitalRead(ResetButtonPin);
  if (StartButtonState)
    LastButtonPressed = START;
  if (StopButtonState)
    LastButtonPressed = STOP;
  if (PlayButtonState)
    LastButtonPressed = PLAY;
  if (ResetButtonState)
    LastButtonPressed = RESET;
}


void sensor()
{
  PaperSensorState = digitalRead(PaperSensorPin);
  OtherSensorState = digitalRead(OtherSensorPin);
  FeedTrayState = digitalRead(FeedTrayPin);

  switch (LastButtonPressed)
  {
    case START: StartFunction(); break;
    case STOP: StopFunction(); break;
    case PLAY: PlayFunction(); break;
    case RESET: ResetFunction(); break;
  }
}

void StartFunction()
{
  if (FeedTrayState)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Processing...");
    delay(200);
    digitalWrite(MotorPin, HIGH);
    count();
  }
  else
  {
    delay(800);
    digitalWrite(MotorPin, LOW);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Insert Papers");
    lcd.setCursor(0, 1);
    lcd.print("Total=");
    delay(200);
    lcd.setCursor(7, 1);
    lcd.print(PageCount);
    delay(200);
    Serial.println(PageCount);
  }
}


void StopFunction()
{
  digitalWrite(MotorPin, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("No. of Papers=");
  delay(200);
  lcd.setCursor(0, 1);
  lcd.print(PageCount);
  delay(500);
  Serial.println(PageCount);
}


void PlayFunction()
{
  if (FeedTrayState)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Processing...");
    delay(200);
    digitalWrite(MotorPin, HIGH);
    count();
  }
  else
  {
    digitalWrite(10, LOW);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Insert Papers");
    delay(200);
    lcd.setCursor(0, 1);
    lcd.print("Total=");
    delay(200);
    lcd.setCursor(7, 1);
    lcd.print(PageCount);
    delay(200);
  }
}

void ResetFunction()
{
  digitalWrite(MotorPin, LOW);
  recount();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Insert Papers");
  delay(200);
}

void count()
{
  // Count only transitions from OFF to ON
  if (PaperSensorState && !PreviousPaperSensorState)
    PageCount++;
  PreviousPaperSensorState = PaperSensorState;
}

void recount()
{
  PreviousPageCount = PageCount;
  PageCount = 0;
}