Hi everybody
I've built a large-scale Head-to-Head Perfection game. Perfection game
I'm having difficulty with the timer. When I press Reset (RS) the timer starts counting down automatically. I'm trying to get the countdown to start when I press Start (ST). I've tried moving the timer to the loop that only activates when the start button is pushed. All the other components of the circuit cooperate. The P1 and P2 buttons don't work until start is pressed. The LED's don't display until start is pressed. But the countdown has already begun. I've tried adding the number of milliseconds from the time the game was reset to the (startTym) to the duration of the game (endTym) but it hasn't helped.
The way I want it to work is to be able to press Reset, push the board halves down, then press Start. The players should have the full amount of time to play. IRL the players are penalized by the amount of time it takes for me to press the Start button.
Any help appreciated.
Code snippet. Full code follows.
void loop() {
b4State =digitalRead(start);
if(b4State ==0) // Start button pressed
{ flag =1;
delay(200); // debounce
}
if(1==flag)
{
if (flag2 == 0 && flag == 1)
{
startTym = millis(); //initial start time
endTym = (startTym + endTym);
flag2 = 1; // makes sure not to grab the start time more than once
}
currentTym = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentTym >= endTym) //check if the game time is up
{
//www.ijprems.com editor@ijprems.com Vol. 04, Issue 11, November 2024, pp : 1638-1642
//e-ISSN : 2583-1062 Impact Factor : 7.001
// https://github.com/adafruit/Adafruit_NeoPixel
// https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573
// https://wokwi.com/ World's most advanced ESP32 simulator
// Not shown is an 4 relay shield. Pins 4, 5, 6, 7 operate relays.
// LED's R1, R2 represent 12v Soleniods
// Ticker represents a 12v relay that provides sound.
// Press Start (ST). This Starts the timer and the Neopixels
// Pressing P1 triggers R1 and freezes the timer
// Pressing P2 triggers R2 and freezes the timer
// Press Reset (RS) to restart the game
// This program is the merger of a button sketch and a timer sketch
// from buttons
#define player1 9
#define player2 3
#define start 12
#define relay1 5
#define relay2 6
#define uint8 unsigned char
uint8 flag =0; uint8 b1State,b2State,b3State,b4State =0;
int flag2 =0;
float tym =0; float eltym =0;
// from timer
#include <Adafruit_NeoPixel.h>
#define NEOPIXEL1_PIN 10
#define NEOPIXEL1_NUM 18
unsigned long startTym; //some global variables available anywhere in the program
unsigned long currentTym;
const unsigned long onTym = 500; //the value is a number of milliseconds
unsigned long offTym = 500; //the value is a number of milliseconds
unsigned long cycleTym = 1000; //the value is a number of milliseconds
unsigned long endTym = 200000; //the value is a number of milliseconds total game time
const byte ticker1 = 7; //relay sound
const byte ticker2 = 8; //relay sound
Adafruit_NeoPixel pixels(NEOPIXEL1_NUM, NEOPIXEL1_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(relay1 ,OUTPUT);
pinMode(relay2 ,OUTPUT);
pinMode(player1 ,INPUT_PULLUP);
pinMode(player2 ,INPUT_PULLUP);
pinMode(start ,INPUT_PULLUP);
digitalWrite(relay1 ,HIGH);
digitalWrite(relay2 ,HIGH);
// timer section
pixels.begin(); // Initialize NeoPixel strip
pixels.clear(); // Clears previous NeoPixel settings
pixels.show(); // Actually shows the cleared settings
pinMode(ticker1, OUTPUT);
pinMode(ticker2, OUTPUT);
//startTym = millis(); //initial start time
digitalWrite(ticker1, LOW);
digitalWrite(ticker2, LOW);
}
void loop() {
b4State =digitalRead(start);
if(b4State ==0) // Start button pressed
{ flag =1;
delay(200); // debounce
}
if(1==flag)
{
if (flag2 == 0 && flag == 1)
{
startTym = millis(); //initial start time
endTym = (startTym + endTym);
flag2 = 1; // makes sure not to grab the start time more than once
}
currentTym = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentTym >= endTym) //check if the game time is up
{
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(ticker1, LOW);
digitalWrite(ticker2, LOW);
delay(120000);
}
b1State =digitalRead(player1 );
b2State =digitalRead(player2 );
if(b1State==0) // Player 1 pressed the button
{ flag =0;
digitalWrite(relay1 ,LOW); // Release the magnet holding one side down
digitalWrite(relay2 ,HIGH); // Keep the winning side down
digitalWrite(ticker1, LOW); // stop the ticker
digitalWrite(ticker2, LOW); // stop the ticker
delay(120000); // Wait 2 min for selfies.
while(digitalRead(start ));
}
if(b2State==0) // Player 2 pressed the button
{ flag =0;
digitalWrite(ticker1, LOW); // stop the ticker
digitalWrite(ticker2, LOW); // stop the ticker
digitalWrite(relay1 ,HIGH); // Keep the winning side down
digitalWrite(relay2 ,LOW); // Release the magnet holding one side down
delay(120000); // Wait 2 min for selfies.
while(digitalRead(start ));}
// this section causes the relays to click like a slowing down spring
else if (currentTym - startTym >= onTym) //test whether the onTym has elapsed
{
digitalWrite(ticker1, LOW);
digitalWrite(ticker2, LOW);
if (currentTym - startTym >= cycleTym) //test whether the cycleTym has elapsed
{
digitalWrite(ticker1, HIGH);
digitalWrite(ticker2, HIGH);
offTym = offTym + sq(offTym)/200000; //This is my formula for making the off time longer while keeping the on time the same
cycleTym = onTym + offTym;
startTym = currentTym; //IMPORTANT to save the start time of the current LED state.
// this section counts down the LED string
if (endTym-currentTym >= endTym * .01) { pixels.setPixelColor(0, pixels.Color(255, 0, 255)); // Purple on pixel 0
}
else { pixels.setPixelColor(0, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .10) { pixels.setPixelColor(1, pixels.Color(255, 0, 0)); // Red on pixel 1
}
else { pixels.setPixelColor(1, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .16) { pixels.setPixelColor(2, pixels.Color(255, 0, 0)); // Red on pixel 2
}
else { pixels.setPixelColor(2, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .21){ pixels.setPixelColor(3, pixels.Color(255, 165, 0)); // Orange on pixel 3
}
else { pixels.setPixelColor(3, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .26){ pixels.setPixelColor(4, pixels.Color(255, 165, 0)); // Orange on pixel 4
}
else { pixels.setPixelColor(4, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .31){ pixels.setPixelColor(5, pixels.Color(255, 165, 0)); // Orange on pixel 5
}
else { pixels.setPixelColor(5, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .37){ pixels.setPixelColor(6, pixels.Color(255, 255, 0)); // yellow on pixel 6
}
else { pixels.setPixelColor(6, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .42){ pixels.setPixelColor(7, pixels.Color(255, 255, 0)); // yellow on pixel 7
}
else { pixels.setPixelColor(7, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .47){ pixels.setPixelColor(8, pixels.Color(255, 255, 0)); // yellow on pixel 8
}
else { pixels.setPixelColor(8, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .52){ pixels.setPixelColor(9, pixels.Color(255, 255, 0)); // yellow on pixel 9
}
else { pixels.setPixelColor(9, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .58){ pixels.setPixelColor(10, pixels.Color(96, 164, 114)); // Green on pixel 10
}
else { pixels.setPixelColor(10, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .63){ pixels.setPixelColor(11, pixels.Color(96, 164, 114)); // Green on pixel 11
}
else { pixels.setPixelColor(11, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .68){ pixels.setPixelColor(12, pixels.Color(96, 164, 114)); // Green on pixel 12
}
else { pixels.setPixelColor(12, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .73){ pixels.setPixelColor(13, pixels.Color(96, 164, 114)); // Green on pixel 13
}
else { pixels.setPixelColor(13, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .79){ pixels.setPixelColor(14, pixels.Color(96, 164, 114)); // Green on pixel 14
}
else { pixels.setPixelColor(14, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .84){ pixels.setPixelColor(15, pixels.Color(96, 164, 114)); // Green on pixel 15
}
else { pixels.setPixelColor(15, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .89){ pixels.setPixelColor(16, pixels.Color(96, 164, 114)); // Green on pixel 16
}
else { pixels.setPixelColor(16, pixels.Color(0, 0, 0));} // pixel off
if (endTym-currentTym >= endTym * .94){ pixels.setPixelColor(17, pixels.Color(96, 164, 114)); // Green on pixel 17
}
else { pixels.setPixelColor(17, pixels.Color(0, 0, 0));} // pixel off
pixels.show();
}
}
}
}
