Optocouplers and Serial monitor readings

Hello Everyone, i am currently working on a project where i am trying to make a coin sorting machine.

The principle, is that a coin is falling down into a container, where it flyes by an optocoupler, of this kind LTH 301-32. I have manages to get some code running which works fine as it was supposed to. The next step is to only get readings inside the Serial monitor when a change happens on the pin which the optocoupler is connected to, so my Serial monitor doesn’t get spammed with every single reading from the optocoupler, and i dont know how to make it happen like i want to. :((

You can see my code here:

!EDIT:
Okay, so when i run my code, the optocoupler gives me a lot of analog reading in my Serial monitor, and i get to see the total saldo, which is 0.00.

Then when i block off the light from the LED in the optocoupler, it is this one LTH 301-32, i see that the analog readings change to a lower value. Lets say if i hold this item in front of the LED in two seconds i would have a ton of analog reading.
In my code i have an if statement that says, i a value gets below 900, it will add 20 (dollars) to my saldo. But because of the many readings it would have within the two seconds that i block the LED, i would have over 2k dollars. It is this part of the programming where i need help.

I want to just add 20 dollars to the saldo, until the LED gets blocked again, so it is not the time of the blokade that decides how many dollars is added to the saldo, but the amount of times that the LED gets blocked by a coin.

Hopefully this explains my problem better :slight_smile:

Optokobler_kode.ino (709 Bytes)

only get readings inside the Serial monitor when a change happens on the pin which the optocoupler is connected

doesn't get spammed with every single reading from the optocoupler

What is the difference between a change that you want to record and a change that is spam?

groundFungus:
What is the difference between a change that you want to record and a change that is spam?

Okay, so when i run my code, the optocoupler gives me a lot of analog reading in my Serial monitor, and i get to see the total saldo, which is 0.00.

Then when i block off the light from the LED in the optocoupler, it is this one LTH 301-32, i see that the analog readings change to a lower value. Lets say if i hold this item in front of the LED in two seconds i would have a ton of analog reading.
In my code i have an if statement that says, i a value gets below 900, it will add 20 (dollars) to my saldo. But because of the many readings it would have within the two seconds that i block the LED, i would have over 2k dollars. It is this part of the programming where i need help.

I want to just add 20 dollars to the saldo, until the LED gets blocked again, so it is not the time of the blokade that decides how many dollars is added to the saldo, but the amount of times that the LED gets blocked by a coin.

Hopefully this explains my problem better :slight_smile:

Why use an analog input for a digital sensor?

groundFungus:
Why use an analog input for a digital sensor?

That is a good question. I saw an example, where they used an analog pin, and i tried to to a digital pin too. Both works, but the problem would continue to be the same no matter what type of pin i use.

Can you provide a wiring diagram for your project? How to post an image.

Can you post or link to a data sheet for the optointerrupter?

Can you post the code that you are using now?

Okay so i have changed my code a bit, but the problem is still the same. The pin on the schematic, is using the analogpin A2 but im using the digitalpin 2


Datasheet

#include <PinChangeInterrupt.h>

int optoPin = 2;
volatile float saldo = 0;
volatile int val = 0;






void setup() {
  Serial.begin(9600);
  //Pin setup
  pinMode(optoPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(optoPin), add, CHANGE);

  //Start message setup
  Serial.print(saldo);
  Serial.print(" Dollars");
  
  

}

void loop() {
  Serial.println(" ");
  val = digitalRead(optoPin);
  Serial.print(val);
  delay(1000);
  
}

void add(){
  saldo += 20;
  Serial.println(saldo);
  Serial.print(" Dollars");
}