#include <TM1637Display.h>
// define the connections pins
#define CLK 9
#define DIO 10
#define CLKb 7
#define DIOb 8
int k = 0;
// create a display object of type TM1637Display
TM1637Display displayA = TM1637Display(CLK, DIO);
TM1637Display displayB = TM1637Display(CLKb, DIOb);
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
pinMode(A4, INPUT);
displayA.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
displayA.showNumberDec(0000, true, 4, 0);
displayB.setBrightness(7);
displayB.showNumberDec(0000, true, 4, 0);
wait(); //Calling wait function. If this doesn't break, the program will stay at this line
int msec;
msec = millis(); //Msec is the time we started after hitting a button
randomSeed(msec); //Startin point in generating random numbers
Serial.begin(9600);
}
void wait()
{
while (1)
{
int data = analogRead(A4);
{
if (data > 500 )//Both switches are active 1 so once press, they go 0.
break;
}
if (data > 500 )//Both switches are active 1 so once press, they go 0.
break;
}
}
void loop()
{
static int save; //intialize save once
static int score = -1; //Scores starting point
static int elapsed = 0; //Refernce point to when game started
elapsed = millis() - save; //Elasped = from when we started to current time - when we started
if ( score == -1) //Score starts as -1 so this if starts
{
save = millis(); //Save will get it's start time
score = 0; //Score is set to 0
}
if ( elapsed <= 60000 ) //Once our current time running on is equal to 15 seconds
{
// read the pushbutton input pin:
int i;
for (i = 0; i < 610; i++) {
displayA.showNumberDec(i, true, 4, 5);
delay(53);
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print(buttonPushCounter);
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
displayB.showNumberDec(buttonPushCounter, true, 4, 0);
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}
if ( elapsed > 60000 ) //Once our current time running on is equal to 15 seconds
{
buttonPushCounter = 0;
displayB.clear();
displayB.setBrightness(7);
displayB.showNumberDec(0000, true, 4, 0);
displayA.clear();
displayA.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
displayA.showNumberDec(0000, true, 4, 0);
wait(); //Wiating function to start again
score = -1; //Set score back to -1
}
}
Hello, do yourself a favour and please read How to get the best out of this forum and post accordingly (including code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).
Please edit your post to add code tags, as described in the instructions "How to get the best out of this forum".
Always use variables declared unsigned long with for millis() and micros().
Didn't solve the problem.
changing
to
static unsigned long elapsed = 0;
didn't fix at least one problem?
also change save and msec to unsigned long int.
also, elapsed.
I did this but now the display is flashing when I push the button
I've never done forums before-
#include <TM1637Display.h>
// define the connections pins
#define CLK 9
#define DIO 10
#define CLKb 7
#define DIOb 8
int k = 0;
// create a display object of type TM1637Display
TM1637Display displayA = TM1637Display(CLK, DIO);
TM1637Display displayB = TM1637Display(CLKb, DIOb);
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup()
{
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
pinMode(A4, INPUT);
displayA.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
displayA.showNumberDec(0000, true, 4, 0);
displayB.setBrightness(7);
displayB.showNumberDec(0000, true, 4, 0);
wait(); //Calling wait function. If this doesn't break, the program will stay at this line
unsigned long msec;
msec = millis(); //Msec is the time we started after hitting a button
randomSeed(msec); //Startin point in generating random numbers
Serial.begin(9600);
}
void wait()
{
while (1)
{
int data = analogRead(A4);
{
if (data > 500 )//Both switches are active 1 so once press, they go 0.
break;
}
if (data > 500 )//Both switches are active 1 so once press, they go 0.
break;
}
}
void loop()
{
static unsigned long elapsed = 0; save; //intialize save once
static int score = -1; //Scores starting point
static unsigned long elapsed = 0; //Refernce point to when game started
elapsed = millis() - save; //Elasped = from when we started to current time - when we started
if ( score == -1) //Score starts as -1 so this if starts
{
save = millis(); //Save will get it's start time
score = 0; //Score is set to 0
}
if ( elapsed <= 60000 ) //Once our current time running on is equal to 15 seconds
{
// read the pushbutton input pin:
int i;
for (i = 0; i < 610; i++) {
displayA.showNumberDec(i, true, 4, 5);
delay(53);
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print(buttonPushCounter);
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
displayB.showNumberDec(buttonPushCounter, true, 4, 0);
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}
if ( elapsed > 60000 ) //Once our current time running on is equal to 15 seconds
{
buttonPushCounter = 0;
displayB.clear();
displayB.setBrightness(7);
displayB.showNumberDec(0000, true, 4, 0);
displayA.clear();
displayA.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest)
displayA.showNumberDec(0000, true, 4, 0);
wait(); //Wiating function to start again
score = -1; //Set score back to -1
}
}
If it is too much trouble for you to read and follow the forum instructions, it si too much trouble for us to bother with your post.
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
1)press Ctrl-T for autoformatting your code
2)do a rightclick with the mouse and choose "copy for forum"
3)paste clipboard into write-window of a posting
post 2 and 3 already asked you to follow the forum's guidelines for posting code.
please click on the pencil below your 2 infringing posts to edit them and add code tags.
ok I fixed them sorry about that...
When you guys said to tag it I was thinking of a different type of tagging.
if ( elapsed <= 60000 )
if ( elapsed >60000 )
Try adding UL after the 60000 constants.
By default, an integer constant is treated as an int with the attendant limitations in values. To specify an integer constant with another data type, follow it with:
- a 'u' or 'U' to force the constant into an unsigned data format. Example: 33u
- a 'l' or 'L' to force the constant into a long data format. Example: 100000L
- a 'ul' or 'UL' to force the constant into an unsigned long constant. Example: 32767ul
https://www.arduino.cc/reference/en/language/variables/constants/integerconstants/
not exactly - the arduino doc is not precise there.
if there is no suffix, the type of the integer literal is the first type in which the value can fit, from the list of types which depends on which numeric base was used
if the integral literal was expressed in decimal (base 10) like 60000 then the compiler will assign the type int if it fits, otherwise long int otherwise long long int (since C++11).
if you use Binary (starts with 0b), octal (starts with 0), or hexadecimal (starts with 0x) bases then the compiler will take the smallest type fitting the value in this ordered list int, unsigned int, long int, unsigned long int, long long int (since C++11), and unsigned long long int (since C++11)
see Integer literal - cppreference.com
what is usually a problem is if you start doing some maths like 60 * 1000 as both 60 and 1000 fit on an int and so the math will be completed as an int value and 60,000 does not fit and you'll end up with the wrong result. That's when using the right suffix (l, u, ul, ll, ull) really matters. Of course it's a good habit to use the suffix always (esp. if you use #define and not typed variables) - it shows you understand or at least are aware of this topic..
Your code indentation is atrocious. Please auto-format it using the Arduino IDE.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.