on off swich

hi i am new here. So i dont know anything about arduino projects etc. my question is : i am tring to make an on off swich but something goes wrong this is the code that i am using! plz help
boolean myen;
int en;
void setup (){
pinMode(12, INPUT);
pinMode(7, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(12,LOW);
digitalWrite(7,LOW);
digitalWrite(2,LOW);
myen =false;
}
void loop(){
en = digitalRead(12);
if (en == HIGH)
{myen= !myen;
delay(1000);}
while(myen==true)
{
digitalWrite(7, HIGH);

en = digitalRead(12);
if (en == HIGH)
myen=!myen;
delay(1000);
}
}

Welcome to the forum.

What do you want to do ?
Pressing a switch to turn something on, and pressing it again to turn it off ?
How is the switch connected ?

When you type a post, you can use the '#'-button for the sketch. The code tags around it looks like this:

Serial.println("Hello code tags");

A button will bounce and jitter, especially when making contact. You need some kind of debouncing.
You can make it yourself and test during 20ms the input, or use for example one of these:

http://playground.arduino.cc/Code/Bounce

Welcome

Please read how to post code

  • use code tags == # button above smileys
  • use CTRL-T before copying code (auto formatting makes it more readable)

The problem with your code is that it uses delay().
when executing delay() the Arduino blocks and cannot check other pins for value changes.,

Please study the "blink without delay " example for a programming technique that is far more responsive.

Peter_n:
A button will bounce and jitter, especially when making contact. You need some kind of debouncing.

I prefer to see this as a risk rather than a certainty. If switch bounce causes a problem it can be solved as you say.

However I have never yet needed to debounce switches and some of mine have been very poor - including just touching 2 wires together. A lot depends on how the program is written.

...R

I disagree, years of experience tells me always debounce switches!

Robin2:
However I have never yet needed to debounce switches

We must be living in a different universe :astonished: my switches always bounce. And when this is a 2-divider with a switch, it will be a problem.

bobcousins:
I disagree, years of experience tells me always debounce switches!

Always ?
Would you debounce a button that once pressed caused a relay to turn on and stay on, for example ?

I suspect the reason my switches don't need debouncing is because, after reading the switch once, my code goes off and does something and by the time it is finished (and ready to read the switch again) all the bouncing will be over and done with. And usually my switches set state variables and then the switches don't get tested again until the state changes. Of course, that's a form of debouncing but it is integral to the code and no special treatment is needed.

...R

i cannot understand one thing!
inside the while function why i does not work the

en = digitalRead(12);
 if (en == HIGH)
  myen=!myen;

part of my code?
i also try to change the value of en to LOW like this

en=LOW;

but still doesnt worked

You can use boolean variables with: myen = !myen;

Your sketch is not straightforward. I can't help if I don't know what you want to do.
Did you see my questions in Reply #2 ?
Do you understand that you can't read the switch during a delay as posted in Reply #3 ?

plus:

  pinMode(12, INPUT);//   <<<< if you are reading your pin?????
  pinMode(7, OUTPUT);
  pinMode(2, OUTPUT);
  digitalWrite(12,LOW);//  <<<<what is this doing here?????????
  digitalWrite(7,LOW);
  digitalWrite(2,LOW);

it activates the pull down resistor for an input.

i want by pushing a button to turn on a led an by pushing the same button to turn it off! i read them but nothing worked! sorry but i am new here and i do not know nothing

robtillaart:
it activates the pull down resistor for an input.

pull up or pull down ?

millions sorry i am an idiot
i forgot to put digitalWrite(7, LOW);
this is the final code i should hve made

boolean myen;
int en;
void setup (){
pinMode(12, INPUT);
pinMode(7, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(7,LOW);
digitalWrite(2,LOW);
myen =false;
}
void loop(){
  en = digitalRead(12);
  if (en == HIGH)
  {myen= !myen;}
while(myen==true)
{
  en=LOW;
digitalWrite(7, HIGH);

 en = digitalRead(12);
 if (en == HIGH)
  myen=!myen;
}
digitalWrite(7, LOW);
}

anagnostou:
millions sorry i am an idiot
i forgot to put digitalWrite(7, LOW);
this is the final code i should hve made

boolean myen;

int en;
void setup (){
pinMode(12, INPUT);
pinMode(7, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(7,LOW);
digitalWrite(2,LOW);
myen =false;
}
void loop(){
  en = digitalRead(12);
  if (en == HIGH)
  {myen= !myen;}
while(myen==true)
{
  en=LOW;
digitalWrite(7, HIGH);

en = digitalRead(12);
if (en == HIGH)
  myen=!myen;
}
digitalWrite(7, LOW);
}

XD

congrats!