Arduino Code is not working, Anything is Wrong with it?

Hi, everyone.
i am new to arduino and i like amazing projects, then i try to make one for my self.
i want to Read 2 inputs at a time and make OUTPUT to work correctly.
i write this code in IDE. anyone plz help me if there is a problem in my coding.
(Note: i want if 1 input is high the 1 led ON, if 2nd input high and first input low then both leds OFF, if both input high then 2nd led ON. )

int btn1 = A1;
int btn2 = A4;
int led = 3;
int led2 = 6;

void setup() {
pinMode(btn1,INPUT_PULLUP);
pinMode(btn2,INPUT_PULLUP);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);

digitalWrite(led,LOW);
digitalWrite(led2,LOW);
}

void signal1() {
if(analogRead(btn1)== 1)
{
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
delay(500);
if(analogRead(btn2)== 1)
{
digitalWrite(led2,LOW);
}

}
void signal3(){
if(analogRead(btn1)== 0){
digitalWrite(led,LOW);
}
if(analogRead(btn2)== 1){
digitalWrite(btn2,LOW);
}
}
void signal2(){
if(analogRead(btn1)== 0)
{
digitalWrite(led,LOW);
}
if(analogRead(btn2)== 0)
{
digitalWrite(led2,HIGH);
}
}
void loop (){
signal1();
signal3();
signal2();
}

I suspect you want to use "digitalRead" for those pins, rather than analogRead.

Welcome to the forum.

You do not need analog read. This function is used to read voltages with the ADC.

Have a look at the following page:

https://www.arduino.cc/reference/en/

Look for digitalRead()

When posting source code please use the </> button above and past your code there. This makes the code easier to read and copy. It should look like this:

// Your Example code here
1 Like

thankyou Both of you for the suggestion . digitalRead work for me... i change my code But this time i write both LOW LOW, but it turn HIGH both leds once. any suggestion.

if(digitalRead(btn1)== 0)
{
digitalWrite(led1,LOW);
}
if(digitalRead(btn2)== 1)
{
digitalWrite(led2,LOW);
}
if(digitalRead(btn1)== 1)
{
digitalWrite(led1,LOW);
}
if(digitalRead(btn2)== 0)
{
digitalWrite(led2,LOW);

thnx and Regards.

Use code tags

code tags ???? can you plz write little ??

Edit your posts after reading this guide to the forum

Use the :pencil2: down here :point_down: then highlight your code and hit the code tag button <|> up here :point_up_2:
This will make it easier for people to help you

1 Like

thankyou Both of you for the suggestion . digitalRead work for me... i change my code But this time i write both LOW LOW, but it turn HIGH both leds once. any suggestion??

if(digitalRead(btn1)== 0)
{
digitalWrite(led1,LOW);
}
if(digitalRead(btn2)== 1)
{
digitalWrite(led2,LOW);
}
if(digitalRead(btn1)== 1)
{
digitalWrite(led1,LOW);
}
if(digitalRead(btn2)== 0)
{
digitalWrite(led2,LOW);

:unamused: :persevere:
i want if 1 input is high the 1 led ON, if 2nd input high and first input low then both leds OFF, if both input high then 2nd led ON.

But its not working. i make them off its going both ON.
anyone plz make it correct. thnx

int led1 = 3;
int led2 = 5;
int btn1 = 8;
int btn2 = 10;


void setup() {
 pinMode(btn1,INPUT);// make input
 pinMode(btn2,INPUT); // 2nd input
 pinMode(led1,OUTPUT); // ist output
 pinMode(led2,OUTPUT); // 2nd output
}

void loop() {
 if(digitalRead(btn1)== 1) // read pin 1
  {
    digitalWrite(led1,HIGH); // make led high
  }
  
  if(digitalRead(btn2)== 1)
  {
    digitalWrite(led2,LOW); // make led low
  }
  if(digitalRead(btn1)== 0)
  {
    digitalWrite(led1,LOW);
  }
  if(digitalRead(btn2)== 1)
  {
    digitalWrite(led2,LOW);
  }
  if(digitalRead(btn1)== 1)
  {
    digitalWrite(led1,LOW);
  }
  if(digitalRead(btn2)== 0)
  {
    digitalWrite(led2,LOW);
  }
  if(digitalRead(btn1)== 0)
  {
    digitalWrite(led1,LOW);
  }
  if(digitalRead(btn2)== 0)
  {
    digitalWrite(led2,HIGH);
  }
}

Buttons are normally wired to gnd and have input-pullup in the code initialisation.

Look at your if statements and follow the logic. You need to write what you want.

Your first if says that if button 1 is pressed then high. It says nothing about if button 2 is pressed. Code runs through and doesn’t care about anything other than the instructions given. If button 1 is pressed it will go high irrespective of what else is pressed.

Use a multi conditional statement

Btn1 == 1 && btn2 ==0
Pseudocode use correct syntax

Ps is this a homework assignment. Recent similar posts

1 Like

i apply multi conditional like this

int led1 = 3;
int led2 = 5;
int btn1 = 8;
int btn2 = 10;


void setup() {
 pinMode(btn1,INPUT_PULLUP);
 pinMode(btn2,INPUT_PULLUP); 
 pinMode(led1,OUTPUT);
 pinMode(led2,OUTPUT); 
}

void loop() {
 if(digitalRead(btn1)== 1 && (btn2)==1) 
  {
    digitalWrite(led1,HIGH); 
    digitalWrite(led2,LOW);
  }
  
  if(digitalRead(btn1)== 1 && (btn2)== 0)
  {
    digitalWrite(led1,LOW); 
    digitalWrite(led2,LOW);
  }
  if(digitalRead(btn1)== 0 && (btn2)== 1)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
  }
  if(digitalRead(btn1)== 0 && (btn2)== 0)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
  }
}
Code not Working. LEDs  remain OFF, however button is ON or button is OFF.

Your ifs are broken. The first part of each uses digitalRead, but then you just compare the pin number to one or zero.

Think about your code and what it does. If you need to read btn1 then you also need to read btn2. The microcontroller is pedantic and will do exactly as it says so don’t take shortcuts

1 Like

Thanks man. Now its Work as i want . very very lot of thanks and Regard.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.