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 4510 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:…
Parents
  • Jan Cumps
    Jan Cumps over 3 years ago

    Here's the loop() with indentation.

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

    I'll try and write a testbed, and see what happens in the four scenarios:

    B1 B2
    0 0
    0 1
    1 0
    1 1
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to Jan Cumps

    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: true ,D3: false ,
    Condition: B1 : 1, B2 : 1 - D2: false ,D3: false ,D2: false ,D3: false ,

    see how many times the same digitalWrite() is called, in  test 1, 2 and 4? 

    Test code. The testLoop() is the same logic as your loop():

    #include <iostream>
    #include <string>
    
    #define HIGH (true)
    #define LOW (false)
    #define D2 ("D2")
    #define D3 ("D3")
    
    bool digitalRead(bool value) {
    	return value;
    }
    
    void digitalWrite(std::string pin, bool value) {
    	std::cout << pin;
    	if (value) {
    		std::cout << ": true  ,";
    	} else {
    		std::cout << ": false ,";
    	}
    }
    
    void testLoop(bool B1, bool B2) {
    	std::cout << "Condition: B1 : ";
    	std::cout << B1;
    	std::cout << ", B2 : ";
    	std::cout << B2;
    	std::cout << " - ";
    
    
    	if (digitalRead(B1) == HIGH && digitalRead(B2) == LOW) {
    
    		digitalWrite(D2, HIGH);
    		digitalWrite(D3, LOW);
    	}
    	else{
    		digitalWrite(D2, LOW);
    		digitalWrite(D3, LOW);
    
    
    		if (digitalRead(B2) == HIGH && digitalRead(B1) == LOW) {
    
    			digitalWrite(D3, HIGH);
    			digitalWrite(D2, LOW);
    		}
    		else{
    			digitalWrite(D2, LOW);
    			digitalWrite(D3, LOW);
    		}
    	}
    	std::cout << std::endl;
    }
    
    int main() {
    	testLoop(false, false);
    	testLoop(false, true);
    	testLoop(true, false);
    	testLoop(true, true);
    	return 0;
    }
    

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

    Now using beacon_dave's logic:

    result:

    Condition: B1 : 0, B2 : 0 - D2: false ,D3: false ,
    Condition: B1 : 0, B2 : 1 - D3: true ,D2: false ,
    Condition: B1 : 1, B2 : 0 - D2: true ,D3: false ,
    Condition: B1 : 1, B2 : 1 - D2: false ,D3: false ,

    Test code: The testLoop() is the same logic as Dave's loop():

    void testLoop(bool B1, bool B2) {
    	std::cout << "Condition: B1 : ";
    	std::cout << B1;
    	std::cout << ", B2 : ";
    	std::cout << B2;
    	std::cout << " - ";
    
    
    	if (digitalRead(B1) == HIGH && digitalRead(B2) == LOW)
    	{
    		digitalWrite(D2, HIGH);
    		digitalWrite(D3, LOW);
    	}
    	else if (digitalRead(B2) == HIGH && digitalRead(B1) == LOW)
    	{
    		digitalWrite(D3, HIGH);
    		digitalWrite(D2, LOW);
    	}
    	else
    	{
    		digitalWrite(D2, LOW);
    		digitalWrite(D3, LOW);
    	}
    
    	std::cout << std::endl;
    }

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Jan Cumps
    Jan Cumps over 3 years ago in reply to Jan Cumps

    Now using beacon_dave's logic:

    result:

    Condition: B1 : 0, B2 : 0 - D2: false ,D3: false ,
    Condition: B1 : 0, B2 : 1 - D3: true ,D2: false ,
    Condition: B1 : 1, B2 : 0 - D2: true ,D3: false ,
    Condition: B1 : 1, B2 : 1 - D2: false ,D3: false ,

    Test code: The testLoop() is the same logic as Dave's loop():

    void testLoop(bool B1, bool B2) {
    	std::cout << "Condition: B1 : ";
    	std::cout << B1;
    	std::cout << ", B2 : ";
    	std::cout << B2;
    	std::cout << " - ";
    
    
    	if (digitalRead(B1) == HIGH && digitalRead(B2) == LOW)
    	{
    		digitalWrite(D2, HIGH);
    		digitalWrite(D3, LOW);
    	}
    	else if (digitalRead(B2) == HIGH && digitalRead(B1) == LOW)
    	{
    		digitalWrite(D3, HIGH);
    		digitalWrite(D2, LOW);
    	}
    	else
    	{
    		digitalWrite(D2, LOW);
    		digitalWrite(D3, LOW);
    	}
    
    	std::cout << std::endl;
    }

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

    that seamed to fix it. thanks for the help

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

    You will still have to work on debouncing the buttons. The loop is run many many times per second, and a mechanical switch doesn't nicely open or close at once. It bounces up many times very fast, before permanently open or close the contact.

    But the processor may be faster than those fast bounces, and your code would behave unstable because of that.

    One possible easy option is to:

    first digitalRead() all inputs at the begin of the loop in local variables. So that you don't poll the same pin several times in the same loop.
    In your if statements, you then use the variables, where you now use the repeated digitalRead() calls

    And put a delay(x) at the end of the loop, after all  the if statements.
    Experiment with the x value, so that you don't get bounce issues, and that the project is still reacting.

    delay(20)  would be a reasonable start point for experimenting.

    #define BUTTON_1 (14)
    #define BUTTON_2 (15)
    
    #define DEBOUNCE_DELAY (20)
    
    loop() {
      // poll input state at start of the loop
      bool btn1 = digitalRead(BUTTON_1);
      bool btn2 = digitalRead(BUTTON_2);
    
      // execute logic based on the gathered state
      if ((btn1 == HIGH) && (btn2 == LOW)) {
        // ...
      }
    
      // debounce delay (not the best debounce method, but may work)
      delay(DEBOUNCE_DELAY);
    }

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

    thanks the delay made my motors to start better

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

    For debugging you could add big delays (like 5 seconds) at the bottom of each if/else block, so that after a button press it stays with the outputs you set, so then you have time to test them. Combined with the Serial.println, that should give you a fair bit of extra insight into what is happening.

    • 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