After research, and studying the C++ syntax I took a shot at writing a little bit of code. Starting with the lCD , then the coin acceptor and activating a few relays when a specific currency total is met. I want to limit the presses to a button when the conditions are met. The issue I'm having is the button code within an Interrupt/ if statement wont activate properly.
Example:
if (money == .50){
button active only for a single shot. (this part is where I get a little confused)
1 shot = activate pin 13 for a specific amount of time set using the delay(xxx); function.
}
I have the code activating the relays to show physical proof that I can update the LCD activate something in the real world.
The Question:
How can I limit button presses when conditions are met, i.e. when money==.50 only allow 1 button press, money == 1.00 only allow 3 button presses.
The button is attached to digital pin 12 Output is digital pin 13
Edit: Full code now attached.
//Load Libraries
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// this constant won't change:
const int Button = 12; // the pin that the pushbutton is attached to
const int LED = 13; // the pin that the LED is attached to
const int Relay = 11;
// Variables will change:
int RelayState = LOW;
int buttonState = 0;
int lastButtonState = HIGH;
int ButtonCount = 0;
//LCD QTY update
int Shots = 0;
//Define PINS
#define COIN_PIN 2
#define I2C_ADDR 0x27 // <<- Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup()
{
// initialize the inputs and outputs
pinMode(Button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(Relay, OUTPUT);
digitalWrite(LED, HIGH);
// Debugging
Serial.begin(9600); // set up the LCD's number of rows and columns:
lcd.begin(20, 4);
//Turn on lcd backlight
lcd .setBacklightPin(BACKLIGHT_PIN,POSITIVE); // turn on lcd LED
lcd.setBacklight(HIGH);
//System Loading
lcd.begin(20, 4);
delay(100);
lcd.setCursor(3,1);
lcd.print("FATAL MENTALITY");
lcd.setCursor(5,2);
lcd.print("DESIGNS.COM");
delay(1000);
lcd.clear();
lcd.print("SCREAM MACHINE 1.1");
delay(1000);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("SYSTEM LOADING..");
{
delay(500);
}
lcd.home();
lcd.setCursor(2,2);
delay(500);
lcd.clear();
lcd.setCursor(5,1);
lcd.print("SCREAM ON!");
delay(1000);
lcd.clear();
//End of LCD system loading code
Serial.println("Ready...");
pinMode(COIN_PIN, INPUT);
attachInterrupt(0, coinISR, RISING); // COIN wire connected to D2;
}
// total amount of money collected;
float Money = 0.0;
// gets incremented by the ISR;
// gets reset when coin was recognized (after train of pulses ends);
volatile int pulses = 0;
volatile long timeLastPulse = 0;
// executed for every pulse;
void coinISR()
{
pulses++;
timeLastPulse = millis();
}
void loop() {
//LCD Information
lcd.setCursor(0,0);
lcd.print("_.:SCREAM MACHINE:._");
lcd.setCursor (1,1);
lcd.print("$1.00 = 3 $0.50 = 1");
lcd.setCursor(0,1);
lcd.setCursor (0,2);
lcd.print("INSERT COINS: $");
lcd.print(Money);
lcd.setCursor(0,3);
lcd.print("TOTAL SHOTS:");
lcd.setCursor(13,3);
lcd.print(Shots);
//coin acceptor code
buttonState = digitalRead(Button);
long timeFromLastPulse = millis() - timeLastPulse;
if (pulses > 0 && timeFromLastPulse > 200){
// sequence of pulses stopped; determine the coin type;
if (pulses == 10)
{
}
Serial.println("Received Quarter (10 pulses)");
Money += .25;
if (Money == .50){Shots +=1;
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);}
if (Money == 1.00) {Shots += 2;
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
}
if (Money > 1.00) {Money = .25;}
if (Shots > 3) {Shots = 1;}
{
pulses = 0;
}
}
}
Where I assume I put the limit of times the program reacts to a switch. (@PaulS)
//coin acceptor code
long timeFromLastPulse = millis() - timeLastPulse;
if (pulses > 0 && timeFromLastPulse > 200){
// sequence of pulses stopped; determine the coin type;
if (pulses == 10)
{
}
Serial.println("Received Quarter (10 pulses)");
Money += .25;
if (Money == .50){Shots +=1;
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(500);
digitalWrite(LED,HIGH);
delay(500);
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);}
if (Money == 1.00) {Shots += 2;
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
}
if (Money > 1.00) {Money = .25;}
if (Shots > 3) {Shots = 1;}
{
pulses = 0;
}
}
}
Thanks In advance for any help,
-Eric G