Arduino variables

Hi! I have an Arduino Uno. I want that when i press a button a led turn on and if a press again it, it turn off. This is my code

const int bottone = A0; int valoreBottone = 0; int precedente = 0; void setup(){ pinMode(2, OUTPUT); Serial.begin(9600); }

void loop(){ valoreBottone = analogRead(bottone); if(valoreBottone != 0){ if(precedente == 0){ on();

} if(precedente == 1) { off(); } } delay(500); } void on(){ digitalWrite(2, HIGH); precedente = 1; } void off(){ digitalWrite(2, LOW); precedente = 0; }

when i press the button, the led begin to blink. I have tried to send the var. "precedente" to the PC and the variables doesn't change! If in void loop i write "precedente = 1;" without an if or a code it does change.. Can you help me? sorry for my english, I'm italian

You need to debounce the input and then look for state changes. You should also be using digital read not analog, and your external circuit can be simplified if you use the internal pull-up resistor on the input pin. Look at the state change detection example sketch.

You need to debounce the input

I imagine that's what the half-second delay is for.

I imagine that's what the half-second delay is for.

That's after the fact.

I see you have:

    if(precedente == 0){
      on();
     
    }
    if(precedente == 1) {
      off();
    }

and then:

void on(){
  digitalWrite(2, HIGH);
  precedente = 1;
}
void off(){
  digitalWrite(2, LOW);
  precedente = 0;
}

This expands to:

    if(precedente == 0){
      // code for on() function
      digitalWrite(2, HIGH);
      precedente = 1;
    }
    // at this point, precedente cannot be 0 (think about it!)
    if(precedente == 1) {
      // code for off() function
      digitalWrite(2, LOW);
      precedente = 0;
    }

The variable doesn't change!!!!!!! I've tried with this code. const int pulsante = A0; int val = 0; int precedente = 0;

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

void loop() { // put your main code here, to run repeatedly: if(val != 0){ if(precedente == 0){ precedente = 1; } } if(val != 0){ if(precedente == 1){ precedente = 0; } } if(precedente == 0){ digitalWrite(2, LOW); } if(precedente == 1){ digitalWrite(2, HIGH); } }

Please use code tags.

Read your code again, carefully.

Start by realizing that you don’t want to know what the switch’s state is; you want to know when the switch transitions from HIGH to LOW (or LOW to HIGH, depending on wiring).

Can you post a code?

PaoloPescatore: Can you post a code?

Code is available in the State Change Detection example.

Try something like this:

void loop(){
  valoreBottone = digitalRead(bottone);
                                      // need debounce code, or a delay and read it again
  if(valoreBottone != 0)
{
    precedente =  (precedente ^ 1);
    digitalWrite(2, precedente);
}
  
  delay(500);
}

But that examlpe isn't that i want to do, because I have a number limited of bytes free on my code :(

PaoloPescatore: But that examlpe isn't that i want to do,

You're not always going to find code the fits perfectly to your requirements; that where you get to actually use your brain and figure out how to apply the concepts demonstrated in the examples, to your code.

PaoloPescatore: because I have a number limited of bytes free on my code :(

I have no idea what that means