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 388 subscribers
  • Views 819 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) ;…
Parents
  • hardip
    hardip 8 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
Reply
  • hardip
    hardip 8 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
Children
No Data
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