Haven't gotten back home yet to test this, but I whipped up some code that I think will handle the hopper sensor a bit quicker using interrupts. I haven't tested it yet, but let me know what you think. NOTE YOU'LL HAVE TO MAKE SURE FRUITSQUEEZER IS NOT USING PIN 21 IN THE APPLICATION (I have no code to check for conflict, so don't send commands to a "light" on pin 21.
NOTE: My hopper activates (and outputs) LOW (0V to run hopper, trigger falls to 0V when a coin passes). You may need to adjust the code accordingly.
EDIT: I'm using an MK4 Universal Hopper (24v). Regarding Latency I do see the lights don't quite "match" on screen when blinking (they also don't light 'simultaneously', one after the other, which is a bit expected based on baud rate). Quick flashes may be skipped (flashes 4 times on screen 2 on board) I think. I didn't test it for too long.
#define hopSens 21 //pin wejściowy z sensowa optycznego (dekrementacja licznika hoppera)
#define hopMot 55 //pin wyzwalający zasilania silnika hoppera
int hopStat = 0; //ilość pulsów i tym samym monet do wypłaty
int SensorStat = 0; //aktualny stan sensora - wysoki lub niski
int lastSensorStat = 0; //poprzedni stan sensora - wysoki lub niski
void setup() {
Serial.begin(115200);
Serial.setTimeout(10);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
pinMode(20, OUTPUT);
//pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
pinMode(29, OUTPUT);
pinMode(30, OUTPUT);
pinMode(31, OUTPUT);
pinMode(32, OUTPUT);
pinMode(33, OUTPUT);
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
pinMode(36, OUTPUT);
pinMode(37, OUTPUT);
pinMode(38, OUTPUT);
pinMode(39, OUTPUT);
pinMode(40, OUTPUT);
pinMode(41, OUTPUT);
pinMode(42, OUTPUT);
pinMode(43, OUTPUT);
pinMode(44, OUTPUT);
pinMode(45, OUTPUT);
pinMode(46, OUTPUT);
pinMode(47, OUTPUT);
pinMode(48, OUTPUT);
pinMode(49, OUTPUT);
pinMode(50, OUTPUT);
pinMode(51, OUTPUT);
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
pinMode(21, INPUT);
digitalWrite(21, HIGH);
attachInterrupt(digitalPinToInterrupt(21), HopperSensor, FALLING); //Set this to Rising if you have a "positive" trigger rather than ground
pinMode(55, OUTPUT);
pinMode(56, OUTPUT);
pinMode(57, OUTPUT);
pinMode(58, OUTPUT);
pinMode(59, OUTPUT);
pinMode(60, OUTPUT);
pinMode(61, OUTPUT);
pinMode(62, OUTPUT);
pinMode(63, OUTPUT);
pinMode(64, OUTPUT);
pinMode(65, OUTPUT);
pinMode(66, OUTPUT);
pinMode(67, OUTPUT);
pinMode(68, OUTPUT);
pinMode(69, OUTPUT);
}
void loop()
{
// SensorStat = digitalRead(hopSens); //przypisanie stanu sensora do pinu podpiętego do sensora hoppera
if (Serial.available() > 0) //ten cały blok kodu czyta sygnały z seriala x-LEDy, c-monety
{
int hopSig = Serial.read();
if (hopSig == 'c')
{
hopStat++;
digitalWrite(hopMot, 0);
}
int pin = Serial.parseInt();
int value = Serial.parseInt();
if (Serial.read() == 'x')
{
digitalWrite(pin, value);
}
int pinA = Serial.parseInt();
int valueA = Serial.parseInt();
if (Serial.read() == 'f')
{
analogWrite(pinA, valueA);
}
}
}
void HopperSensor() {
hopStat--;
if (hopStat > 0) //jeśli licznik monet będzie większy od 0, zaczynamy zabawę
{
digitalWrite(hopMot, 0); //włączamy silnik hoppera i tym samym całe jego zasilanie
}
else
{
digitalWrite(hopMot, 1);
}
}
Edited by compuw22d, 01 April 2019 - 09:19 PM.