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 Multi Toggle
  • 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 5 replies
  • Subscribers 385 subscribers
  • Views 888 views
  • Users 0 members are here
Related

Multi Toggle

Gary.Baker
Gary.Baker 7 months ago
CAN SOMEONE WITH MORE EXPERIENCE THAN ME HELP WITH THE MISTAKE WITH THIS CODE.
THE PROBLEM IS THAT EITHER PINS 2 OR 3 WILL TOGGLE PINS 12 OR 13.
WHAT I WANT IS PNE 2 TOGGLE PIN 12 AND PIN 3 TOGGLE PIN 13
THANKS. GARY
/*
Button

The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground

- Note: on most Arduinos there is already an LED on the board
attached to pin 13.

*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin2 = 2; // the number of the pushbutton pin
const int ledPin13 = 13; // the number of the LED pin
const int buttonPin3 = 3; // the number of the pushbutton pin
const int ledPin12 = 12; // the number of the LED pin

// variables will change:
int buttonState2 = 0; // variable for reading the pushbutton status
int buttonState3 = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
// initialize the LED pin as an output:
pinMode(ledPin12, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin3, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
digitalWrite(13, !digitalRead(13)); // toggle state
delay(500); // wait around for 1 sec (1000 ms)
}



// read the state of the pushbutton value:
buttonState3 = digitalRead(buttonPin3);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState3 == HIGH) {
digitalWrite(12, !digitalRead(12)); // toggle state
delay(500); // wait around for 1 sec (1000 ms)

}else {
}
}
  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz 7 months ago +3
    A few points: (1) When entering code, please use the insert code menu item, so that it's properly formatted. You'll get less people helping if you make it hard for them to see your code properly. …
  • shabaz
    shabaz 7 months ago +2
    How AI would do it, with some prodding: // toggle.ino // rev 0.1 // buttons with pull-downs on pins 2 and 3 are to toggle outputs on pins 12 and 13 respectively // buttons are connected to the microcontroller…
  • robogary
    robogary 7 months ago +2
    I myself being a self admitted coding dunderhead, avoid the use of inversion in this case because its easy to overlook. I'd use direct commands if buttonState3 == HIGH then {digitalWrite (12, LOW) ;…
  • shabaz
    shabaz 7 months ago

    A few points:

    (1) When entering code, please use the insert code menu item, so that it's properly formatted. You'll get less people helping if you make it hard for them to see your code properly.

    (2) Your line checking for buttonState2 controls output 13, and your line checking button3 controls output12. That seems to be the opposite to what you want, so change the numbers on those lines.

    (3) You've configured pins 12 and 13 to be outputs, yet you read them using digitalRead. I don't know if that is valid or not in Arduino world. It might be OK, but not all microcontrollers necessarily will report back what you expect, if you try to read an output pin. It might not be an issue, but personally I would not do this unless I was sure what the behavior was. If you're in any doubt, then simply create two variables, called (say) state12 and state13 (these are terrible names by the way, but it's the sort of convention you seem to be using, so I've stuck with it), then set your outputs either high or low at startup, as you desire, and set those state variables to match. Then, you never need to try to do a digitalRead on those outputs, you can simply check your variable state, and update that along with your digitalWrite commands.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • balajivan1995
    balajivan1995 7 months ago

    Won't it be much easier to use interrupt rather than whatever this copy pasted code with absurd amount of blocking delay?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz 7 months ago

    How AI would do it, with some prodding:

    // toggle.ino
    // rev 0.1
    // buttons with pull-downs on pins 2 and 3 are to toggle outputs on pins 12 and 13 respectively
    // buttons are connected to the microcontroller positive rail when pressed. Pull-down resistors are used.
    
    #include <Arduino.h>
    
    // define the pins
    #define BUTTONA 2
    #define BUTTONB 3
    #define OUTPUTA 12
    #define OUTPUTB 13
    
    // define the states
    #define OFF 0
    #define ON 1
    
    // define the states of the buttons
    int buttonAState = OFF;
    int buttonBState = OFF;
    
    // define the states of the outputs
    int outputAState = OFF;
    int outputBState = OFF;
    
    void setup() {
      // set the button pins as inputs
      pinMode(BUTTONA, INPUT);
      pinMode(BUTTONB, INPUT);
      
      // set the output pins as outputs
      pinMode(OUTPUTA, OUTPUT);
      pinMode(OUTPUTB, OUTPUT);
      
      // set the initial states of the outputs
      digitalWrite(OUTPUTA, outputAState);
      digitalWrite(OUTPUTB, outputBState);
    }
    
    // debounce the buttons
    void debounce(int button) {
      delay(50);
      while (digitalRead(button) == ON) {
        delay(50);
      }
    }
    
    void loop() {
      // read the buttons
      buttonAState = digitalRead(BUTTONA);
      buttonBState = digitalRead(BUTTONB);
      
      // if button A is pressed, toggle output A
      if (buttonAState == ON) {
        outputAState = !outputAState;
        digitalWrite(OUTPUTA, outputAState);
        debounce(BUTTONA);
      }
      
      // if button B is pressed, toggle output B
      if (buttonBState == ON) {
        outputBState = !outputBState;
        digitalWrite(OUTPUTB, outputBState);
        debounce(BUTTONB);
      }
    }
    

    This is still not ideal, but has a chance of working (might need some mods, it is untested). I'd still do it differently, but this code should be easier to follow for a newcomer to Arduino.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • robogary
    robogary 7 months ago

    I myself being a self admitted coding dunderhead, avoid the use of inversion in this case because its easy to overlook. 

    I'd use direct commands if buttonState3 == HIGH  then {digitalWrite  (12, LOW) ;}   else {digitalWrite(12,HIGH);} 

    The time delays are for debouncing ?    If so, as you add code, it ties up the IO scan time. 

    To make things more complicated, I'll edge detect, create a "last scan" value. If the value != last scan value ( state changes on an input), it then goes off to do the desired action without waiting a couple seconds. A time delay can be put elsewhere as needed.  

    I also dont like putting pin #s as a VARIABLE because I make an IO map assignment before I start a small project, altho a couple times would have been convenient. I acquiescence to software folks who want to smack me for even suggesting that party foul  :-)  In my defense, Im not writing 1000000 lines of code, not creating libraries, and dont code with team members   :-) 

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • hardip
    hardip 23 days ago

    const int buttonPin2 = 2; // the number of the pushbutton pin
    const int ledPin13 = 13; // the number of the LED pin
    const int buttonPin3 = 3; // the number of the pushbutton pin
    const int ledPin12 = 12; // the number of the LED pin
    
    volatile byte state12 = LOW;  // variable that will be updated in the ISR
    volatile byte state13 = LOW;  // variable that will be updated in the ISR
    
    int prev_12=0;
    int prev_13=0;
    
    void pin12(){state12 = !state12;}
    void pin13(){state13 = !state13;}
    
    void setup() {
      pinMode(buttonPin2, INPUT_PULLUP);
      pinMode(ledPin13, OUTPUT);
      pinMode(buttonPin3, INPUT_PULLUP);
      pinMode(ledPin12, OUTPUT);
    
      attachInterrupt(digitalPinToInterrupt(buttonPin2), pin12, RISING);
      attachInterrupt(digitalPinToInterrupt(buttonPin3), pin13, RISING);
    }
    
    void loop() {
      if(prev_12 != state12 || prev_13 != state13){
        prev_12 = state12;
        prev_13 = state13;
        digitalWrite(ledPin12,state12);
        digitalWrite(ledPin13,state13);}
        delay(100);
    }

    • 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