COMPLETE NOOB - How to code rocker switch

So I'm trying to create a switch panel for a flight simulator. I have wired 15 rocker switches (on/off switches) to the board. Each switch has 2 prongs. I wire one prong to one hole and another to another hole (say positive to A2 and negative to D4).

I downloaded the IDE and have been going over the tutorials. But all I can find are the darn LED light tutorials. And even then they seem so technical to me. Can anyone explain in layman terms how to program the switch on the board to act as an on off switch for say turning the lights on or off in the simulator?

What kind of code?

One side of switch to ground, other side of switch to input pin.
Input pin configured as INPUT_PULUP.

Switch is closed when a digitalRead on the pin returns LOW.

Wire one side of all of the switches together and then to GND on the Arduino. Wire the other side of the switches to individual pins on the Arduino but don't use pins 0 and 1 as they have a special function. With that wiring in place, when a switch is closed the Arduino pin will be connected to GND and that can be detected in the program, but what about when the switch is open ? What state will it be in ? As it stands, who knows, so it's time for some programming. Note the pinMode() function and the comment

Connect one of the switch pins to pin D2 and copy this program into the Arduino

const byte switchPin0 = 2;  //give the switch pin a name 

void setup()
{
  Serial.begin(9600); //start the Serial interface at 9600 baud
  pinMode(switchPin0, INPUT_PULLUP);  //the switch pin is an input pulled HIGH by an internal resistor
}

void loop()
{
  byte switchState0 = digitalRead(switchPin0);  //read the state of the input pin (HIGH or LOW)
  if (switchState0 == LOW)  //if it is LOW then the switch is closed
  {
    Serial.println("Switch 0 is closed"); //print a message
  }
}

Open the Serial monitor (Tools/Serial Monitor on the IDE)
Close the switch connected to pin 2 and you should see a message in the monitor

With 15 switches the pin numbers should be kept in an array, a row of variables with the same name and a number to tell which one.

https://www.arduino.cc/reference/en/language/variables/data-types/array/

Then the same code with the array name and a variable for what number pin to use can be used to read any pin.
The alternative is 15 sections of code to handle 15 different names to read 15 different switches -- makes a huge mess.

Please be happy with the simple code while learning, it will be easier and go quicker.
If you can turn a led ON/OFF you can turn a motor ON/OFF. The led is a cheap stand-in, easier to try things out with.

If you're making a sim controller, are you using a board capable of being an HID? The Arduino Micro and Leo and the PJRC Teensy's can all appear to a PC as stick(s), mouse and keys at the same time.
You can get sliders for trim pretty cheap!
https://www.mouser.com/Passive-Components/Potentiometers-Trimmers-Rheostats/Slide-Potentiometers/_/N-9q0yl

UKHeliBob:
Wire one side of all of the switches together and then to GND on the Arduino. Wire the other side of the switches to individual pins on the Arduino but don't use pins 0 and 1 as they have a special function. With that wiring in place, when a switch is closed the Arduino pin will be connected to GND and that can be detected in the program, but what about when the switch is open ? What state will it be in ? As it stands, who knows, so it's time for some programming. Note the pinMode() function and the comment

Connect one of the switch pins to pin D2 and copy this program into the Arduino

const byte switchPin0 = 2;  //give the switch pin a name 

void setup()
{
  Serial.begin(9600); //start the Serial interface at 9600 baud
  pinMode(switchPin0, INPUT_PULLUP);  //the switch pin is an input pulled HIGH by an internal resistor
}

void loop()
{
  byte switchState0 = digitalRead(switchPin0);  //read the state of the input pin (HIGH or LOW)
  if (switchState0 == LOW)  //if it is LOW then the switch is closed
  {
    Serial.println("Switch 0 is closed"); //print a message
  }
}




Open the Serial monitor (Tools/Serial Monitor on the IDE)
Close the switch connected to pin 2 and you should see a message in the monitor
[/quote

> UKHeliBob:
> Wire one side of all of the switches together and then to GND on the Arduino. Wire the other side of the switches to individual pins on the Arduino but don't use pins 0 and 1 as they have a special function. With that wiring in place, when a switch is closed the Arduino pin will be connected to GND and that can be detected in the program, but what about when the switch is open ? What state will it be in ? As it stands, who knows, so it's time for some programming. Note the pinMode() function and the comment 
> 
> Connect one of the switch pins to pin D2 and copy this program into the Arduino
> 
> 
> ```
> const byte switchPin0 = 2;  //give the switch pin a name 

void setup()
{
  Serial.begin(9600); //start the Serial interface at 9600 baud
  pinMode(switchPin0, INPUT_PULLUP);  //the switch pin is an input pulled HIGH by an internal resistor
}

void loop()
{
  byte switchState0 = digitalRead(switchPin0);  //read the state of the input pin (HIGH or LOW)
  if (switchState0 == LOW)  //if it is LOW then the switch is closed
  {
    Serial.println("Switch 0 is closed"); //print a message
  }
}




Open the Serial monitor (Tools/Serial Monitor on the IDE)
Close the switch connected to pin 2 and you should see a message in the monitor

That actually helped me understand a lot. But now you get to see how little I know. I had never done anything like this before. So it's my first time even to solder.

After looking online I thought it must be basically wire one side to A# or D# and the other side to another A# or D#.

In fact, in the code you provided I am unsure how to know if the pin # is for A or D? I mean, I know you said D2 but is there something different you would put if it was wired in A2?

And silly me has to go learn how to unsolder now lol.

You can learn how to use the button in this tutorial.

And then learn how to use a button to control an LED

For using multiple buttons, see this tutorial

There is a lot of material on the Arduino main site, a whole manual's worth. Here's just one page of links, the Foundations Page:

For beginners, start with

Microcontrollers:
-- Digital Pins: How the pins work and what it means for them to be configured as inputs or outputs.
-- Analog Input Pins: Details about the analog-to-digital conversion and other uses of the pins.
-- PWM: How the analogWrite() function simulates an analog output using pulse-width modulation.
-- Memory: The various types of memory available on the Arduino board.

Programming:
-- Language Reference: Discover the what each term of the Arduino code language means.
-- Variables: Understand how to define and use variables in a Sketch.
-- Functions: Learn how to define and use functions in a Sketch.

and a bit later:
-- Library: Using and installing Arduino Libraries.
-- Cores: Need to add a new board to the Arduino Software? Install the relate core and manage it.

Bookmark the Reference page and the Libraries page and the Tutorial links pages including Foundations. When you open the Arduino IDE, also open a web browser and load the Reference then add a tab for every page that refers to what you need for your project.

Code has to be exact to even compile. If you have any doubts what to write, look it up right away and reinforce what you know while filling in what you forgot and you will learn faster. If you get tired, take a break and go back with fresh eyes. Coding while tired is likely to put more bugs in than you take out.

in the code you provided I am unsure how to know if the pin # is for A or D? I mean, I know you said D2 but is there something different you would put if it was wired in A2?

Either pin 2 will work in the program. The "A" pins can do more that the "D" pins but you are not ready for that. The code expects pin D2 to be used (the D is not actually required). If you want to use pin A2 then replace the references to 2 in the program with A2

Did you try the circuit and program ?

jbearnolimits:
Can anyone explain in layman terms how to program the switch on the board to act as an on off switch for say turning the lights on or off in the simulator?

What kind of code?

Your code can read whether or not a digital pin (most analog pins and PWM pins are also digital pins) is HIGH or LOW.

There are 2 modes the pin can be set to read. One mode;

setMode( pin, INPUT );

has to be supplied voltage about 2.8V or more to make the pin HIGH while voltage about 1V or less makes the pin LOW.
If the voltage is in between, the pin stays the same --- very like how a thermostat works, look up hysteresis.

A pin set to INPUT connected to a switch connected to 5V will put 5V on the pin when the switch is closed but when the switch is opened the charge stays in the wire and pin so there must be another wire and resistor (usually 10K Ohms) on the pin connected to ground to drain that leftover charge so the pin can go LOW when the switch is opened. That wire and resistor to GND is called a pulldown. Switch closed, the 5V pulls the pin HIGH, switch open the pulldown makes it LOW.

The other mode;

setMode( pin, INPUT_PULLUP );

supplies the pin with very weak 5V (comes through a lot of resistance) and is switches special friend. It does the same HIGH/LOW as a pin set to INPUT, but HIGH and LOW mean different things since the pin gets V from the chip.

A pin set to INPUT_PULLUP connected to a switch connected to GND needs no extra wire and resistor.
When the switch is open the pin is 5V and reads HIGH. When the switch is closed, the 5V drains through the switch to GND.

Once your code gets fast enough you will have to deal with "bounce" but I think that's for later.

UKHeliBob:
Either pin 2 will work in the program. The "A" pins can do more that the "D" pins but you are not ready for that. The code expects pin D2 to be used (the D is not actually required). If you want to use pin A2 then replace the references to 2 in the program with A2

Did you try the circuit and program ?

I had to figure out how to upload the sketch but managed to do it. At least it said the upload succeed. But when I open serial monitor and start flipping the switch nothing happens. I also noticed the blue lights on the board don't light and only one green light is on.

Please provide a schematic of your project, hand drawn and photographed is good enough, and post a copy of the code you are using here

What baud rtae have you got selected in the Serial monitor ?

I think you need to put this project on a WAY back burner. You are nowhere near ready for something like this. Start with the very first tutorial, Blink, and work your way through them all. You aren't a question or two away from doing what you want. You are at the first step of "I want to learn how to write code". Some people pick it up fast, many don't. Work the blinking LED tutorials, try to modify them to do different things, and see which one you are. I was going to try my hand at Visual C, but have decided it is a lot more than I want to take on right now. Good luck and stick with it. It's fun once you are able to start creating things.
Joe

I will echo what @JoeWilson said above in #11.

I’ve been tinkering with Arduino and sketch writing for more than a year, and still consider myself somewhat of a newbie.

But what has made the difference is really stopping and studying all the online tutorials, going through then one line by one line, to learn how they work.

You need to understand the LED blink examples and how they work so it’s second nature before you proceed to bigger projects. There are loads of online tutorials that walk you through exactly how to code, how to do the connections, and why they work. You need to put the work in there first to study and understand. That includes both the software coding AND the hardware wiring. (Two related but distinct efforts.)

The problem you pose in your original post is essentially this: how to use a physical switch to blink a light. There are loads of tutorials on how to do that. (Actually, you don’t even need an Arduino to blink a light with a switch. Just a power source, light bulb, and a switch. Maybe a resistor if you’re using an LED instead of a resistive lamp.)

This is also where time spend on project design is really important. Have you drawn a picture for yourself of what you’re trying to do? Is an Arduino microcontroller even necessary? If so, why? Exploring all those questions for yourself will help with overall project design. (And also help forum members give really good advice to getting a project over the finish line.)

Anyway, the point is: once you’ve done a lot of exploring, reading, studying, and trial and error, then once you get some code and some wired up components, and if then you hit a brick wall, getting advice on the forum will be more valuable.

Sorry it takes so long to respond. Just started a new job. Anyway, yes I have decided to take a step back. I used to write code for websites so this one seems simple enough, it’s just different is all. But I think I need to learn a thing or two about soldering first because I’m pretty sure I must have burned the board up.

Because I wired it to one switch to test it out. D2 and GND (ground). Copied and pasted the code provided by UKHeliBob and uploaded it to the board and got nothing in the serial monitor when using the switch. It’s odd though because each time I would open a window in the IDE the blue light would flash so I know it was at least connected to the computer.

Edit: Header pins, DuPont wires and a breadboard can help you prototype without having to solder directly on the board for each connection

Well, took one more stab at it and ran some of the example codes and it looks like the board isn’t burned up after all because the serial monitor was able to read it. Just not getting anything with the code provided by UKHeliBob for some reason.

Forget about your project for now. Disconnect your Arduino from everything apart from the USB input and upload the code I posted. Connect a wire to digital pin 2, open the Serial monitor, check that the baud rate is set to 9600. Now, when you touch the other end of the wire to GND you should get a message on the Serial monitor.

If you get no message, then change the value of switchPin0 to the number of any pin except 0 and 1, upload again and connect the wire to your chosen pin. Does anything happen when you touch teh other end of the wire to GND ?

If you want to use one of the analogue pins then remember to use pin numbers A0, A1, A2 etc instead of just a number

UKHeliBob:
Forget about your project for now. Disconnect your Arduino from everything apart from the USB input and upload the code I posted. Connect a wire to digital pin 2, open the Serial monitor, check that the baud rate is set to 9600. Now, when you touch the other end of the wire to GND you should get a message on the Serial monitor.

If you get no message, then change the value of switchPin0 to the number of any pin except 0 and 1, upload again and connect the wire to your chosen pin. Does anything happen when you touch teh other end of the wire to GND ?

If you want to use one of the analogue pins then remember to use pin numbers A0, A1, A2 etc instead of just a number

Thanks, I did exactly what you said. Got nothing from touching D2 to GND. But when I used D3 and touched it to GND it worked. All of the other D pins (didn't use 1 or 0) worked by just changing the pin and putting that pin number in the code. Soooo defective board or maybe defective soldering to D2?

Soooo defective board or maybe defective soldering to D2?

It certainly looks like it

UKHeliBob:
It certainly looks like it

And we all know which one of the two is most likely lol. Thanks for the help.