Hi guys i am new here and Im planing to build a pinball machine using a arduino as score using a 16x2 display and 3 switches to count points 100,500 and 1000, until here every thing is fine but i wanna know how i can use the milis funcion cause im planing to set a bonus timer if switch 4 was pressed and points should be 5 times comon points during some time like 60 seconds ex: if switch 4 is pressed a = a5 ; b = b5 and soo on during 60 seconds.
Here is my code if someone can help , i will apreciate it.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
bool button_0State = LOW; // the current STABLE STATE of the input pin
bool lastButton_0State = LOW; // the previous reading from the input pin
long lastDebounce_0Time = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
boolean lastReadButton1;
boolean lastReadButton2;
boolean lastReadButton3;
boolean lastReadButton4;
unsigned int score = 0;
int ballnumber = 5;
int a = 100;
int b = 500;
int c = 1000;
// SETUP===========================================
void setup() {
//input pins for scoring triggers
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
// set up the LCD:
lcd.begin(16, 2);
lcd.clear();
}
//LOOP==============================================
void loop() {
//LCD TOP ROW: SCORE / GAME OVER
lcd.setCursor(0, 0);
if (ballnumber < 0)
{
lcd.print("GAME OVER ");
}
else
{
lcd.print("SCORE: BALL:");
lcd.print(ballnumber);
}
//LCD BOTTOM ROW: NUMERIC SCORE
lcd.setCursor(0, 1);
lcd.print(score);
//BONUS w/out milis//////////////////////////////////////
boolean readButton4 = digitalRead( 10 ) ;
if (readButton4 == LOW and lastReadButton4 == HIGH)
{
a = a*5; b = b*5; c = c*5;
}
lastReadButton4 = readButton4;
//SCORE a 100 PTS //////////////////////////////////////
boolean readButton1 = digitalRead ( 8 ) ;
if (readButton1 == LOW and lastReadButton1 == HIGH)
{
score = score + a;
}
lastReadButton1 = readButton1;
//SCORE a 100 PTS //////////////////////////////////////
boolean readButton2 = digitalRead ( 9 ) ;
if (readButton2 == LOW and lastReadButton2 == HIGH)
{
score = score + b;
}
lastReadButton2 = readButton2;
///LOST BALL///BLACK///////////////////////////////////
bool reading = digitalRead( 7 );
if (reading != lastButton_0State) {
lastDebounce_0Time = millis(); // reset the time since last stable state
lastButton_0State = reading;
}
if ((millis() - lastDebounce_0Time) > debounceDelay) {
// test for a transition from low-high or high-low
if (button_0State != lastButton_0State) {
button_0State = lastButton_0State;
if (button_0State == HIGH) {
ballnumber = ballnumber - 1;
}
}
}
}
if switch 4 is pressed a = a5 ; b = b5 and soo on during 60 seconds.
Save the value of millis() when switch 4 becomes pressed and set a score multiplier to 5. Then each time through loop() check whether 60 seconds has elapsed (millis() - start time > 60 seconds). If so, then set the score multiplier to one. If not then just carry on.
UKHeliBob:
Save the value of millis() when switch 4 becomes pressed and set a score multiplier to 5. Then each time through loop() check whether 60 seconds has elapsed (millis() - start time > 60 seconds). If so, then set the score multiplier to one. If not then just carry on.
Good morning and thanks for your help.
I tried to do what you succeded but I think I still do not understand how it works in the code, it would be possible for you to fix it for me if you can.
Thank you.
Here is the code i have done till now :
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
bool button_0State = LOW; // the current STABLE STATE of the input pin
bool lastButton_0State = LOW; // the previous reading from the input pin
long lastDebounce_0Time = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
//int bonustime = 0; // tempo para que o bonus funcione
boolean lastReadButton1;
boolean lastReadButton2;
boolean lastReadButton3;
boolean lastReadButton4;
unsigned long bonusbutton; // tempo desde que o botão foi pressionado
int bonustime = 10000; // tempo do bonus
unsigned int score = 0;
int ballnumber = 5;
int a = 100;
int b = 500;
int c = 1000;
void points();
void bonus();
// SETUP===========================================
void setup() {
//input pins for scoring triggers
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
// set up the LCD:
lcd.begin(16, 2);
lcd.clear();
}
//LOOP==============================================
void loop() {
points();
bonus();
}
void points(){
//LCD TOP ROW: SCORE / GAME OVER
lcd.setCursor(0, 0);
if (ballnumber < 0)
{
lcd.print("GAME OVER ");
}
else
{
lcd.print("SCORE: BALL:");
lcd.print(ballnumber);
}
//LCD BOTTOM ROW: NUMERIC SCORE
lcd.setCursor(0, 1);
lcd.print(score);
//SCORE a 100 PTS //////////////////////////////////////
boolean readButton1 = digitalRead(8);
if (readButton1 == LOW and lastReadButton1 == HIGH)
{
score = score + a;
}
lastReadButton1 = readButton1;
//SCORE a 500 PTS //////////////////////////////////////
boolean readButton2 = digitalRead(9);
if (readButton2 == LOW and lastReadButton2 == HIGH)
{
score = score + b;
}
lastReadButton2 = readButton2;
///LOST BALL///BLACK///////////////////////////////////
bool reading = digitalRead(7);
if (reading != lastButton_0State) {
lastDebounce_0Time = millis(); // Redefinir o tempo desde o último estado estável
lastButton_0State = reading;
}
if ((millis() - lastDebounce_0Time) > debounceDelay) {
// test for a transition from low-high or high-low
if (button_0State != lastButton_0State) {
button_0State = lastButton_0State;
if (button_0State == HIGH) {
ballnumber = ballnumber - 1;
}
}
}
}
void bonus(){
static unsigned long bonusbutton = digitalRead(10);
if ((millis() - bonusbutton) < bonustime) {
if (digitalRead(10) == HIGH)
{
a = 500; b = 1000; c = 1500;
}
if ((millis() - bonusbutton) > bonustime) {
if (digitalRead(10) == LOW)
{
a = a; b = b; c = c;
}
//if ((millis() - bonusbutton) >= bonustime){
//bonustime = millis();
//}
}
}
}
Firstly pull all that code out of loop() into small, well named functions, and leave loop() as a list of function
calls. This will help structure the code and prevent loop() becoming confusing mess of stuff.
Your program is going to be a group of state-machines for each feature of the pinball machine, so try to
keep that reflected in the code - each statemachine should have a function to drive its transitions, called
from loop(), and sub functions for the detail - choose function names well and the code is very readable
and easy to navigate.
MarkT:
Firstly pull all that code out of loop() into small, well named functions, and leave loop() as a list of function
calls. This will help structure the code and prevent loop() becoming confusing mess of stuff.