Issues with no output being pushed

I have an Arduino Leonardo which i am doing a proof of concept with for my flight sim. Currently, I am just using standard microswitches that are usually used for rc remotes. Some are simple on off switches some are momentary switches. I have the wiring done correctly to the best of my knowledge, as i have power running to switch, signal wire running to input and in parallel i have a 220 ohm resistor also coming from signal side of switch going to ground to avoid a floating switch. I have done this for all 12 inputs, and I feel like there is a better way to do this.

Now, I have had simple code written that works for my application but I need to be able to throw my switch and not have the input to repeat over and over, so i am using arguments on a loop to solve this, but I believe there is something wrong with my code. I have only written out 2 of the total 12 inputs i have wired up as a test after wiring my board and I get no outputs. I just have my outputs as typing a number as a keyboard. This is how I am going to do my switches anyways, so I'll just have to change mapping on my simulator to get everything set up.

So my question is threefold:

  1. Is my coding correct
  2. Is there a more efficient way of checking change of input value (high or low) without causing a noticeably long delay while working with 12 inputs (maybe more in the future).
  3. I am sure there is a better way to avoid a floating switch than wire a single 220 ohm resister on EVERY input, if so please let me know.

I am still learning and have been able to make this work with continuous outputs being triggered, but as I said I just want the output to trigger once until a change of state is detected.

I have looked around a lot and the way to do this but a lot of people's suggestions on other posts seems to confuse me.

#include <Keyboard.h>

int input01 = A0
int input01 = A1
int input01 = A2
int input01 = A3
int input01 = A4
int input01 = A5
int input01 = 01
int input01 = 02
int input01 = 03
int input01 = 04
int input01 = 05
int input01 = 06

// Current States of Switches

int input01c = 0;
int input02c = 0;
int input03c = 0;
int input04c = 0;
int input05c = 0;
int input06c = 0;
int input07c = 0;
int input08c = 0;
int input09c = 0;
int input10c = 0;
int input11c = 0;
int input12c = 0;

// Previous states of switches

int input01p = 0;
int input02p = 0;
int input03p = 0;
int input04p = 0;
int input05p = 0;
int input06p = 0;
int input07p = 0;
int input08p = 0;
int input09p = 0;
int input10p = 0;
int input11p = 0;
int input12p = 0;

void setup() {
pinMode(input01, INPUT);
pinMode(input02, INPUT);
pinMode(input03, INPUT);
pinMode(input04, INPUT);
pinMode(input05, INPUT);
pinMode(input06, INPUT);
pinMode(input07, INPUT);
pinMode(input08, INPUT);
pinMode(input09, INPUT);
pinMode(input10, INPUT);
pinMode(input11, INPUT);
pinMode(input12, INPUT);
}
void loop() {
input01c = digitalRead(input01);
input02c = digitalRead(input02);
input01c = digitalRead(input03);
input02c = digitalRead(input04);
input01c = digitalRead(input05);
input02c = digitalRead(input06);
input01c = digitalRead(input07);
input02c = digitalRead(input08);
input01c = digitalRead(input09);
input02c = digitalRead(input10);
input01c = digitalRead(input11);
input02c = digitalRead(input12);

if (input01c == input01p) {
    if (input01c == HIGH) {
    Keyboard.write(1);
    } 
    else {
      // Do nothing
    }
    delay(20);
  }
  input01c = input01p;
if (input02c == input02p) {
    if (input02c == HIGH) {
    Keyboard.write(2);
    } 
    else {
      // Do nothing
    }
    delay(20);
  }
  input02c = input02p;
}
if (input01c == input01p) {

You need to test for change but you are testing for no change. You need

if (input01c != input01p) {

Wire switches between input and ground and use INPUT_PULLUP instead of the resistors. You will need to reverse your logic as low will represent button pressed.
Use bool for storing your input states.
There's a tutorial somewhere on inputs and buttons, I'd post a link but I can't remember where it is and I'm on my mobile phone, making finding things difficult. Lose the delays, you need to learn to code without them. Delay will cause you problems when your code gets more complex.

You also need to consider de-bouncing.

Hi,
What is this supposed to mean?

int input01 = A0
int input01 = A1
int input01 = A2
int input01 = A3
int input01 = A4
int input01 = A5
int input01 = 01
int input01 = 02
int input01 = 03
int input01 = 04
int input01 = 05
int input01 = 06

Can I suggest this for pin labeling and also to fix one of your code problems.

int input01Pin = A0
int input02Pin = A1
int input03Pin = A2
int input04Pin = A3
int input05Pin = A4
int input06Pin = A5
int input07Pin = 01
int input08Pin = 02
int input09Pin = 03
int input10Pin = 04
int input11Pin = 05
int input12Pin = 06

This way you know that the variable is a pin number and not a value.
Tom... :slight_smile:

Following on from my previous reply...
state change detection for help on detecting button presses without using delay.

Also, if you have something like this:

int input01 = A0
int input01 = A1
int input01 = A2
int input01 = A3
int input01 = A4
...

It's time to consider an array
int inputs[] = {A1, A2, A3, A4, ...};

You might also find this helpful
keypad data entry

I forgot to change the input 01s to the proper numbers, it isn't that way in my code I copy pasted in the middle of editing and demoed out haha.

For arrays, if I code

Int inputs[3] = {A0,A1,A2}

how do I call upon individual inputs?
Is it done this way to call upon a2?

Inputs[2]

also, for values in an array, such as input01c, how do I modify it? Is it like this? This is assuming these arrays are predefined and I am just trying to modify the value of one line item in an array. Let us say it is the 3rd value in an array of 12.

InputsC[2] = 2

Also, and I know this is a lot of questions but I'm really just trying to get a better grasp of this and not try trying to just get all the answers, can I pull the value of a single item in an array and apply it to another single item in another array? I feel like this is a dumb question but I just really want to be sure.

As for the debouncing without delays, I have read many an article but I fail to understand what part of the code listed in debouncing codes I see is the part that debounces the input. I am usually pretty good at discerning as long as I have some explanation, but as I see here, it is really confusing me.

I figured out the deblouncing, by calling the mills function and using logic to set the delay time of mills, so ignore my confusion over debluncing XD literally found a post that spells it out in plain english 2 mins after posting this hahaha.

I am going to try and rewrite my code and post a more complete version as soon as I can either find these solutions or some of you helpful peeps let me know. I really like problem solving and figuring things out but Holy cow so many hours trying this is starting to test my sanity haha.

Top of the page, Documentation / Reference / Array or direct link array - Arduino Reference

Steve

Got it Steve, I guess I just needed to see it again for the 10th time to actually understand it. No Idea why it did not stick when I looked at it before. Arrays are nice, just kinda confusing to someone who has really never used them before. I am currently rewriting my code and hopefully I can get that posted and see if I need to do any tweaking of any kind. I'm much closer to my goal thanks to you guys I really do appreciate it.

So I have been working all night on this code and I can not get an output. I have my pin A0 and A1 wired to their own switches, and the other sides ran to ground. Attached is my revised code. I am sure I am making some silly syntax error of some sort.

#include <Keyboard.h>

// Define digial input array

int rawinput[13] = {A0,A1,A2,A3,A4,A5,0,1,2,3,4,5};

// Define Current state of button array

int inputc[12];

// Define Previous state of button array

int inputp[12];

void setup() {

 // initialize the button pin as a input
{
  for (int i=0; i<12; i++)
    pinMode(rawinput[i],INPUT_PULLUP);
}
int inputp[12] = {HIGH};
}
void loop() {
  // read the switch input pin:
  inputc[0] = digitalRead(A0);
  inputc[1] = digitalRead(A1);
  inputc[2] = digitalRead(A2);
  inputc[3] = digitalRead(A3);
  inputc[4] = digitalRead(A4);
  inputc[5] = digitalRead(A5);
  inputc[6] = digitalRead(0);
  inputc[7] = digitalRead(1);
  inputc[8] = digitalRead(2);
  inputc[9] = digitalRead(3);
  inputc[10] = digitalRead(4);
  inputc[11] = digitalRead(5);

  // compare the buttonState to its previous state
  if (inputc[0] != inputp[0]) {
    // if the state has changed, increment the counter
    if (inputc[0] == LOW) {
    Keyboard.write(1);
    } 
    else {
      // Do nothing
    }
    // Delay a little bit to avoid bouncing
    delay(20);
  };
  // save the current state as the last state, for next time through the loop
  inputc[0] = inputp[0] ;

  // compare the buttonState to its previous state
  if (inputc[1] != inputp[1]) {
    // if the state has changed, increment the counter
    if (inputc[1] == LOW) {
    Keyboard.write(2);
    } 
    else {
      // Do nothing
    }
    // Delay a little bit to avoid bouncing
    delay(20);
  };
// save the current state as the last state, for next time through the loop
inputc[1] = inputp[1] ;
}

I am keeping the delays until I can get a hang of the debouncing script, and I will be addings bools as I continue to develop this code. Currently, I am just trying to get this to give me a simple output. I apologize for all the questions. I am trying to read a lot of guides to help me on my way, it is just hard to combine it all into one working code.

Not sure what this is for:

  int inputp[12] = {HIGH};

It declares a new array which immediately goes out of scope so it does nothing.

This does nothing useful:

  // save the current state as the last state, for next time through the loop
  inputc[0] = inputp[0] ;

Because next time round loop inputc[0] will be overwritten by the digitalRead at the top.

So I have been working all night on this code and I can not get an output.

I am sure I am making some silly syntax error of some sort.

Get some sleep then! What seems like an impossible problem when tired will be easy after some sleep, happens to all of us.

Please, in the IDE select tools / auto format before posting code, it will tidy it up.

int rawinput[13] = {A0, A1, A2, A3, A4, A5, 0, 1, 2, 3, 4, 5};

Why 13? Is this just as symptom of being tired? Not that it is the cause of your problem.

Instead of (or as well as)

Keyboard.write(1);

Use

Serial.print("Button pressed");

Or something similar because you know the serial monitor works.

Get 1 or 2 buttons working before introducing all the others, make things simpler.

I was going to point out some other problems, but wildbill beat me to it.

Don't worry about asking questions, the people that annoy us are the ones that don't make any effort, clearly you are making an effort, the questions are fine.

Now sleep!!!

Wildbill, I am trying to declare all 12 entries of array inputp as HIGH in setup before anything is ran in case any switches are thrown prior to powering on the Arduino with this. Array inputc is current read input value on each loop run, and Array inputp is the previous value from last run

int inputp[12] = {HIGH};

I feel like this might not be correct but from what I've seen I believe this might work.

I flipped my 2 arrays in the wrong direction, inputp[0] should reflect inputc[0] 's value at the end of each check.

Yes the 13 is an error i made cause of being tired. I will get some sleep soon but I really with I could get some progress with this. You know how it is.

Did not know about auto formating, that saves me a lot of time. I have been doing it manually this whole time.

So with those corrections my code still does not output anything on the serial or the keyboard

#include <Keyboard.h>

// Define digial input array

int rawinput[12] = {A0, A1, A2, A3, A4, A5, 0, 1, 2, 3, 4, 5};

// Define Current state of button array

int inputc[12];

// Define Previous state of button array

int inputp[12];

void setup() {

  // initialize the button pin as a input
  {
    for (int i = 0; i < 12; i++)
      pinMode(rawinput[i], INPUT_PULLUP);
  }
  int inputp[12] = {HIGH};
}
void loop() {
  // read the pushbutton input pin:
  inputc[0] = digitalRead(A0);
  inputc[1] = digitalRead(A1);
  inputc[2] = digitalRead(A2);
  inputc[3] = digitalRead(A3);
  inputc[4] = digitalRead(A4);
  inputc[5] = digitalRead(A5);
  inputc[6] = digitalRead(0);
  inputc[7] = digitalRead(1);
  inputc[8] = digitalRead(2);
  inputc[9] = digitalRead(3);
  inputc[10] = digitalRead(4);
  inputc[11] = digitalRead(5);

  // compare the buttonState to its previous state
  if (inputc[0] != inputp[0]) {
    // if the state has changed, increment the counter
    if (inputc[0] == LOW) {
      Keyboard.write(1);
      Serial.print("Button 0 pressed");
    }
    else {
      // Do nothing
    }
    // Delay a little bit to avoid bouncing
    delay(20);
  };
  // save the current state as the last state, for next time through the loop
  inputp[0] = inputc[0] ;

  // compare the buttonState to its previous state
  if (inputc[1] != inputp[1]) {
    // if the state has changed, increment the counter
    if (inputc[1] == LOW) {
      Keyboard.write(2);
      Serial.print("Button 1 pressed");
    }
    else {
      // Do nothing
    }
    // Delay a little bit to avoid bouncing
    delay(20);
  };
  // save the current state as the last state, for next time through the loop
  inputp[1] = inputc[1] ;
}

I feel I might be going about this the complete wrong way.

If you want to set all the values in an array to something you need to use the same for loop technique you used for setting all the pinModes in that other array, as described in that documentation you keep re-reading (it really might go in better after some sleep!).

Steve

Thanks Steve, I think I finally get it, after like 4 cups of coffee.

Just to verify, instead of

int inputp[12] = {HIGH};

in my setup, change it to

  for (int i = 0; i < 12; i++) {
    (inputp[i] = HIGH);
  }

i am FINALLY understanding for syntax, I was so confusted why "i" had so many arguements attached to it and now i understand that it just means start here, under these conditions, and at what increment, and do that to the array under those arguments.
That being said, yes, PerryBebbington, I am doing this at the rate of only 2 buttons at the moment, and I have completely stripped my board of my test rig and have gone back to just getting 2 to work.
Lastly, with these changes, this should work right? I need to rebuild my proof of concept rig as i needed to completely strip it due to being switched to pullup instead of just standard readouts. Kinda a bummer, I now got 250 220 ohm resistors just waiting to be used. Guess I'll need to incorporate a ton of LEDs later on huh?

  for (int i = 0; i < 12; i++) {
    (inputp[i] = HIGH);
  }

Much better.

I now got 250 220 ohm resistors just waiting to be used.

I've got thousands of resistors, every value I will never need! Just keep collecting components, then you will always have some to hand. Also collect power supplies from old electronic kit that's being thrown out, that way you always have a power supply for whatever you are making.

grandnagusquark:
Lastly, with these changes, this should work right?

You'll need to post your latest code for us to be able to tell you that.

Awesome!

I am hoping that soon my financial situation gets better so I can buy some new breadboards and better wires rather than this cruddy bell wire I am using so I can get tests up and running faster. I ordered 20 Breadboards online at 17$ a pop a few months ago and EVERY ONE OF THEM were DOA. Company said they dont guarantee hobby items will work or not on shipping, and did not refund or send working ones. So in short, I have to solder wires and twist stuff together in order for me to test my code.

I'll go and make some more coffee, then get back to work on this. Will update.

Also, when all this is said and done, I will be releasing all my CAD, code, and wiring schematics to all of you to use if you want to have a nice modular flight sim setup. But that is a while from now, and my 3D printer is in need of some love to get back to proper tolerances.

Wildbill, here is my updated code:

#include <Keyboard.h>

// Define digial input array

int rawinput[13] = {A0, A1, A2, A3, A4, A5, 0, 1, 2, 3, 4, 5};

// Define Current state of button array

int inputc[12];

// Define Previous state of button array

int inputp[12];

void setup() {

  // initialize the button pin as a input
  for (int i = 0; i < 12; i++) {
    pinMode(rawinput[i], INPUT_PULLUP);
  }
  //Set all inputs to HIGH, in case any switches are thrown on init of Arduino
  for (int i = 0; i < 12; i++) {
    (inputp[i] = HIGH);
  }
}
void loop() {
  // read the pushbutton input pin:
  inputc[0] = digitalRead(A0);
  inputc[1] = digitalRead(A1);
  inputc[2] = digitalRead(A2);
  inputc[3] = digitalRead(A3);
  inputc[4] = digitalRead(A4);
  inputc[5] = digitalRead(A5);
  inputc[6] = digitalRead(0);
  inputc[7] = digitalRead(1);
  inputc[8] = digitalRead(2);
  inputc[9] = digitalRead(3);
  inputc[10] = digitalRead(4);
  inputc[11] = digitalRead(5);

  // compare the buttonState to its previous state
  if (inputc[0] != inputp[0]) {
    // if the state has changed, increment the counter
    if (inputc[0] == LOW) {
      Keyboard.write(1);
      Serial.print("Button 0 pressed");
    }
    else {
      // Do nothing
    }
    // Delay a little bit to avoid bouncing
    delay(20);
  };
  // save the current state as the last state, for next time through the loop
  inputp[0] = inputc[0] ;

  // compare the buttonState to its previous state
  if (inputc[1] != inputp[1]) {
    // if the state has changed, increment the counter
    if (inputc[1] == LOW) {
      Keyboard.write(2);
      Serial.print("Button 1 pressed");
    }
    else {
      // Do nothing
    }
    // Delay a little bit to avoid bouncing
    delay(20);
  };
  // save the current state as the last state, for next time through the loop
  inputp[1] = inputc[1] ;
}

20 breadboards! 17$ each?! Is that USD or another country that uses a dollar of some sort?

I've got loads of these, they seem to work pretty well:

USD, but they were quite large and came with a mounting board and a few probes a well. All was junk. I have a Breadboard somewhere around here but it barely works either.

Things are improving but I don't see a Serial.begin or a Keyboard.begin.

Also, you're using pins 0 and 1 as digital inputs, which I assume are the ones the serial port uses on a Leonardo just as they are on an Uno. Probably best to substitute some different ones.