hi, i need help, i don't know why my code doesn't work, i would like when i click a button so the number on the display changes to TM1637 but it doesn't work? (Sorry for my English, I don't speak English well)
#include <TM1637.h>
int button = 4;
int buttonstate = 0;
int CLK = 2;
int DIO = 3;
TM1637 tm(CLK, DIO);
void setup() {
// put your setup code here, to run once:
tm.init();
pinMode(button, INPUT);
//set brightness; 0-7
tm.set(2);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate = digitalRead(button);
if (button == HIGH)
displayNumber(10.00);
else if (button == HIGH);
displayNumber(11.25);
if (button == HIGH)
displayNumber(12.50);
else if (button == HIGH);
displayNumber(13.75);
}
void displayNumber(int num) {
tm.display(3, num % 10);
tm.display(2, num / 10 % 10);
tm.display(1, num / 100 % 10);
tm.display(0, num / 1000 % 10);
}
That is an odd use of an if / then / else if / else statement where each condition is exactly the same.
I assume that you want, on each subsequent button press, that the next number in the series is displayed.
I looked at StateChangeDetection I tried to convert it for my code but it doesn't work or I don't know how I can set it to change the number from 10.00 = 11.25 = 12.50 = 13.75 with each click
#include <TM1637.h>
int button = 4;
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
int CLK = 2;
int DIO = 3;
TM1637 tm(CLK, DIO);
void setup() {
// put your setup code here, to run once:
tm.init();
pinMode(button, INPUT);
//set brightness; 0-7
tm.set(2);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(button);
buttonState = digitalRead(button);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
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;
}
if (buttonPushCounter % 9 == 0)
{
displayNumber(10.00);
} else {
displayNumber(11.25);
}
void displayNumber(int num) {
tm.display(3, num % 10);
tm.display(2, num / 10 % 10);
tm.display(1, num / 100 % 10);
tm.display(0, num / 1000 % 10);
}
In this case, part 1 is to increment a counter each time a button is pressed:
0, 1, 2, 3, 0, 1, 2 ......
Modify the first part of your code to do this and, for testing, write the value of the counter to the serial monitor.
Part 2 is to display a number on the display depending on the value of the counter. That is simply a big if / then / else if / else (or switch) statement.
There may be other, equally valid, ways of solving this.
Modify the first part of your code to do this and, for testing, write the value of the counter to the serial monitor.
Come back when you have tried this and whether it works or not post your code. Then you can move on to translating the count to another number of your choice
I tried the test, nothing works, at least my code doesn't work and I don't know why
int button = 2;
int buttonstate = 0;
int led = 13;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate == digitalRead(button);
switch (buttonstate) {
case 1:
{
digitalWrite(led, HIGH);
// statements
break;
}
case 2:
{
digitalWrite(led, LOW);
// statements
break;
}
{ case 3:
digitalWrite(led, HIGH);
// statements
default:
break;
}
}
}
You have no code in that sketch to sense that the button has become pressed and nothing to increment a variable
Go back to the StateChangeDetection example. Modify it to print buttonPushCounter each time it changes and take out the code that controls the LED then come back and post your code and describe what it does
#include <TM1637.h>
int button = 4;
int counter = 0; // counter for the number of button presses
int buttonstate = 0; // current state of the button
// previous state of the button
int CLK = 2;
int DIO = 3;
TM1637 tm(CLK, DIO);
void setup() {
// put your setup code here, to run once:
tm.init();
pinMode(button, INPUT);
//set brightness; 0-7
tm.set(5);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonstate = digitalRead(button);
if (buttonstate == HIGH) {
counter++;
delay(100);
Serial.print(counter);
if (counter == 1)
displayNumber(1000);
else if (counter == 2)
displayNumber(1125);
else if (counter == 3)
displayNumber(1250);
else if (counter == 4)
displayNumber(1375);
else if (counter == 5)
displayNumber(1500);
else if (counter == 6)
displayNumber(1625);
else if (counter == 7)
displayNumber(1750);
else if (counter == 8)
displayNumber(1850);
else if (counter == 9)
displayNumber(2000);
else if (counter == 10)
displayNumber(2125);
else if (counter == 11)
displayNumber(2250);
if (counter == 11) counter = 0;
}
}
void displayNumber(int num) {
tm.display(3, num % 10);
tm.display(2, num / 10 % 10);
tm.point(1);
tm.display(1, num / 100 % 10);
tm.display(0, num / 1000 % 10);
}