Skip to main content
Grammar (subject/verb agreement), copy editing, code formatting
Source Link

I'm starting to learn how to use Arduino and I trytried to make this code, which is just a simple if else statement,statement; what I want to make is when the input is LOW, the ledLED 1 is HIGH and ledLEDs 2,3 isare LOW., and when the input is HIGH, the ledLEDs 2,3 isare HIGH and LED 1 is LOW. I wrote this code but when I put the switch in LOW, instead having just like what I intended, all the output isoutputs are HIGH.

I try changing the output and I checked all the wiring to the ledLEDs so my mistake is in the code.

Could you guys tell me what's wrong with the code.

Thank you?

int switchstate = 0;
void setup() {
  pinMode (13,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(3,INPUT);
 
}

void loop() {
  switchstate = digitalRead(3);
  if (switchstate == LOW) {
    digitalWrite(11,HIGH);
    digitalWrite(12,LOW);
    digitalWrite(13,LOW);
 
  }
  else {
    digitalWrite(11,LOW);
    digitalWrite(12,HIGH);
    digitalWrite(13,HIGH);
  }
}

I'm starting to learn how to use Arduino and I try to make this code, which is just a simple if else statement, what I want to make is when the input is LOW, the led 1 is HIGH and led 2,3 is LOW. and when the input is HIGH, the led 2,3 is HIGH and LED 1 is LOW. I wrote this code but when I put the switch in LOW, instead having just like what I intended, all the output is HIGH.

I try changing the output and I checked all the wiring to the led so my mistake is in the code.

Could you guys tell me what's wrong with the code.

Thank you

int switchstate = 0;
void setup() {
pinMode (13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(3,INPUT);
 
}

void loop() {
switchstate = digitalRead(3);
if (switchstate == LOW){
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
 
}
 else{
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
}

I'm starting to learn how to use Arduino and I tried to make this code, which is just a simple if else statement; what I want to make is when the input is LOW, LED 1 is HIGH and LEDs 2,3 are LOW, and when the input is HIGH, the LEDs 2,3 are HIGH and LED 1 is LOW. I wrote this code but when I put the switch in LOW, instead having just like what I intended, all the outputs are HIGH.

I try changing the output and I checked all the wiring to the LEDs so my mistake is in the code.

Could you guys tell me what's wrong with the code?

int switchstate = 0;
void setup() {
  pinMode (13,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(3,INPUT);
}

void loop() {
  switchstate = digitalRead(3);
  if (switchstate == LOW) {
    digitalWrite(11,HIGH);
    digitalWrite(12,LOW);
    digitalWrite(13,LOW);
  } else {
    digitalWrite(11,LOW);
    digitalWrite(12,HIGH);
    digitalWrite(13,HIGH);
  }
}
Post Migrated Here from electronics.stackexchange.com (revisions)
Source Link

If - else statement in Arduino

I'm starting to learn how to use Arduino and I try to make this code, which is just a simple if else statement, what I want to make is when the input is LOW, the led 1 is HIGH and led 2,3 is LOW. and when the input is HIGH, the led 2,3 is HIGH and LED 1 is LOW. I wrote this code but when I put the switch in LOW, instead having just like what I intended, all the output is HIGH.

I try changing the output and I checked all the wiring to the led so my mistake is in the code.

Could you guys tell me what's wrong with the code.

Thank you

int switchstate = 0;
void setup() {
pinMode (13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(3,INPUT);

}

void loop() {
switchstate = digitalRead(3);
if (switchstate == LOW){
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,LOW);

}
else{
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
}