Need help in coding timer to be display in lcd and timer in relay


#21

How should i declare it?
attachInterrupt(digitalPinToInterrupt(Coinpin), coinInterrupt, RISING);


#22

it’s already fix
:smiley:


#23

The compilation is successful but thers a problem the relay is always on


#24

Could you post your full ino file again for review?


#25

CZONETRIAL1.ino (2.2 KB)
I try to insert coin and this is the result

Thank you so much sir :smiley:


#26

It would seem something is not right with the condition after your serial print. Please add in lines to print the remainingTimeMs and let us know what you see in the serial output (copy and paste is better than a screenshot). I’ve included the existing lines to set the reamingingTimeMs and check it so that you know where to put these new lines.

long remainingTimeMs = countdownEndTimeMs - nowMs;
Serial.print("remainingTimeMs: ");
Serial.println(remainingTimeMs, DEC);
if (remainingTimeMs > 0) {

Also, I’d move the statements to print the pulse to the top of your loop function, before you check if it’s > 0.

void loop () {
    Serial.print("Pulse: ");
    Serial.println(pulse, DEC);
    // If a coin has been inserted, reset the timeout
    if (pulse > 0) {

#27

I’m sure this is going to end up being one of the longest threads on here! :smiley:

The principle behind adding these serial print lines is to allow us to see what a value is before the code branches the logic based on the value. So by printing the value of pulse before we check if it’s more than zero, we’ll know whether the program went into that block or not. The same for the remainingTimeMs.

Another approach is to put println in each code path so that you can see where it is in the program. It’s the poor developer’s debugger - because we don’t have a debugger in the Arduino IDE.

So just keep adding println statements in your code until you understand where the flow is and why it’s not behaving as you expect.


#28

Thank You sir for helping :smiley: I’ll do my best to make this thing run :smiley:


#29

This is the result

`

remainingTimeMs: -16

`


#30

One other thing, and this might be causing some issues, or will in future. Change the following line:

volatile int pulse = 0;

to this:

volatile byte pulse = 0;

This saves us having to disable interrupts whilst we read that value because it’s a single byte.


#31

It must be stuck in the blocking loop then. Try replace that block with this (to include prints):

// Block the loop until a coin is inserted
while (pulse < 1) {
	Serial.print("Waiting for pulse to be at least 1. Pulse: ");
	Serial.println(pulse, DEC);
    // Let the Arduino sleep for a bit to save battery
    delay(100);
}

And please confirm you’ve changed pulse to a byte from integer.


#32

after adding this codes this happens

// Block the loop until a coin is inserted
while (pulse < 1) {
	Serial.print("Waiting for pulse to be at least 1. Pulse: ");
	Serial.println(pulse, DEC);
    // Let the Arduino sleep for a bit to save battery
    delay(100);
}

this happens

C:\Users\Adrian C. Mendoza\Downloads\CZONETRIAL1\CZONETRIAL1.ino: In function 'void loop()':

CZONETRIAL1:80:21: error: a function-definition is not allowed here before '{' token

 void coinInterrupt(){    pulse++;

                     ^

CZONETRIAL1:87:1: error: expected '}' at end of input

 }

 ^

exit status 1
a function-definition is not allowed here before '{' token


#33

There’s either a missing closing brace ("}") or one too many now. When you move your cursor to the } do you see it highlight the matching opening brace?

I see the issue. You have the while in there twice.


#34

I already removed the other while the error about the brace has gone but my declaration of the coinInterrupt is wrong> >

> 'int coinInterrupt' redeclared as different kind of symbol


#35

Where id that line come from? Please remove that int coinInterrupt; line.


#36

after removing and compiling, this is the result


#37

And pulse doesn’t ever get to 1 when you insert a coin? If that’s the case, the interrupt might not be working. Try put a println in the interrupt method:

void coinInterrupt(){
    Serial.println("coinInterrupt invoked");
    pulse++;
    bInserted = true;
}

#38

This is the coin acceptor I’ve been using, I bought this coin acceptor without a manual.


#39

LOL isn’t that the worst. I’ve been there. Do you know what model number it has or the company name? What i normally do is just search for the schematics online. I’m sure they have an online pdf version.


#40

I already try searching it on the internet but i didn’t find one :frowning: