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 823 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
  • 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
Reply
  • 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
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