ledstate reset button problem

Hi everyone my plan was to 2 pushbutton set reset led but I failed doing the reset part. when I push the button once led should remain lit and when I pressed the reset button it should go off. My problem is the last else statement doesnt work. Appreciate for any help.

const int ledpin=11;
const int buttonpin=1;


const int resetpin=3;

int resetstate=0;
int resetcount=0;
int lastreset=0;



int buttonstate=0;
int  buttonPushCounter_one=0;
int lastbuttonstate=0;

void setup() {
  // put your setup code here, to run once:
pinMode(ledpin,OUTPUT);
pinMode(buttonpin,INPUT);
pinMode(resetpin,INPUT);
}





void button()

{

buttonstate=digitalRead(buttonpin);


if (buttonstate!=lastbuttonstate)
{
if (buttonpin==HIGH)
{
 buttonPushCounter_one++;

  
}

lastbuttonstate = buttonstate;

 delay(10);
}

}


///////////////////////////////

void reset()

{

resetstate=digitalRead(resetpin);

if (resetstate!=lastreset)
{
if (resetpin==HIGH)
{
 resetcount++;

  
}

lastreset = resetstate;

 delay(10);
}

}



////////////////////////////////
void loop() {

button();
delay(20);

reset();

if (resetcount%2==0)
{
 
  buttonPushCounter_one % 2 != 0;

  
}



if (buttonPushCounter_one % 2 == 0) 
              {
               digitalWrite(ledpin, HIGH);
              } 

else {
  digitalWrite(ledpin, LOW);
  
  }




}

Because you make them INPUT, you have external resistors? Easier to just put the button between GND and a pin and use INPUT_PULLUP.

Problem is

  if (buttonPushCounter_one % 2 == 0)
  {
    digitalWrite(ledpin, HIGH);
  }

Once that is true, it doesn't become false until you press the button again. Because

    buttonPushCounter_one % 2 != 0;

does nothing. It just checks if the 2 modulus of buttonPushCounter_one is not 0 and does nothing with the answer :wink:

Some tips

Fix the layout a bit. Press Ctrl+T in the IDE and remove some black lines.

Remove all the crappy delays. They will bit you later because it's the same as telling the Arduino to put it's head into the sand.

Use more useful function names. reset() isn't a good name. reset what? checkResetButton is. And more general, button is a bad name as well. The reset button is a button as well. Call them reset button and set button :slight_smile:

Make variable names more readable. It's kind of the standard to camelCaseVariableNames. So no underscores and add some caps to indicate the separate words in a variable name. So ledPin, buttonPin etc

Use the smallest variable you can use. So for pin numbers it's a byte. The const is great! But a lot like to write the variable with a starting Capital then to remind us its const. So LedPin and ButtonPin (or actually SetButtonPin and ResetButtonPin).

Make life easy and grab a library like Bounce2 to deal with all the button stuff :wink:

And instead of doing the modulus, just reset the counter when needed. Faster and no problems on roll over.

Use the smallest variable you can use. So for pin numbers it's a byte

I agree with this advice and advocate it myself, but there was some discussion here recently about digitalWrite() promoting the pin number to an int thus nullifying the effect of declaring the pin number as a byte.

Personally I will still be advocating the use of const byte for pin number unless/until an Arduino with more than 256 digital pins is in wide use.

UKHeliBob:
about digitalWrite() promoting the pin number to an int thus nullifying the effect of declaring the pin number as a byte.

That might be, but that would be only at the moment of calling for that single variable.

Test with simple sketch

byte leds[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

void setup(){
  for(byte i = 0; i < sizeof(leds)/sizeof(leds[0]); i++){
    pinMode(leds[i], OUTPUT);
  }
}
void loop(){
  for(byte i = 0; i < sizeof(leds)/sizeof(leds[0]); i++){
    digitalWrite(leds[i], HIGH);
    delay(1000);
    digitalWrite(leds[i], LOW);
  }
}

Changing the variable type:
byte => ROM 1110, RAM 23
int => ROM 1128, RAM 37
const byte => ROM 1104, RAM 23
const int => ROM 1122, RAM 37

So using a byte does save RAM and ROM :slight_smile:

So using a byte does save RAM and ROM

As I would expect. In the thread in which the subject arose I think the point being made was that using ints meant that the code would run faster because the byte would not need to be promoted to an int. Of course, for most programs that loss of speed would not matter and anyway, if speed were a problem the Arduino functions would not be used in any case.

I don't expect that to be a thing as well. Promoting it to an int is as simple as copying the byte into the LSB register and clear the MSB. Signed to signed is a bit harder though because then you have to mess with the sign bit.

And in this case the program size seems to reflect it.* The programs with bytes is smaller then the one with int's.

*Yes, I now, program size and program speed are nor really related but in a simple program as this which just runs the same loop over and over the amount of code does say something about the speed I think.

We agree that byte is better, or at least no worse and it certainly makes more sense when pin numbers are never larger than 255.

Out of interest, this is the thread in which this was recently discussed.
https://forum.arduino.cc/index.php?topic=441134.0

thanks but these doesnt solve my problem. I can turn on the led but when I pressed the reset button it doesnt work. should I put a return buttonPushCounter_one inside the if statement

If you have modified your code, post the modified version.
Near impossible for us to comment on your code while it remains invisible.

Guys I tried with bounce library and I still cant managed to do the reset part. I am so demotivated I cant even do a simple project. Here is the code

#include <Bounce2.h>

byte buttonPin = 3;
byte ledPin = 11;
byte resetPin = 1;


Bounce bouncer_one = Bounce();   //bouncer for on button LED
Bounce bouncer_two = Bounce();   //bouncer for reset button LED

void setup() {

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(resetPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  bouncer_one.attach(buttonPin);   //pin definitons for bouncers
  bouncer_two.attach(resetPin);


}

void loop() {
  bouncer_one.update();
  bouncer_two.update();

  if (bouncer_one.rose())         //turns LED on after first rising edge and stays on
  {
    digitalWrite(ledPin, HIGH);
  }

  if ((bouncer_two.rose()) && (ledPin == HIGH)) // turns of the led when reset button pushed and led is on BUT DOESNT WORK I DONT KNOW WHY?

  {
    digitalWrite(ledPin, LOW);
  }

}

I did by removing the && (ledPin == HIGH) part it works now. thanks

Three things:

(ledPin == HIGH)

Do you ever expect that to be true? :wink: ledPin is defined as 11. And HIGH as a 1. (11 == 1) will always give you false. Did you mean to read the pinstate by any change :wink: And indeed, what does it matter if the led is on or off? You don't check it to be off when you turn it on. Why would it hurt to turn it off when it's already off?

And why do you check for the rising edge? That will happen when you release the button. Not saying it's wrong, might be what you want. Just saying.

And last. bouncer_one and bouncer_two are obviously terrible terrible names. Even more terrible then buttonPin and resetPin (poor reset is a button as well!). I would suggest setButtonPin, resetButtonPin, setButton and resetButton as variable names. Now it's clear from the name what is is :slight_smile: