[Solved]Coin acceptor programming

Hello guys.I bought a coin acceptor which recognizes only one coin and bought an ardunio uno.I want to read pulse from coin acceptor , then i want to send message to pc's serial port with ardunio.I wrote a program for pc.This program reads serial port,and when it receives signal , computer is usable.

I connected coin acceptor to arduno's interrupt pin 2 with pull up resistor.I used function attachinterrupt.Interrupt works fine.Everytime i put coin , credit increases1.But i want to send signal to computers serial port that coin inserted.When i send message with Serial.printl() function , my interrupt function is triggered.So credit increases again.In loop function i tried to capture 1 pulse , and ignore pulses more then one.This also worked fine.But sometimes , credit increases after 1 hour work.Can you help me pls...

int credit=0;
int counter1=0;
int counter2=0;
unsigned long oldTime=0;
unsigned long newTime=0;
void setup() {
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(2),pulse,RISING);
}

void pulse()
{
counter1=counter1+1;
}

void loop() {

newTime=millis();
if(newTime-oldTime>1000)
{
if(counter1-counter2>1)
{
counter1=0;
counter2=0;
};
if(counter1-counter2==1)
{
credit= credit+1;
Serial.println(credit);
Serial.flush();
counter1=0;
counter2=0;
};
counter1=0;
counter2=0;
oldTime=newTime;
}
}

Welcome to the Forum. First, please read Nick Gammon's post at the top of this Forum about how to post here, especially the use of code tags when posting source code. Second, try using Ctrl-T in the IDE on your source code before you post it in the code tags. It will reformat your code into a common C style that will make it easier for us to read. Third, any time you use a variable in your interrupt service routine (ISR) that is not defined within that ISR function, you should define it with the volatile keyword:

volatile int counter1;

Fourth, it is a common C idiom to use:

  • counter1++;*

instead of

  • counter1 = counter1 + 1;*

Fifth, when you use a non-int constant in an expression, it's usually a good idea to tell the compiler the data type of the constant:

if (newTime - oldTime > 1000L) // Note the 'L' at the end of the constant

Sixth, you don't need the trailing semicolon at the end of the if statement block:

  if(counter1 - counter2 == 1)
    {
    credit= credit+1;
    Serial.println(credit);
    Serial.flush();
    counter1=0;
    counter2=0;
    };                  // This semicolon is not needed.

Seventh, I think you will find it easier to read your code if you place spaces around operators, like in the if statement test above.

Finally, you are testing in your if blocks for a time interval of 1 millisecond. Do you think that might be a problem?

econjack:
Welcome to the Forum. First, please read Nick Gammon's post at the top of this Forum about how to post here, especially the use of code tags when posting source code. Second, try using Ctrl-T in the IDE on your source code before you post it in the code tags. It will reformat your code into a common C style that will make it easier for us to read. Third, any time you use a variable in your interrupt service routine (ISR) that is not defined within that ISR function, you should define it with the volatile keyword:

volatile int counter1;

Fourth, it is a common C idiom to use:

  • counter1++;*

instead of

  • counter1 = counter1 + 1;*

Fifth, when you use a non-int constant in an expression, it's usually a good idea to tell the compiler the data type of the constant:

if (newTime - oldTime > 1000L) // Note the 'L' at the end of the constant

Sixth, you don't need the trailing semicolon at the end of the if statement block:

  if(counter1 - counter2 == 1)

{
    credit= credit+1;
    Serial.println(credit);
    Serial.flush();
    counter1=0;
    counter2=0;
    };                  // This semicolon is not needed.




Seventh, I think you will find it easier to read your code if you place spaces around operators, like in the *if* statement test above.

Finally, you are testing in your *if* blocks for a time interval of 1 millisecond. Do you think that might be a problem?

Thank you so much.Now my program runs perfect.I tested the program all day.There is no errors.I have added liquid crystal to show time in minutes and seconds.I have added role to make coin acceptor work , when message comes from serial port.My lock screen program starts with windows,when coin inserted , lock screen is hidden,but i have to send message to serial port 3 times.I think i have problem with my windows program.So i added do-while loop to send time variable 3 times.Now works fine.This is my programs last status:

#include<LiquidCrystal.h>
LiquidCrystal LCD(8,7,6,5,4,3);
const int role=9;
int credit=0;
volatile int counter1=0;
int counter2=0;
unsigned long oldTime=0;
unsigned long newTime=0;
int minute=0;
int second=0;
int timer=0;
int sender=0;

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2),pulse,RISING);
  pinMode(role,OUTPUT);
  LCD.begin(16,2);
}

void pulse()
{
  counter1++;
}

void loop() {
  
  newTime=millis();
  if(newTime-oldTime>1000L)
  {
    LCD.setCursor(0,0);
    LCD.print(" Remaining time ");
    LCD.setCursor(0,1);
    LCD.print("      ");
    LCD.print(minute);
    LCD.print(":");
    LCD.print(second);
    LCD.print(" ");
    if(counter1-counter2>1)
    {
      counter1=0;
      counter2=0;
    }
    if(counter1-counter2==1)
    {
    minute++;
    sender++;
    timer=(minute*60)+second;
    counter1=0;
    counter2=0;
     do
     {
      Serial.println(timer);
      Serial.flush();
      sender++;
      delay(100);
       if(sender==3)
       {
        sender=0;
       }
     }while(sender!=0);
    }
    if ((second>0)&&(minute>-1))
    {
    second--;
    }
    if ((second==0)&&(minute>0))
    {
      second=second+59;
      minute--;
    }
    counter1=0;
    counter2=0;
    oldTime=newTime;
  }
  if(Serial.available())
  {
    Serial.read();
    digitalWrite(role,HIGH);
  }
}

i will use 3 different coins
1=2pulse add 1min
2=4pulse add 3min
10=6 pulse and 15min

what do i need to change in the code?

what do i need to change in the code?

Whatever doesn't meet your undefined requirements.