Have problem regarding my project

Hi guys, i am making a project, basically it consist of 2 buttoms, and 3 leds. basically, at the beggining the first led is on, and the other ones are off... if i pushed the up buttom, it has to turn off the first led, to turn one the second.. and if i pushed the up button once more it has to clear the led one, and the led two, and turn on the third led... and it is the maximum value... now if i pushed the down button, it has to reduce the lights, about, have to turn off the third, and return just the second on... and so, on...

It seems simple... but i am getting crazy, regarding of when i pushed the first time the up button, instead to turn one the 2nd led, it turn on the third, and now with the third led on, if i pushed the down button, it is turning me on the 1st one... so basically the second one is never turning me on!!!!.

It is the code i already made, please help me. Thanks!

ARDUINO CODE

const int botup = 53;
const int botdown = 52;
int dato = 1;

void setup() {
// put your setup code here, to run once:

pinMode (botup, INPUT);
pinMode (botdown, INPUT);
DDRD = 0XFF;
PORTD = dato;
}

void loop() {
// put your main code here, to run repeatedly:

delay(10);
if (digitalRead(botup) == LOW && (dato < 3))
increase();
if (digitalRead(botdown) == LOW && (dato > 1))
decrease();
}

void increase(){
dato = dato*2;
PORTD = dato;
wait:
if (digitalRead(botup == LOW))
goto wait;

}

void decrease(){
dato = dato/2;
PORTD = dato;
wait1:
if (digitalRead(botdown == LOW))
goto wait1;
}

const int botup = 53;
const int botdown = 52;
int dato = 1;


void setup() 
{
    pinMode (botup, INPUT_PULLUP);
    pinMode (botdown, INPUT_PULLUP);
    DDRD = 0XFF;
    PORTD = dato;
}

void loop() 
{      
    if (digitalRead(botup) == LOW && (dato < 4))
        increase();
        
    if (digitalRead(botdown) == LOW && (dato > 1))
        decrease();

     delay(1000);
     
}//loop

void increase()
{
    //dato goes
    // 1 -> 2 -> 4
    dato = dato*2;
    PORTD = dato;
    
    while( digitalRead(botup) == LOW );
           
}

void decrease()
{
    //dato goes
    // 4 -> 2 -> 1
    dato = dato/2;
    PORTD = dato;
    
    while( digitalRead(botdown) == LOW );
    
}

Here's some comments on your code...

const int botup = 53;
const int botdown = 52;
int dato = 1;

void setup() {
  // put your setup code here, to run once:
  
    pinMode (botup, INPUT);
    pinMode (botdown, INPUT);
    DDRD = 0XFF;
    PORTD = dato;
}

void loop() {
  // put your main code here, to run repeatedly:
  
  delay(10);  <---  Changed delay to 1000, otherwise things
                    happen very fast
                    
  if (digitalRead(botup) == LOW && (dato < 3))
    increase();
  if (digitalRead(botdown) == LOW && (dato > 1))
    decrease();    
}

void increase(){
    dato = dato*2; <--- why not do "dato <<= 1"
    PORTD = dato;
wait:                               <--- don't do this. Just use a while()
    if (digitalRead(botup == LOW)) <--|- bad syntax for digitalRead()
    goto wait;  <---------------------+
       
}

void decrease(){
    dato = dato/2; <--- why not do "dato >>= 1"
    PORTD = dato;
wait1:
    if (digitalRead(botdown == LOW)) <-- bad syntax
    goto wait1;
}
wait:

if (digitalRead(botup == LOW))
   goto wait;




No, just no. No C++ program should have goto in it. If you used goto then you did something wrong. Use a while loop if that's what you want:

I have heard/learnt that the use of goto statement in programming is a bad practice unless it is absolutely needed. Now, I am hearing that 'it is forbidden' (No C++ program should have goto in it) to use goto statement in C++ program.

What is so terrible about the goto statement?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Metallor:
What is so terrible about the goto statement?

Nothing really. It is generally a useful indication that you are dealing with something written by a programmer who doesn't understand the language or even the basic principles of program flow. Such programs and possibly programmers should be avoided.

Steve

Metallor:
What is so terrible about the goto statement?

Around the campfire it's said it leads to 'spaghetti' code. Do a web search on "don't use goto" - lots of hits.

If you are going to waste spend time on this, go to (see what I did there?) the classic statement on the matter, "Go To Statement Considered Harmful".

a7