element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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 Need help
  • 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
  • Replies 31 replies
  • Subscribers 388 subscribers
  • Views 4499 views
  • Users 0 members are here
  • help
  • prototyping
  • code
  • arduino
Related

Need help

masa98
masa98 over 3 years ago

hi

im working on a car seat controller and im using 2 arduino nano boards to controll the seat motors. but i ran into a problem with the code. i only get 1V from D2,D4,D5 if i use this code. but if i load another code the pins give 5V and my cirquit only draws 8.5 mA. can some body help.

 //B1-B6 Buttons 
 //D2-D7 output pins
 
int BI = 14;
int B2 = 15;
int B3 = 16;
int B4 = 17;
int B5 = 18;
int B6 = 19;

int D2 = 2;
int D3 = 3;
int D4 = 4;
int D5 = 5;
int D6 = 6;
int D7 = 7;

void setup() {
  
  pinMode(B1, INPUT);
  pinMode(B2, INPUT);
  pinMode(B3, INPUT);
  pinMode(B4, INPUT);
  pinMode(B5, INPUT);
  pinMode(B6, INPUT);

  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  
}

void loop() {
  
  if (digitalRead(BI) == HIGH && digitalRead(B2) == LOW) {
    
  digitalWrite(D2, HIGH);
  digitalWrite(D3, LOW);
}
  if (digitalRead(B2) == HIGH && digitalRead(BI) == LOW) {
    
  digitalWrite(D3, HIGH);
  digitalWrite(D2, LOW);
}
  else{
  digitalWrite(D2, LOW);
  digitalWrite(D3, LOW);
}

  if (digitalRead(B3) == HIGH && digitalRead(B4) == LOW) {
    
  digitalWrite(D4, HIGH);
  digitalWrite(D5, LOW);
}
  if (digitalRead(B4) == HIGH && digitalRead(B3) == LOW) {
    
  digitalWrite(D5, HIGH);
  digitalWrite(D4, LOW);
}
  else{
  digitalWrite(D4, LOW);
  digitalWrite(D5, LOW);
}

  
  if (digitalRead(B5) == HIGH && digitalRead(B6) == LOW) {
    
  digitalWrite(D6, HIGH);
  digitalWrite(D7, LOW);
}
  if (digitalRead(B6) == HIGH && digitalRead(B5) == LOW) {
    
  digitalWrite(D7, HIGH);
  digitalWrite(D6, LOW);
}
  else{
  digitalWrite(D6, LOW);
  digitalWrite(D7, LOW);
}
}

  • Sign in to reply
  • Cancel

Top Replies

  • beacon_dave
    beacon_dave over 3 years ago +4
    It looks like the second 'if' should be an 'else if'. if else if else instead of if if else Otherwise the pin is being turned on by the first if and then immediately turned off by the else.
  • scottiebabe
    scottiebabe over 3 years ago +4
    'B1' versus 'BI' seems strange, not sure if it's related.
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to Jan Cumps +4
    This is what happens: Condition: B1 : 0, B2 : 0 - D2: false ,D3: false ,D2: false ,D3: false , Condition: B1 : 0, B2 : 1 - D2: false ,D3: false ,D3: true ,D2: false , Condition: B1 : 1, B2 : 0 - D2:…
  • robogary
    robogary over 3 years ago

    Please review the Arduino hardware pinouts and their designations

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • beacon_dave
    beacon_dave over 3 years ago

    It looks like the second 'if' should be an 'else if'.

    if 
    else if
    else

    instead of

    if
    if
    else

    Otherwise the pin is being turned on by the first if and then immediately turned off by the else.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 3 years ago

    'B1' versus 'BI' seems strange, not sure if it's related.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 3 years ago

    Also, just as an idea,  it would be simpler if you just wrote one third of the code (i.e. for one pair of buttons) and tested and got that running first, because then you can scale it or rewrite it.

    It should really be prototyped on breadboard with a couple of LEDs and resistors, so that you can be sure your code is working fine before subjecting it to the motors. If you're seeing 1V on pins, then you've got hardware issues (as well as the code issues).

    Also, (I'm being blunt because you won't get far without a lot of help from others, unless you make this effort), you really should study C, because the code is really not pretty (clue: too many variables, and too much code that could be collapsed and simplified).

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • masa98
    masa98 over 3 years ago in reply to scottiebabe

    i got somekind of error when i had it as B1 

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • masa98
    masa98 over 3 years ago

    i made some changes to the code but now i get 5v from D2 and only 2.9v from D3. the funktion im after is, if B1 is HIGH then D2 must be HIGH and D3 must be LOW. and if B2 is HIGH then D2 must be LOW and D3 must be HIGH

     //B1-B6 Buttons 
     //D2-D7 output pins
     
    int BI = 14;
    int B2 = 15;
    int B3 = 16;
    int B4 = 17;
    int B5 = 18;
    int B6 = 19;
    
    int D2 = 2;
    int D3 = 3;
    int D4 = 4;
    int D5 = 5;
    int D6 = 6;
    int D7 = 7;
    
    void setup() {
      
      pinMode(B1, INPUT);
      pinMode(B2, INPUT);
      pinMode(B3, INPUT);
      pinMode(B4, INPUT);
      pinMode(B5, INPUT);
      pinMode(B6, INPUT);
    
      pinMode(D2, OUTPUT);
      pinMode(D3, OUTPUT);
      pinMode(D4, OUTPUT);
      pinMode(D5, OUTPUT);
      pinMode(D6, OUTPUT);
      pinMode(D7, OUTPUT);
      
    }
    
    void loop() {
      
      if (digitalRead(BI) == HIGH && digitalRead(B2) == LOW) {
        
      digitalWrite(D2, HIGH);
      digitalWrite(D3, LOW);
    }
      else{
      digitalWrite(D2, LOW);
      digitalWrite(D3, LOW);
    
    
    if (digitalRead(B2) == HIGH && digitalRead(BI) == LOW) {
        
      digitalWrite(D3, HIGH);
      digitalWrite(D2, LOW);
    }
      else{
      digitalWrite(D2, LOW);
      digitalWrite(D3, LOW);
    }
    }
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 3 years ago in reply to masa98

    I'm not an Arduino expert but it appears to that Bx is defined as the binary equivalent   forum.arduino.cc/.../480156 interesting 

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • genebren
    genebren over 3 years ago in reply to masa98

    You still have an issue with your B1 and BI.  BI is not being set as input, but B1 is.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 3 years ago in reply to scottiebabe

    So make sure you stick to the BI variable as B1 would just be a constant 1

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • masa98
    masa98 over 3 years ago in reply to genebren

    it had on effect when i changed it

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • 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 © 2025 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.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube