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 Making toggle switch work in game
  • 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
  • State Verified Answer
  • Replies 4 replies
  • Subscribers 392 subscribers
  • Views 928 views
  • Users 0 members are here
  • code
  • toggle_switch
Related

Making toggle switch work in game

pot82es
pot82es over 9 years ago

Hi, I just got the Arduino micro, and I'm very bad at writing code...

I found this code down below at http://forum.arduino.cc/index.php?topic=121129.0

 

My problem is that when I press the toggle switch it just spits out G's with 100ms delay, and stops when I turn the toggle switch off. My goal is to make it write a G only once on my computer when the toggle switch is pressed on or off (changing state). This way I can make a toggle switch that retracts a landing gear ingame whenever its pressed. I do not know what to do next... I'm hoping anyone have a solution. Thanks.

 

GIF by silviustro

 

image

 

#include <Keyboard.h>

boolean toggleState; // hold current state of switch

boolean lastToggleState;  // hold last state to sense when switch is changed

long toggleTimer = millis();  // // debounce timer

const int toggleSwitchPin = 9;

 

 

 

 

void setup(){

// setup stuff here

}

 

 

void loop(){

 

 

toggleState = digitalRead(toggleSwitchPin);

 

 

if (millis() - toggleTimer > 100){  // debounce switch 100ms timer

  if (toggleState && !lastToggleState) {  // if switch is on but was previously off

    lastToggleState = true;  // switch is now on

    toggleTimer = millis();  // reset timer

    Keyboard.write('G');

  }

 

 

  if (!toggleState && lastToggleState){  // if switch is off but was previously on

    lastToggleState = false; // switch is now off

    toggleTimer = millis(); // reset timer

    }

  }

}

 

Code is originally written by

Goofballtech

  • Sign in to reply
  • Cancel

Top Replies

  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago +1
    Your answer is a few post down from the one your code came from Toggle switch act like a button?
Parents
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago

    Your answer is a few post down from the one your code came from image

    Toggle switch act like a button?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • pot82es
    0 pot82es over 9 years ago in reply to Robert Peter Oakes

    Ok, but I do not understand how, in his example it says do stuff here on both, so i set keyboard write on both. Could u give me an example?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to pot82es

    If you look at your first code sample, it has initialization stuff in setup, thats what needs to go into the setup() block in this one. the poster just did not repeat obvious stuff.

     

    In the loop() structures you add the code to write the "G" to the serial ports etc,

     

    if you look at the two posts you will see the difference, this post simply shows the code needed to handle the non repeating output to the serial port

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to pot82es

    If you look at your first code sample, it has initialization stuff in setup, thats what needs to go into the setup() block in this one. the poster just did not repeat obvious stuff.

     

    In the loop() structures you add the code to write the "G" to the serial ports etc,

     

    if you look at the two posts you will see the difference, this post simply shows the code needed to handle the non repeating output to the serial port

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
  • pot82es
    0 pot82es over 9 years ago in reply to Robert Peter Oakes

    Thank you, I believe got it image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • 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