1
\$\begingroup\$

I have 3 push buttons, 3 leds, and 2 relays. When you push button1 once, the led1 turns on and timer is set to 10 sec. When push button1 twice, led2 turns on and timer is set to 20 sec. When push second button once, select first relay. When pushs second button twice, then select second relay. And in the end start button (button3) to start saved relays and led3 for time that we before set.

code:

#define BTN1_PIN 3 #define BTN2_PIN 4 #include <GyverButton.h> GButton butt1(BTN1_PIN); GButton butt2(BTN2_PIN); int relay1=8; int relay2=9; int led=5; int led2=6; int led3=7; int butt3=5; int secmek=0; int gozlemek=0; void setup() { Serial.begin(9600); butt1.setDebounce(150); butt2.setDebounce(150); butt1.setClickTimeout(500); butt2.setClickTimeout(500); pinMode(relay1, OUTPUT); digitalWrite(relay1, LOW); pinMode(relay2,OUTPUT); digitalWrite(relay2, LOW); pinMode(led,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); } void loop() { digitalWrite(led,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); digitalWrite(relay1,LOW); digitalWrite(relay2,LOW); butt1.tick(); butt2.tick(); if(butt1.hasClicks()){ byte clicks = butt1.getClicks(); switch(clicks){ case 1: digitalWrite(led, HIGH); gozlemek=10000; break; case 2: digitalWrite(led2,HIGH); digitalWrite(led,LOW); gozlemek=20000; break; } } if(butt2.hasClicks()){ byte clicks = butt2.getClicks(); switch(clicks){ case 1: secmek=1; break; case 2: secmek=2; break; case 3: secmek=3; break; } } if(butt3 = HIGH){ digitalWrite(led3,HIGH); switch(secmek){ case 1: digitalWrite(relay1, HIGH); break; case 2: digitalWrite(relay2,HIGH); digitalWrite(relay1,LOW); break; case 3: digitalWrite(relay1, HIGH); digitalWrite(relay2, HIGH); break; } delay(gozlemek); } } 
\$\endgroup\$
3
  • 1
    \$\begingroup\$Please post 'GyverButton.h' as well, assuming you wrote that include.\$\endgroup\$
    – Mast
    CommentedApr 13, 2021 at 20:02
  • \$\begingroup\$github.com/AlexGyver/GyverLibs/blob/master/GyverButton/… here you are\$\endgroup\$
    – Sha'an
    CommentedApr 13, 2021 at 20:06
  • \$\begingroup\$Aren't you using gcc/g++ since it's Arduino? Then start with fixing all the warnings. Also, knowing which programming language you are using helps when programming. Arduino is generally C++, so if you ask for a C review it might be irrelevant, since the two languages are very different nowadays.\$\endgroup\$
    – Lundin
    CommentedApr 14, 2021 at 11:56

1 Answer 1

1
\$\begingroup\$

Unfortunately, it looks like your code won't work.

The last condition:

if(butt3 = HIGH){ 

will always be true.

This is a common mistake in C and C++, missing one = in a condition, turning it in to an assignment.

A useful trick to avoid this is always put the constant first, then the compiler will catch the error for you:

if (HIGH == butt3) { 

Another issue is logic:

Until the 3rd button is pressed, your loop will run constantly, shutting down all LEDs over and over every fraction of a second, so you will likely never see them light up, or see a horrible flicker at best.

You need a way to save the state, or only reset the LEDs after the delay timer runs out.

Finally, a few points on code cleanliness:

  • Use defines for pin numbers and give them proper names:

Like so:

#define RELAY1_PIN 8 #define RELAY2_PIN 9 #define LED1_PIN 5 #define LED2_PIN 6 #define LED3_PIN 7 #define START_BTN_PIN 5 

This has several advantages:

  1. It saves RAM by not creating variables, and there is very little RAM on most Arduinos.

  2. Capitalization makes it clear that these are constants.

  3. The names are more descriptive so your code is easier to understand.

  4. There is no danger you will accidentally change pin numbers somewhere in the code.

  • Name all variables in English.
    It may be tempting to use words from your own language, but it makes the code less readable, and harder to help you if you post it on the internet.

I doubt anyone reading this post will know what gozlemek and secmek mean.
Even people who do speak your language, may find your spelling hard to understand if your language actual alphabet is different and you need to make up "latinized" spelling.

  • Keep your spaces uniform.
    It is a good practice to put in a proper amount of spacing to make different parts of the code easily identifiable to humans.

The Arduino and the compiler won't care.

So, instead of:

if(butt2.hasClicks()){ 

write:

if (butt2.hasClicks()) { 

and instead of:

digitalWrite(led2,HIGH); digitalWrite(led,LOW); gozlemek=20000; 

write:

digitalWrite(led2, HIGH); digitalWrite(led, LOW); gozlemek = 20000; 
  • Don't "translate" if you don't have to.

This switch statement is not needed:

byte clicks = butt2.getClicks(); switch(clicks){ case 1: secmek=1; break; case 2: secmek=2; break; case 3: secmek=3; break; } 

because the value you want in secmec is the same as the value switch is checking.

Just do this:

secmek = butt2.getClicks(); if (secmek > 3) secmek = 0; //safeguard against too many clicks 
\$\endgroup\$
11
  • \$\begingroup\$Down-voted for "Yoda condition" nonsense, we don't live in the 1980s any longer. The solution is not to write if(HIGH == butt3), don't teach people that. The solution is to upgrade to a compiler such as Turbo C from 1989 or later. It will then warn "possible incorrect assignment" or "assignment inside condition". If your compiler doesn't do this, then the root of the problem is that you are using a compiler which is utter garbage, even worse than TC from year 1989.\$\endgroup\$
    – Lundin
    CommentedApr 14, 2021 at 11:53
  • \$\begingroup\$@Lundin have you ever worked with the Arduino IDE? I guess not, because you would not be writing this or down voting me. The whole concept around Arduino development, their code style guides, their current (not 2.0 beta) IDE is for people who are not interested in programming but more in building. People who will ignore compiler warnings. In fact, it takes some effort to see those warnings in the IDE. They are hidden by default. Also, what do you have against Yoda? You hate baby Yoda as well? ;P\$\endgroup\$
    – Lev M.
    CommentedApr 14, 2021 at 20:15
  • \$\begingroup\$Well, this site is about programming. Simply don't teach people bad practices that went obsolete over 30 years ago.\$\endgroup\$
    – Lundin
    CommentedApr 15, 2021 at 6:36
  • \$\begingroup\$@Lundin care to explain why this practice is actually bad, other then you aesthetically disliking it? Where is the technical, measurable downside? This practice will ensure a problematic mistake, to which novice programmers are particularly prone, will stop compilation regardless of what compiler is used, or what the compiler settings are. It will not generate bad assembly and it will not make the code less readable, so aside from giving you the excuse to bring up some nostalgia, what is the problem exactly?\$\endgroup\$
    – Lev M.
    CommentedApr 15, 2021 at 11:05
  • \$\begingroup\$@Lundin P.S. this site is about helping people improve their code with constructive suggestions that actually work. I don't see you making any so far.\$\endgroup\$
    – Lev M.
    CommentedApr 15, 2021 at 11:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.