Hi... really need help on this.
IS THERE ANY OTHER WAY TO COUNT OCCURRING PULSES FIRST BEFORE ENTERING THE CONDITIONS IN THE LOOP?
By counting first all occurring pulses the system can identify what type of coin was inserted and move the servo motor to each degree with respect to the type of coin inserted.
TYPE 1 coin = 1 pulse.
TYPE 2 coin = 5 pulses.
TYPE 3 coin = 10 pulses.
With the limited knowledge i have i used a function with delay in it that is being called as a temporary solution to count first all occurring pulses. With the "Counter" function it gives the system the ability to count pulses first before entering the conditions.
#include <Servo.h>
#include <EEPROM.h>
Servo myservo;
volatile int coins = 0;
volatile int CoinChange = 0;
int CoinSub1 = 10;
int CoinTotal;
int COne;
int CFive;
int CTen;
void setup()
{
myservo.attach(9);
//CoinSlot
EIFR = _BV (INTF0);
pinMode(2,INPUT_PULLUP);
attachInterrupt(0, coinInserted, FALLING);
//coinslot
}
void coinInserted(){
coins = coins + 1;
CoinChange = 1;
}
void Counter(){
delay(1500);
delay(900);
}
void loop()
{
COne = EEPROM.read(4);
CFive = EEPROM.read(6);
CTen = EEPROM.read(8);
CoinTotal = (COne*1) + (CFive*5) + (CTen*10);
Counter();
delay(900);
CoinSub1 = CoinSub1 - coins;
if(CoinChange == 1){
if (CoinSub1 == 9/*coins == 1*/)
{
if(COne < 50 && CoinTotal <= 799)
{
COne = COne + 1;
EEPROM.write(4, COne);
myservo.write(30);
}
}
else if (CoinSub1 == 5/*coins == 5*/)
{
if(CFive < 50 && CoinTotal <= 795)
{
CFive = CFive + 1;
EEPROM.write(6, CFive);
myservo.write(50);
}
}
else if (CoinSub1 == 0/*coins == 10*/)
{
if(CTen < 50 && CoinTotal <= 790)
{
CTen = CTen + 1;
EEPROM.write(8, CTen);
myservo.write(70);
}
}
CoinSub();
}
}
void CoinSub(){
CoinSub = 10;
}
BUT with this coding, with repetitive insertion of coins, the ACCURACY isn't 100% because even if there is still pulses occurring it enters the conditions within the loop which results in a wrong identification of coin. And another result of this coding is that the response time of the servo motor to move in the degree respective to the coin inserted after a coin is inserted in the system is 3-5 seconds.
FOR EFFICIENCY, i require the system to be always 100% in identifying the coins being inserted and a much faster response time.