element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum I/O reading Problem
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 5 replies
  • Subscribers 417 subscribers
  • Views 471 views
  • Users 0 members are here
  • help
  • arduino
Related

I/O reading Problem

e14 Contributor
e14 Contributor over 12 years ago

I created a small program on the Arduino coding platform and it compiles fine, but when it is tested on my breadboard it does not work. I think that I messed up somewhere in the coding since i'm a beginner, anyway here is the code: 

int lightA = 0;

int lightB = 0;

 

 

void setup(){

pinMode(13, INPUT);

  pinMode(12, INPUT);

   pinMode(8, OUTPUT);

    pinMode(7, OUTPUT);

}

void loop(){

  lightA = digitalRead(13);

  lightB = digitalRead(12);

  if (lightA == HIGH){

   digitalWrite(8, HIGH);

    digitalWrite(7, LOW);

  }

  if (lightB == HIGH){

   digitalWrite(8, LOW);

    digitalWrite(7, HIGH);

  }

  else{

   digitalWrite(8, LOW);

    digitalWrite(7, LOW);

  }

 

 

}

 

If you can find what i'm doing wrong please post!

  • Sign in to reply
  • Cancel
Parents
  • Robert Peter Oakes
    0 Robert Peter Oakes over 12 years ago

    do you have pull up resistors on the inputs ?

     

    are the outputs doing anything ?

     

    also

     

    you need the additional ifs to have an else in front of them otherwise you will have the situation where only if A and B are low will both outputs be low if b input is high then it will always override input a

     

    so what you should be getting is this:-

    A=HIGH and B=HIGH = output 8=  LOW, 7 = HIGH

    A=LOW and B=HIGH = output 8=  LOW, 7 = HIGH

    A=HIGH and B=LOW = output 8 = HIGH, 7 = LOW

    A=LOW and B=LOW = output 8 = LOW, 7 = LOW

     

    if you wanted this

    A=HIGH and B=HIGH = output 8=  HIGH, 7 = LOW

    A=LOW and B=HIGH = output 8=  LOW, 7 = HIGH

    A=HIGH and B=LOW = output 8 = HIGH, 7 = LOW

    A=LOW and B=LOW = output 8 = LOW, 7 = LOW

     

    if (lightA == HIGH){
       digitalWrite(8, HIGH);
        digitalWrite(7, LOW);
      }
     else if (lightB == HIGH){
       digitalWrite(8, LOW);
        digitalWrite(7, HIGH);
      }
      else{
       digitalWrite(8, LOW);
        digitalWrite(7, LOW);
      }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Robert Peter Oakes
    0 Robert Peter Oakes over 12 years ago

    do you have pull up resistors on the inputs ?

     

    are the outputs doing anything ?

     

    also

     

    you need the additional ifs to have an else in front of them otherwise you will have the situation where only if A and B are low will both outputs be low if b input is high then it will always override input a

     

    so what you should be getting is this:-

    A=HIGH and B=HIGH = output 8=  LOW, 7 = HIGH

    A=LOW and B=HIGH = output 8=  LOW, 7 = HIGH

    A=HIGH and B=LOW = output 8 = HIGH, 7 = LOW

    A=LOW and B=LOW = output 8 = LOW, 7 = LOW

     

    if you wanted this

    A=HIGH and B=HIGH = output 8=  HIGH, 7 = LOW

    A=LOW and B=HIGH = output 8=  LOW, 7 = HIGH

    A=HIGH and B=LOW = output 8 = HIGH, 7 = LOW

    A=LOW and B=LOW = output 8 = LOW, 7 = LOW

     

    if (lightA == HIGH){
       digitalWrite(8, HIGH);
        digitalWrite(7, LOW);
      }
     else if (lightB == HIGH){
       digitalWrite(8, LOW);
        digitalWrite(7, HIGH);
      }
      else{
       digitalWrite(8, LOW);
        digitalWrite(7, LOW);
      }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to Robert Peter Oakes

    Well when I start the program on the Arduino both of the lights turn on and I do have resistors on the outputs.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 12 years ago in reply to e14 Contributor

    what about the inputs ?, im assuming some kind of pull up on the inputs and a switch to short to ground ?

     

    also you are not setting the outputs to any state after initializing them to output

    do this

     

    void setup(){

         pinMode(13, INPUT);

         pinMode(12, INPUT);

         pinMode(8, OUTPUT);

         pinMode(7, OUTPUT);

          digitalWrite(8, LOW); 

          digitalWrite(7, LOW);

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago in reply to Robert Peter Oakes

    I had been using a regular 220Ω resistor for the inputs and I didn't have a switch to ground so ill add that and ill have to revise my script. Thanks! 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 12 years ago in reply to Robert Peter Oakes

    Pin 13 has the LED across it, so not a good choice for an input.

    it will tend by low all the time.

     

    By default the Outputs are low when you set them as an output, however its a good practice to set the state JUST so you know.

     

    My guess it needs resistors between the pin and ground.

    The switch is likely to go from +5v to the pin

     

      else{

       digitalWrite(8, LOW);

        digitalWrite(7, LOW);

     

    otherwise you'd need to hold the switch all the time.

     

    You could try this which compiles and should work.

     

    int LightA = 12;  // set the outputs
    int LightB = 13;  // set the outputs
    int ButtonA = 7;  // set the input ** Add a resistor from the pin to ground, switch goes from 5v to the pin
    int ButtonB = 8;  // set the input ** Add a resistor from the pin to ground, switch goes from 5v to the pin
    
    void setup()
    {
     pinMode(LightA, OUTPUT);
     pinMode(LightB, OUTPUT);
     pinMode(ButtonA, INPUT);
     pinMode(ButtonB, INPUT);
    }
    
    void loop()
    {
      digitalRead(ButtonA);
      digitalRead(ButtonB);
      digitalWrite(LightA, ButtonA);
      digitalWrite(LightB, ButtonB);
    }
    
    }

     

     

    This link here gives good information about what is happening with the pins.

    http://www.baldengineer.com/tutorials/arduino-pull-ups/

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube