Light on and off with same button

Hello!

I'm trying to write a program that makes a LED high, and a LED low with the same button.
Press once: LED = high
Press 2nd time: LED = low

I saw there is an example from Arduino, but that looks way more complicated than what I'm trying to do.
Anyway, here is my code:

  // decclareren ingangen, uitgangen en variabelen
  const int ledPin = 13;
  const int knopPin = 2;

  int knopStand = 0;
  int knopCount = 0;

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

}

void loop() {
  // put your main code here, to run repeatedly:
  knopStand = digitalRead(knopPin);

  if (knopStand == HIGH && knopCount == LOW){
    digitalWrite(ledPin, HIGH);
    digitalWrite(knopCount, HIGH);
  }

   if (knopStand == HIGH && knopCount == HIGH){
    digitalWrite(ledPin, LOW);
    digitalWrite(knopCount, LOW);
  }
}

The LED goes on, but it doesnt go off. It seems knopCount doesnt go to HIGH.

The program has no errors.

If I understand it right, in the void loop the program keeps going in a loop, right?

Thanks in advance!

StafH:
If I understand it right, in the void loop the program keeps going in a loop, right?

Yes, almost. There is no such thing as a "void loop". There is a loop() function that returns a void datatype.

I saw there is an example from Arduino, but that looks way more complicated than what I'm trying to do.

Which one? It probably isn't.

The ONLY way to do what you want is to determine when the switch changes from not pressed to pressed. That is what the state change detection example shows how to do.

When the switch BECOMES pressed, increment a counter. Turn the LED on when the count is even. Turn if off when the count is odd.

You have to set pin 13 as OUTPUT.

Thanks for the fast answers! :slight_smile:

Found the Change Detection example. Helped me a lot.
I now understand how to do this.

One last question: A void data type can be anything?

StafH:
One last question: A void data type can be anything?

There isn't any data type called "void".

When you define a function such as

void lightON() {
   digitalWrite(13, HIGH);
}

it just means that it does not return any value.

If, on the other hand you wanted a function to calculate a value and return the answer as an integer you would do this

int multiply(int val1, int val2) {
   int product = val1 * val2;
   return product;
}

I hope I have answered the question that was in your mind. If not, please explain further.

...R

hi

iv got the LCD with Buttons on i use this

if (digitalRead(inputPin2) == LOW) {
lcd.setCursor(0,0);
lcd.print("USB Off ");
digitalWrite(2, HIGH);
delay(200);
}
else if (digitalRead(inputPin2) == HIGH) {
lcd.setCursor(0,0);
lcd.print("USB On ");
digitalWrite(2, LOW);
delay(200);
}
else {
delay(1);
}
it works great

Good news if it works for you.

However, keep in mind that any switch will bounce a few times before stabilizing.
Some programs wait for 35ms before judging that the switch has stabilized
This probably might not be critical for your application, but you might want to read about debouncing.

http://playground.arduino.cc/Learning/SoftwareDebounce
http://playground.arduino.cc/Learning/SoftwareDebounce

There are several methods, even libraries and you can also install a capacitor in parallel to the contacts of your switch(es).

Dan ....

Using the Bounce2 library is by far the most easiet way to deal with switches. Just:

Bounce mySwitch;

void setup(){
  mySwitch.attach(pinnumber, INPUT_PULLUP);
}

In the loop (or whenever you want to use it) just call mySwitch.update() and use mySwitch.fell() to see if it changed to a pressed state. Or use the methodes rose() or read()

Thanks for the info Robin and Dan. Will be reading those websites in regard debounce.

I will stick with the basics for now :stuck_out_tongue: Just messing around to learn.

Thanks for all the help!

Robin2:
There isn't any data type called "void".

Yes there is.

C++ standard:
"The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value."

aarg:
Yes there is.

I shall humbly give myself 25 lashes when I can find suitable birch twigs.

...R

Aside from the return type for a function that returns nothing, the only use for void as a type is for pointers that can point to any kind of data. These are often used for functions that stream data, where any kind of data can be streamed, even pointers to class instances.