using interrupts

i need help with reading interrupts from coin acceptor and activating respective solenoids based on the interrupt received. for example if i set the coin acceptor to give 5 pulses for p1 coin, arduino should be able to read the 5 pulses and activate the respective solenoid/led and for p2 coin if pulses is 2 arduino should activate another solenoid/led. Please help me program this. :frowning:

i've connected pulse of coin acceptor to interrupt 0 which is pin 2 on arduino
and for solenoid i've connected transistor and a digital pin is connected to the base so when p1 is detected arduino shld read the interrupt and push solenoid and for p2 another solenoid

solenoid i"ve used is of 5V, and coin acceptor is 12V and i've shorted the ground of arduino and 12V supply i need help with program only Please help.

Show us a good schematic of your circuit.
Show us a good image of your wiring.

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.
[code] Paste sketch here. [/code]

cyborg8274:
Please help me program this.

Make a start and if you get stuck post the code that represents your best attempt.

...R

connection is as in the picture and any 2 digital pins are the output to the solenoids.
the coin acceptor can be set to give pulses upto 50, but I’ve set it to give
2 pulses for p1 coin and
5 pulses for p2 coin

pulses to arduino is connected with signal wire.

code </
volatile int coins = 0; //counter pin from coin acceptor
int solenoidPin = 12; // output to solenoid1 pin for 1st solenoid
int solenoid2Pin = 13;

void setup()
{
Serial.begin(9600);
pinMode (2,INPUT_PULLUP); //2 is counter pin from coin acceptor
attachInterrupt(0, coinInserted, CHANGE); //coin counterr
pinMode(solenoidPin, OUTPUT); //sets 12th pin as output
pinMode(solenoidPin, OUTPUT);//11th output for sol2
}
void coinInserted()
{
coins=coins+1;
}
void loop()
{
Serial.print(coins);

if( coins == 2)
{
digitalWrite(solenoidPin, HIGH);
delay(1000);
digitalWrite(solenoidPin, LOW);
delay(1000);
coins = 0;

}
else if(coins == 5);
{
digitalWrite(solenoid2Pin, HIGH);
delay(1000);
digitalWrite(solenoid2Pin, LOW);
delay(1000);
}
}

/>

if(coins == 5); That semicolon is a problem.

If you insert a 5P coin, won't your program trigger the sol on pin 12 after the first 2 pulses? Shouldn't you use either RISING or FALLING in the interrupt setup, looks like CHANGE would cause doubling of interrupts?
That acceptor may already have what you need built in, can you post a link and/or datasheet?
On your Fritzing picture, I'm not sure about the color of the 3rd band on that pulldown resistor, if it's silver then it's 0.1 Ohm, that's bad.

Yes, I’ve tried the falling and also rising, but I I’m not getting any output on either of the output pins. I don’t think there is a problem with the coin acceptor as I’ve checked the pulse by directly connecting a led so the led blinked twice for p2 coin and blinked 5 times for p1 coin jus like I’ve set them to give pulses. I don’t think pull down resistor is even necessary i’ve Simply used it, connecting it to interrupt directly also works.

OP’s code properly displayed using the code button </> and indented for easier reading - and erroneous semi-colons removed

volatile int coins = 0; //counter pin from coin acceptor
int solenoidPin = 12; // output to solenoid1 pin for 1st solenoid
int solenoid2Pin = 13;

void setup()
{
    Serial.begin(9600); 
    pinMode (2,INPUT_PULLUP); //2 is counter pin from coin acceptor
    attachInterrupt(0, coinInserted, CHANGE); //coin counterr
    pinMode(solenoidPin, OUTPUT); //sets 12th pin as output
    pinMode(solenoidPin, OUTPUT);//11th output for sol2
}

void coinInserted()
{
    coins=coins+1;
}

void loop()
{
    Serial.print(coins)

    if( coins == 2)
    {
        digitalWrite(solenoidPin, HIGH);
        delay(1000);
        digitalWrite(solenoidPin, LOW);
        delay(1000);
        coins = 0;
    }
    else if(coins == 5)
    {
        digitalWrite(solenoid2Pin, HIGH);
        delay(1000);
        digitalWrite(solenoid2Pin, LOW);
        delay(1000);
    }
}

…R

Still got that extraneous semicolon.

TolpuddleSartre:
Still got that extraneous semicolon.

I notice you did not correct the code before posting Reply #4

I have now updated my version
...R

@cyborg8274, your program has a major logical problem - how does it know that all of the pulses for a particular coin have been detected?

If you don't deal with that the first 2 pulses for a 5 pulse coin could be interpreted as a 2 pulse coin.

My guess is that when you insert a coin the pulses occur fairly quickly so that you would know that the train of pulses is complete when another pulse does not arrive within a certain time. If you build some code like that into your ISR then it could set a variable (let's call it newCoin) = true when all the pulses have stopped coming.

When the code in loop() sees that newCoin == true it will know that a valid count exists.

You also have a small technical problem. You are using an int as the datatype for your coins variable. An int uses 2 bytes and if you try to read a variable with 2 (or more) bytes that is updated by an ISR the value could change while you are reading the variable - because the read takes more than one instruction. The simplest way to deal with this for your project is to define coins as a byte variable. Otherwise you need to briefly disable interrupts while you read a multi-byte variable.

...R

Can you tell me how do I read all the impulses and then activate the solenoid pins.

@cyborg8274

Can you please, reformat the program codes of your Post#3.

  1. Open your post in the Edit/Modify Mode.

  2. Bring the cursor at the very beginning of your code. Now, type this word: [ c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.

  3. Bring the cursor at the very end of your code. Now, type this word: [ / c o d e ]. Please, do not keep spaces among the letters while typing the word. Keep them together. I have to keep spaces due to some technical reasons.

  4. Press the save button.

Now, your codes/post looks so beautiful!

. I have to keep spaces due to some technical reasons.

Why not just write them like this [code][/code] ?

Look Ma, no spaces

[quote author=GolamMostafa link=msg=3679464 date=1523016818

Can you please, reformat the program codes of your Post#3.
[/quote]
I believe that is what I did in Reply #7

...R

cyborg8274:
Can you tell me how do I read all the impulses and then activate the solenoid pins.

Did you read Reply #10?

...R

byte PWM_PIN = 2;
 int pwm_value;
 const int ledpin=13; 
void setup() {
  // put your setup code here, to run once:
 pinMode(PWM_PIN, INPUT);
 pinMode(ledpin, OUTPUT);
 Serial.begin(115200);
 

}

void loop() {
  // put your main code here, to run repeatedly:
pwm_value = pulseIn(PWM_PIN, HIGH);
Serial.println(pwm_value);

if(pwm_value == 2)
{
  digitalWrite(ledpin, HIGH);
  delay(1000);
  digitalWrite(ledpin, LOW);
  delay(1000);
  return 0;
}
else if(pwm_value==5)
{
  
 digitalWrite(12, HIGH);
 delay(1000);
 digitalWrite(12, LOW);
 delay(1000);
 return 0;
}
}

how about this code

please help me get out of the if loop and please check if this code checks all the pulses and then executes

cyborg8274:
how about this code

IMHO the code in your Original Post was better - going in the right direction.

What gave you the idea to use pulseIn()

Let's think about this logically ...

When you put in a coin that produces the maximum number of pulses what is the time interval (in microsecs) between the first pulse and the last pulse?

When you ISR first detects a pulse it should save the value of micros() and your maain code should allow it to keep counting pulses until enough time has elapsed that you can be certain that no more pulses will come from that coin. I have no idea what that time interval might be but let's pretend it is 20 millisecs or 20,000 microsecs. Then the code in your ISR (extended from your Original Post) might be like this

void coinInserted()
{
    if (waitingForCoin == true) { // make sure it only saves the time for the first pulse
       isrSavedMicros = micros();
       waitingForCoin = false;
    }
    coins = coins + 1;
}

and in your code in loop() you could have something like this

void loop() {
   noInterrupts();
      if (waitingForCoin == false) {
         coinStartMicros = isrSavedMicros;
         tempCoinPulses = coins;
      }
    interrupts();
    if (micros - coinStartMicros >= maxPulseTimeMicros) { // we know there will be no more pulses
       numCoinPulses = tempCoinPulses;
       waitingForCoin = true;
    }
   // other code
}

...R

Why have you started a new thread ?

I posted a program there but its not working at all, so i needed someone to write program as i'm very bad at programming