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:
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);
}
}