In this script below , a counter is there with Up & Down key (I designed)
Its working fine but with a small change in desired result
A display is there to show the count with order and speed . . .
Actually I applied 3 functions here :
Button 4 for Count Up (with delay of 300ms)
Button 5 for Count Down (with delay of 300ms)
While Pressing and Holding button 4 , press 5 => Count Up (with delay 100ms)
While Pressing and Holding button 5 , press 4 => Count Down (with delay 100ms)
So from above 4 cases all the 3 are working correctly but , the 4th case is working exactly like 3rd case
It means the counter starts counting in Up direction with more speed but I want it to make it count
Downwards with more speed
As a conclusion Case 3 & Case 4 actions are all the way same
Please help me in solving this one . . .
const byte IN4 = 4;
const byte IN5 = 5;
boolean readValue4;
boolean readValue5
int click = 0;
#include <TM1637Display.h>
#define CLK 3
#define DIO 2
TM1637Display display = TM1637Display(CLK, DIO);
void setup() {
Serial.begin(9600);
}
void loop() {
display.setBrightness(7);
if (click == 1)
{
display.showNumberDec(1);
}
else if (click == 2)
{
display.showNumberDec(2);
}
else if (click == 3)
{
display.showNumberDec(3);
}
else if (click == 4)
{
display.showNumberDec(4);
}
else if (click == 5)
{
display.showNumberDec(5);
}
else if (click == 6)
{
display.showNumberDec(6);
}
else if (click == 7)
{
display.showNumberDec(7);
}
else if (click == 8)
{
display.showNumberDec(8);
}
if (!digitalRead(IN4) && !digitalRead(IN5)) // 1+
{
if (click < 8)
{
click = click + 1;
}
else
{
click = 1;
}
delay(100);
}
else if (!digitalRead(IN5) && !digitalRead(IN4)) // 1-
{
if (click > 1)
{
click = click - 1;
}
else
{
click = 8;
}
delay(100);
}
else if (!digitalRead(IN4)) // 1+
{
if (click < 8)
{
click = click + 1;
}
else
{
click = 1;
}
delay(300);
}
else if (!digitalRead(IN5)) // 1-
{
if (click > 1)
{
click = click - 1;
}
else
{
click = 8;
}
delay(300);
}
}
PaulRB
March 11, 2023, 12:14pm
2
King_RAJ_Enters:
!digitalRead(IN4)
How are your buttons wired? I noticed that you do not use INPUT_PULLUP. Are you using external pull-up resistors?
These two if conditions are, in practice, identical. Checking the buttons in a different order will not distinguish which button was pressed first and which second. They both detect only that both buttons are pressed. You need a smarter check than this!
Ya INPUT PULL_UP is there . . . I forgot to mention here but in original program its there . .
Well coming to the smarter check as you said , how to do that thing . . In what way ???????????
ec2021
March 11, 2023, 4:49pm
4
Hi @King_RAJ_Enters ,
first if you do not want to bother with debouncing you might take a look at the library ezButton
https://arduinogetstarted.com/library/button/example/arduino-multiple-button-all
I think that it is highly advisable to avoid stumbling into the problems of bouncing!
The second is that if you want the sketch to react differently depending on which button (4 or 5) has been pressed first, follow this pseudo code:
Start loop
check buttons 4 and 5
while button 4 is pressed
check button 5
if 5 is pressed Count Up (every 100 ms)
while button 5 is pressed
check button 4
if 4 is pressed Count Down (every 100 ms)
End loop
Good luck and have fun!
ec2021
PaulRB
March 11, 2023, 5:24pm
5
INPUT_PULLUP is not used in the code you posted, I checked that. It doesn't matter if it was used in some other code you have not shared, if the code you upload does not use INPUT_PULLUP and you have no external pull-up resistors, then your inputs will be floating and your code will not work reliably.
If you want help with some code but then post some other code, you are wasting everybody's time.
Yeah its there in same code I just forgot to copy that setup part , nothing else !!
const byte IN4 = 4;
const byte IN5 = 5;
boolean readValue4;
boolean readValue5
int click = 0;
#include <TM1637Display.h>
#define CLK 3
#define DIO 2
TM1637Display display = TM1637Display(CLK, DIO);
void setup() {
Serial.begin(9600);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
}
void loop() {
display.setBrightness(7);
if (click == 1)
{
display.showNumberDec(1);
}
else if (click == 2)
{
display.showNumberDec(2);
}
else if (click == 3)
{
display.showNumberDec(3);
}
else if (click == 4)
{
display.showNumberDec(4);
}
else if (click == 5)
{
display.showNumberDec(5);
}
else if (click == 6)
{
display.showNumberDec(6);
}
else if (click == 7)
{
display.showNumberDec(7);
}
else if (click == 8)
{
display.showNumberDec(8);
}
if (!digitalRead(IN4) && !digitalRead(IN5)) // 1+
{
if (click < 8)
{
click = click + 1;
}
else
{
click = 1;
}
delay(100);
}
else if (!digitalRead(IN5) && !digitalRead(IN4)) // 1-
{
if (click > 1)
{
click = click - 1;
}
else
{
click = 8;
}
delay(100);
}
else if (!digitalRead(IN4)) // 1+
{
if (click < 8)
{
click = click + 1;
}
else
{
click = 1;
}
delay(300);
}
else if (!digitalRead(IN5)) // 1-
{
if (click > 1)
{
click = click - 1;
}
else
{
click = 8;
}
delay(300);
}
}
Kk sir le me check . . ll Update you !!
PaulRB
March 11, 2023, 7:04pm
8
Again, you are posting code you have not even tested on the arduino. Please do not waste our time like this!
ec2021
March 11, 2023, 7:25pm
9
I second @PaulRB 's post ...
Anyway, here is an example (which I tested and it works ...) how to use ezButton and to solve the specific task:
#include "ezButton.h"
const byte IN4 = 4;
const byte IN5 = 5;
ezButton but4(IN4);
ezButton but5(IN5);
signed long count = 0;
void setup() {
Serial.begin(115200);
Serial.println("Start");
}
void Refresh(){
but4.loop();
but5.loop();
}
void loop(){
Refresh();
while (but5.getState() != 1){
Refresh();
if (but4.getState() !=1){
count--;
Serial.println(count);
delay(100);}
}
while (but4.getState() != 1){
Refresh();
if (but5.getState() !=1){
count++;
Serial.println(count);
delay(100);}
}
}
Instead of the delays a millis()-function will be better, but it should do it for this example.
ec2021
Yesss its already tested a month ago directly on Arduino . . . .
So many other scripts get added after this for other included functions but that is not a part of this thread . . . .
Important matter is the target to make it count up and down with proper press of button sequence as rest of previous things are already done with it
This small Up Down issue is going on or else count can also be proceeded in forward manner
But I planned to make an operation fully applicable for a proper order of functions . . .
So discussing this thread !!!
PaulRB
March 11, 2023, 8:01pm
11
You are asking for help with code that you claim to have uploaded and tested on an Arduino. But you post different code.
Twice now you have done this, to my certain knowledge. On the first time you admitted that it what you posted is not the code you tested. The second time, you posted code that I am certain has never been uploaded or tested on an Arduino. How do I know? I can read. I can see errors which would have 100% prevented that code from being uploaded.
Maybe the differences between the code you tested and the code you posted are small and would make no difference. But maybe they are not small, and I will never be able to help you fix them because the code you posted is not the same code at all. I do not know which is these things is true and I have lost all trust in you.
Good luck with your project.
Ya Sir applying it with my script . . ll update you after all the trials possible !!!
ec2021:
millis()
Well what about this millis()-function . . any study link or script example to apply this ??
ec2021
March 11, 2023, 9:30pm
14
Ya sir sure !!
Sir a very typical type issue is here
Code is at least working on simulator but not on arduino mega practically
I mean buttons are not functioning any of the way . .
Just the code gets load only and nothing is happening
Sir for dual press operation , the function is going too smooth and all the way OK
Now what about single press for 4 or 5 for Up or Down count ??
I am trying but not able to make it run automatically . . .
Well Sir . . Tried with all types of de bounce scripting but unable to design a single press and simple count UP
Please help me in this one
with this code it just Count UP while pressing & releasing manually each time repeatedly unlike the actual function
//Refresh();
if (!digitalRead(IN4))
{
count++;
Serial.println(count);
delay(300);
}
That's finally an option I had . .
No effect of delay , nothing !!!
ec2021
March 12, 2023, 7:46am
19
It ist hard to guess why things go wrong if you post only parts of the whole sketch... Please post always the complete code ...
To react on single button use, you have to distinguish between
Use of B5 while B4 is pressed
Use of B4 while B5 is pressed
Use of B5 alone
Use of B4 alone
The third and fourth use can either be detected
after the buttons have been released without any use of the other button or
if the respective button is pressed for a certain time without pressing the other one
ec2021
Fresh and Clear code is here which I am running on simulator online
#include "ezButton.h"
const byte IN4 = 4;
const byte IN5 = 5;
ezButton but4(IN4);
ezButton but5(IN5);
signed long count = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start");
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
}
void Refresh(){
but4.loop();
but5.loop();
}
void loop(){
if (!digitalRead(IN4))
{
count++;
Serial.println(count);
delay(300);
}
if (!digitalRead(IN5))
{
count--;
Serial.println(count);
delay(300);
}
Refresh();
while (but4.getState() != 1)
{
Refresh();
if (but5.getState() !=1){
count++;
Serial.println(count);
delay(100);}
}
while (but5.getState() != 1)
{
Refresh();
if (but4.getState() !=1)
{
count--;
Serial.println(count);
delay(100);
}
}
}