loosing my nerves.. switch

i dont know what am i doing wrong:S i already used switch befor and it worked... i have a relay and i want to toggle between off and on with a button on pin 2... i want to use switch but it doesent work no matter how i turn it:S please help here is the code:

int pin2 = 2; int pin13 = 13; int stevilo = 0; int val=0;

void setup() {

pinMode(pin13, OUTPUT); pinMode(pin2, INPUT); }

void loop() {

val = digitalRead(pin2);

if (val == HIGH) stevilo++;

switch(stevilo){

case 0:{ digitalWrite(pin13,LOW); break; }

case 1:{ digitalWrite(pin13,HIGH); break; }

if(stevilo>1)stevilo=0; } }

this is just one of many i tried:P tnx!!!!

You are not enabling the pullup resistor for pin 2. This implies that you have an external resistor. Do you?

The test for HIGH implies that it is a pulldown resistor. How IS your switch wired?

There are extra curly braces in the switch statement that make it more difficult to read. You should remove them. Putting each necessary { on its own line, and using Tools + Auto Format would be appreciated, too.

Finally, there is no debouncing of the switch, and no tracking of the previous state, so you are not toggling the LED only on transition to pressed.

OK, that wasn't the final comment, I guess. This one will be. The switch statement is useful there are 3 or more cases. It is less useful when there are only two exclusive cases. You either want the LED on or off, so a simple if test would be better, in my opinion.

i know if is easier:P i tried it befor:P i made a simple code for a button.. and then i tried this:

int inPin = 2; // the number of the input pin int outPin = 13; // the number of the output pin

int state = HIGH; // the current state of the output pin int reading; // the current reading from the input pin int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers

void setup() { pinMode(inPin, INPUT); pinMode(outPin, OUTPUT); }

void loop() { reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we've waited long enough // to ignore any noise on the circuit, toggle the output pin and remember // the time if (reading == HIGH && previous == LOW && millis() - time > debounce) { if (state == HIGH) state = LOW; else state = HIGH;

time = millis(); }

digitalWrite(outPin, state);

previous = reading; }

works perfect. but still i dont know why my doesent. i know switch isnt useful with just 2 cases. but i want to really understand that and learn it. tnx and sry but i really am new at this:P

 switch(stevilo){

  case 0:     {       digitalWrite(pin13,LOW);       break;     }

  case 1:     {       digitalWrite(pin13,HIGH);       break;     }

    if(stevilo>1)stevilo=0;   }    // end of switch

Your "if" is in the wrong place, it is part of case 1.

It should be more like this:

void loop()
{

  val = digitalRead(pin2);

  if (val == HIGH)
    stevilo++;

  switch(stevilo)
  {

  case 0:
      digitalWrite(pin13,LOW);
      break;

  case 1:
      digitalWrite(pin13,HIGH);
      break;
  }   // end of switch

  if(stevilo>1)
    stevilo=0;

}  // end of loop

I like to put "// end of xxx" at each closing curly brace, so I know what it is ending. If you had done that it would be more obvious what is wrong.

So, take this code, that does debouncing and proper edge detection (the if is true only when the switch transitions from released to pressed) and use a switch statement instead of the if statement. It should work the same.

Your first code did not debounce or do proper edge detection, which caused the value in stevilo to be incremented more than you intended/thought was happening.

tnx for all the help:) it works.. i have a bit problems with the button i will fix that:) tnx!