0

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);
  }
}

migrated from electronics.stackexchange.com Jul 11 '16 at 4:33

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • Can you post a schematic of your circuit. – Aircraft Jul 11 '16 at 4:27
  • do you check voltages ? When input is HIGH, does it give right answer ? – Anklon Jul 11 '16 at 5:15
  • What type of switch, a push button or a slide switch? – RSM Jul 11 '16 at 5:45
  • You say your circuit is correct, yet by looking at your code, the leds should never be lit at the same time.. You do not debounce the input in software, but it should not matter as if the input flickered, it should change the state of all the leds. Review the circuit again. – jogco Jul 11 '16 at 5:52
  • Pull-up/-down in place? – JimmyB Jul 11 '16 at 7:32
1

Your code doesn't seem to be the problem. Contrary to what you are saying I think your problem is most definitely in your hardware setup.

Most likely, your digitalRead doesn't register a HIGH input, so it doesn't get 5V. Instead of digitalRead, go for analogRead(A0). So change your circuit and attach it to pin A0.

Use serial.println(switchstate) to print the results to the serial monitor and then decide what your top value (HIGH) and bottom value (LOW) for detection will be. Perhaps include a pull down resistor as well on the A0 pin.

  • Thanks, I will try that as soon as I got home. One more thing thou, how the output generate all high even I put the wrong wiring. is the arduino have NC thing when there is no logic in it will just outputing high ? – glenn Himawan Jul 11 '16 at 15:00
  • I don't know what you are using to create this HIGH input. Switchstate also is an integer so it could be possible your thing that is supposed to create a HIGH output, isn't creating a HIGH output and as such Switchstate is filled as an integer, not a boolean (booleans have two states: true or false, or HIGH or LOW). Experiment. – Len Jul 11 '16 at 15:08
0

To debug this, as Len has suggested I would recommend putting in some Serial.println("Debug info") commands into your code. This will confirm that you are programming the Arduino as you expect, and that the Arduino is running.

To do this simply include a Serial.begin(9600) command in your setup block, and then a Serial.println("Some text") wherever you want to check if you are reaching a certain part of the code. You can then read these messages via the serial monitor (which you can get to from Tools->Serial Monitor in the Arduino IDE).

For hardware debugging if you have a multi-meter available I would check that you are getting 0V/5V on digital pin 3 when you expect (i.e. when you are / are not pressing the button). Also, to check your LEDs are working, and connected to the output pins you think they are, write a script which just scrolls through them with a short delay (See the Blink example under File->Examples->01.Basics->Blink for an example of how to do that).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.