Created
August 10, 2022 15:38
-
-
Save Frulko/8937148304e61f01e30bcc3c8f8c52e7 to your computer and use it in GitHub Desktop.
Coin Acceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int coinSelector = A1; // Analog input pin that the coin selector uses | |
int signalValue = 0; // For storing value from analog input | |
int signalPreviousValue = 0; | |
int offsetTrigger = 80; | |
int pulseCounter = 0; | |
bool hasTriggered = false; | |
int state = 0; | |
int previousState = 0; | |
int incomingByte = 0; | |
int learningNumber = 50; | |
int learningCounter = 0; | |
int learningOffset = 0; | |
/* | |
detect front montant pour determiner debut de lecture | |
plus si temps - dernier front montant est trop long alors nouvelle pièce | |
*/ | |
void setup() { | |
Serial.begin(38400); | |
Serial.println("Ready..."); | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
incomingByte = Serial.read(); | |
if ((char)incomingByte == 'r') { | |
Serial.println("---- ------ ------ reset"); | |
pulseCounter = 0; | |
} | |
if ((char)incomingByte == 'l') { | |
Serial.println("---- ------ ------ learn"); | |
learningCounter = 0; | |
learningOffset = 0; | |
} | |
} | |
if (learningOffset == 0 && learningCounter != learningNumber) { | |
if (signalValue > learningOffset) { | |
learningOffset = signalValue; | |
} | |
learningCounter++; | |
} | |
signalValue = analogRead(coinSelector); | |
if (signalValue < 4) { | |
signalValue = offsetTrigger - 4; | |
} | |
Serial.print("Learning: "); | |
Serial.print(learningOffset); | |
Serial.print("-- counter: "); | |
Serial.print(pulseCounter); | |
Serial.print(" --- pulse: "); | |
Serial.print(signalValue); | |
if (signalValue > (learningOffset + 2)) { // up state | |
//Serial.println(" -- up"); | |
state = 1; | |
} | |
if (signalValue < (learningOffset + 2) && signalPreviousValue > (learningOffset + 2)) { // low state | |
Serial.println(" -- pulse"); | |
pulseCounter += 1; | |
} else { | |
Serial.println(""); | |
} | |
previousState = state; | |
signalPreviousValue = signalValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment