Unable to read switch status in between ADC conversion

From last few days i was trying to hookup with Arduino uno as i am building a project which works on ADC and some manual switches.

I put up Two Potentiometer in parallel and connected switches with both of them in series so i can read two different digital value whenever i press either one and process my functionality accordingly.

And i put Three led's to pin 11,12,13.

And one Switch for interruption from current execution at pin 9.

CODE:

//Analog pin
const int analogInPin = A0;

//output pin
const int led1 = 11;
const int led2 = 12;
const int led3 = 13;     

//input pin
const int Button = 9;     

//variables
int sensorValue=0;

void setup() {

 pinMode(led1,OUTPUT);
 pinMode(led2,OUTPUT);
 pinMode(led3,OUTPUT);
 
 pinMode(Button,INPUT_PULLUP);
}

void loop() {
 //read the analog in value:
             
     sensorValue = analogRead(analogInPin);
     
     
     if(sensorValue>=490 && sensorValue<=510)        //This condition should met when i press first button with first potentiometer.
     {
        digitalWrite(led1,1);       //led1 will glow
        digitalWrite(led2,0);       //led2 will be off

        digitalWrite(led3,1);       //led3 will glow

     }


     if(sensorValue>=790 && sensorValue<=810)        //This condition should met when i press second button with second potentiometer.
     {
        digitalWrite(led1,0);      //led1 will be off
        digitalWrite(led2,1);      //led2 will glow
        digitalWrite(led3,1);      //led3 will glow
     }

delay(2);
}

Everything is working fine in this code but i needed to include that Button pin to this code so that whenever i presses that Button, only status of led3 got change and status of both of the led's should remain same.

//Analog pin
const int analogInPin = A0;

//output pin
const int led1 = 11;
const int led2 = 12;
const int led3 = 13;     

//input pin
const int Button = 9;     

//variables
int sensorValue=0;

void setup() {

 pinMode(led1,OUTPUT);
 pinMode(led2,OUTPUT);
 pinMode(led3,OUTPUT);
 
 pinMode(Button,INPUT_PULLUP);
}

void loop() {
 //read the analog in value:
             
     sensorValue = analogRead(analogInPin);
     
     
     if(sensorValue>=490 && sensorValue<=510)        //This condition should met when i press first button with first potentiometer.
     {
        digitalWrite(led1,1);       //led1 will glow
        digitalWrite(led2,0);       //led2 will be off

        digitalWrite(led3,1);       //led3 will glow

     }

     if(sensorValue>=790 && sensorValue<=810)        //This condition should met when i press second button with second potentiometer.
     {
        digitalWrite(led1,0);      //led1 will be off
        digitalWrite(led2,1);      //led2 will glow
        digitalWrite(led3,1);      //led3 will glow
     }

      if(digitalRead(Button)==0)
     {
        digitalWrite(led3,0);      //status of led3 should change only and status of both of above led's should remain same
      }

delay(2);
}

Here If(Button==0) is the condition when button is actually pressed and i need it to work like changing the status of led3 only without altering the status of led1 and led2

I am putting my every effort to make it right but getting unsuccessful in doing so, Any Help is appreciated Thanks..

Try looking for values around 800 and 500. I feel you may be expecting something too precise.

Read this:-
http://forum.arduino.cc/index.php?topic=149021.0

Thanks Man @Grumpy_mike i did the changes..

Thanks for the changes it makes it so much easier to see what is going on.

The line that says:-

if(Button==0)

will always be false because you have defined the variable Button to be 9

const int Button = 9;

I think you meant to use:-

if(digitalRead(Button)==0)

I think you meant to use:-

if(digitalRead(Button)==0)

yes exactly i've done the changes but still having same problem

yes exactly i've done the changes but still having same problem

Have you read the forum rules? You should never change the technical contents of that first post because if makes answers based on your original code look silly.

So now what exactly is your problem? That code should do what you asked, that is the only time L3 changes is when the button is pressed.

You need to tell us exactly what happens.

  1. Does pressing the button have no effect?
  2. Does pressing the button also turn off one or both of the other two LEDs?

It is looking like you have not wired it up the way you think you have wired it up. So please supply a schematic and a photograph of what you have done.

Grumpy_Mike:
Have you read the forum rules? You should never change the technical contents of that first post because if makes answers based on your original code look silly.

Sorry Grumpy_mike my bad as i am new to this forum i will never repeat that mistake.

It is looking like you have not wired it up the way you think you have wired it up. So please supply a schematic and a photograph of what you have done.

I have connected the INPUT Button's first terminal to controller and second terminal to Ground so controller get Zero signal whenever button will be pressed. And for Note I also Declared Input Button as

pinMode(Button, INPUT_PULLUP);

So now what exactly is your problem? That code should do what you asked, that is the only time L3 changes is when the button is pressed.

You need to tell us exactly what happens.

  1. Does pressing the button have no effect?
  2. Does pressing the button also turn off one or both of the other two LEDs?

As according to my Original Code, whenever i presses the Button, controller doesn't retain previous status of LED1 and LED2, it turn OFF both of The LED's and as LED3 is also coded to turn OFF, so Every Led get's Turn Off.

whenever i presses the Button, controller doesn't retain previous status of LED1 and LED2, it turn OFF both of The LED's and as LED3 is also coded to turn OFF, so Every Led get's Turn Off.

That simply will not happen if you have wired it up like you describe. It sounds like the push button is shorting out the power supply, causing a reset and hence turning the LEDs off.

as I said:-

So please supply a schematic and a photograph of what you have done.

Because how it is wired up is not how you think it is wired.

So please supply a schematic and a photograph of what you have done.

Here is the Schematic to explain you how i had wired it up

Where is the photograph so we can check what you think you have wired up is what you have actually wired up.

Also, you NEED resistors in line with those LEDs. You are damaging your Arduino without them.

consider using the serial monitor as you have no idea what a0 is reading. It wouldn't surprise me to see the a0 number move one or two points especially if you are running of the usb power supply. Serial monitor will also show you the button states so you can see if theres a oddity in the code or the wiring.