Micro and push button

Hi, I'm a novice here so please excuse my lack of knowledge. I have a micro hooked up to a push button in a corn harvester. The reason being while harvesting along the idea is to enter a keyboard stroke to "cycle" the harvesting software. It then records an interesting spot in the field you would then plan to soil sample or look at at another point in time.

I have a 10k resistor from 5v and pin 2 to one side of the button, and a connection to ground on the other side. It works well except I often get random stray button pushes when you engage anything else electrical in the combine. I thought the resistor was supposed to ensure there wasn't any noise but there's a lot. Can anyone help? I really appreciate any help!

#include <Keyboard.h>

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button is pressed
  if (digitalRead(2) == LOW) {
    //Send an ASCII 'C',
    Keyboard.write(67);
  }
}

Your switch (all switches do) needs debouncing to prevent false pushes detected.

Look for switch debouncing in this forum and elsewhere on the internet.

1 Like

you can use a debounce software, hereafter an example:

int inputPin = 7;
int counter = 0;
int buttonState = 0;
int lastButtonState = 0;

int currentButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  pinMode(inputPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  currentButtonState = digitalRead(inputPin);

  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (currentButtonState != buttonState) {
      buttonState = currentButtonState;
      if (buttonState == LOW) {
        counter++;
        Serial.println(counter);
      }
    }
  }
  lastButtonState = currentButtonState;
}

Ok, debounceing sounds like it should help. So do you guys think something like this will work? I'm not currently near the arduino or combine to test.

#include <Keyboard.h>

int inputPin = 2;
int counter = 0;
int buttonState = 0;
int lastButtonState = 0;

int currentButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  currentButtonState = digitalRead(2);

  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (currentButtonState != buttonState) {
      buttonState = currentButtonState;
      if (buttonState == LOW) {
        counter++;
    //Send an ASCII 'C',
    Keyboard.write(67);
      }
    }
  }
  lastButtonState = currentButtonState;
}

And I would just change the "unsigned long debounceDelay = 50" less or more to adjust? Just to be clear what exactly is the code doing? Does it mean you need to hold the button for at least 50ms to work (which would be good I think) or it's only going to be looking for the button press every 50ms? Because what i'm seeing is when i'm doing something unrelated to the button like adjusting a rotor's speed or turning lights on and off it'll set off the micro.

Thank you for the help thus far

With long wires in an electrically noisy environment a stronger pullup (lower value pullup resistor) can help. Also add a 0.1 (or so, value not critical) from the input side of the switch to ground will help to bypass noise to ground and away from the processor.

image

Hello chuff26

Welcome to the worldbest Arduino forum ever.

Take a view to gain the knowledge.

Have a nice day and enjoy coding in C++.

Thank you all for the help! I ended up wiring it differently and also adding the debounce code and it's working really well now. Here's what I did if it'll help someone in the future.

#include <Keyboard.h>

int inputPin = 2;
int counter = 0;
int buttonState = 0;
int lastButtonState = 0;

int currentButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 250;

void setup() {
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  currentButtonState = digitalRead(2);

  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (currentButtonState != buttonState) {
      buttonState = currentButtonState;
      if (buttonState == HIGH) {
        counter++;
    Keyboard.write(67);
      }
    }
  }
  lastButtonState = currentButtonState;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.