Hi everyone! I'm trying to create a system that uses a coin acceptor which sends pulses and buttons to instruct the arduino to know how much pulses it has to receive before turning on a led light.
int pulsePin = 22;
int ledPin = 53;
float highCounter = 0;
int pulse = 0;
int lastPulse = LOW;
int switchPin = 52;
int val;
int buttonState;
void setup() {
pinMode(pulsePin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pulsePin, HIGH);
pinMode(switchPin, INPUT);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
Serial.begin(4800);
Serial.println("Setup Complete");
buttonState = digitalRead(switchPin);
}
void loop() {
pulse = digitalRead(pulsePin);
if (pulse != lastPulse) {
lastPulse = pulse;
if (pulse == HIGH) highCounter++;
Serial.println(highCounter);
}
val = digitalRead(switchPin);
if (val != buttonState)
{
Serial.println("Insert 5 Peso Coin"); //only works until here
if (highCounter == 5)
{
Serial.println("Water Out");
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
highCounter = 0;
}
}
buttonState = val;
}
Can anyone help me to know what I am doing wrong in this?
Can anyone help me to know what I am doing wrong in this?
Sure. You haven't told us what the problem is. The code you posted (without bothering to properly format it first) does something. You didn't bother saying what that is. You want it to something. Presumably, that is not that same something as the something it is doing. But, you haven't said what that something is, either.
You haven't told us what coin acceptor you are using, or how the switch is wired, either.
Ah hehe sorry about that! I'm using a multi coin acceptor that accepts 1, 5, and 10 peso coins. What I want the program to do is that if I press the button it would instruct the arduino to collect 5 pesos once that is done it will light up the LED for 5 seconds. It reads the number of pulses received from the arduino and displays the "Inset 5 peso coin" but the next condition after that doesn't work which is if the highcounter is equal to 5 it would light up the led and display water out on the serial monitor.
pulse = digitalRead(pulsePin);
if (pulse != lastPulse) {
lastPulse = pulse;
if (pulse == HIGH) highCounter++;
Serial.println(highCounter);
}
Why are you setting lastPulse to pulse only when they are different? You should set lastPulse to pulse on EVERY pass through loop, just like you set buttonState to val.
And look at those names. Some of them are horrible. pulsePin, pulse, and lastPulse are good. Though, currPulse and prevPulse would be better than pulse and lastPulse.
But, val, buttonState, and switchPin don't seem to go together, and convey no information.
You still haven't said how the switch is wired, what coin acceptor you are using, or exactly what the problem is.
The switch state should determine whether to display the prompt. It should have NOTHING to do with checking highCounter or activating/deactivating the pin.
As the code is now, the pin will be toggled only if a 5 peso coin is inserted and then the switch is pressed.
Sorry about the names I just merged 2 codes that I made. The code below reads pulses from a coin acceptor and once the it receives a certain amount of pulses for instance 5 pulses it will turn on a led for 5 seconds.
unsigned long oldMillis = 0;
int pulsePin = 52;
int ledPin = 13;
unsigned long highCounter = 0;
unsigned long duration = 0;
int pulse = 0;
int lastPulse = LOW;
void setup() {
pinMode(pulsePin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pulsePin, HIGH);
Serial.begin(4800);
Serial.println("Stand Alone Filtered Water");
Serial.println("Refilling Station");
}
void loop() {
pulse = digitalRead(pulsePin);
if (pulse != lastPulse) {
lastPulse = pulse;
if (pulse == HIGH) highCounter++;
}
// print and reset highCounter every seconds
if ( millis() - oldMillis >= 5000 )
{
oldMillis = millis();
Serial.println(highCounter);
if (highCounter == 1)
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}
if (highCounter == 5)
{
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
if (highCounter == 10)
{
digitalWrite(ledPin, HIGH);
delay(15000);
digitalWrite(ledPin, LOW);
}
highCounter = 0;
}
}
The codes below is for a button where if button is pressed it will turn on a led light for 5 seconds.
int led1Pin = 12;
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int buttonState; // variable to hold the button state
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(led1Pin, OUTPUT);
Serial.begin(4800); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val != buttonState) { // the button state has changed!
digitalWrite(led1Pin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
}
buttonState = val; // save the new state in our variable
}
As the code is now, the pin will be toggled only if a 5 peso coin is inserted and then the switch is pressed.
Really? I thought you wanted to press the switch, see a message displayed, wait for 5 pulses, and then activate the LED pin.
Detecting the switch press and displaying the message are one event you want to deal with.
Detecting coin acceptor pulses is another event.
Toggling the LED pin is another event.
The events should occur in some order. You need to deal with events that happen in a different order, such as coin pulse events happening before the switch becoming pressed event happens.