1

Good day everyone, I'm returning to the Arduino world after my a 3 year hiatus following the birth of my son.

I'm trying to push a button connected to pin 2 and have that send a 'd' to SoftwareSerial on pins 10,11. This should trigger an output back from the device connected to the SoftwareSerial.

I'm encountering an issue:

When I press the button it sends about 15 d's. I would like to limit this to just one char per button press, I attempted to build an if statement but it seems to ignore the conditions.

#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int button1State = 0;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Start each software serial port
  portOne.begin(4800);
  while (!portOne) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

// the loop function runs over and over again forever
void loop() {
  int Once = 0;
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH ) { 
    if (Once == 0) {
      Once = 1;
      // turn LED on:
      digitalWrite(ledPin, HIGH);
      // send D to Radio
      portOne.write("d");
      Serial.write("d");
      Serial.print(Once);
      Serial.println();
      delay(1);
    }
  }
  else (buttonState == LOW) {
    // turn LED off:
    Once = 0;

    digitalWrite(ledPin, LOW);

  }
  portOne.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (portOne.available() > 0) {
    char inByte = portOne.read();
    Serial.print(inByte);
  }
}
  • There's built in code formatting in the post editor, use it to format your code correctly. It looks like {}. – uint128_t Mar 4 '16 at 15:49
  • 1
    1) You should debounce your button. 2) You should qualify Once as static, otherwise it's reset to zero (actually, recreated) every time you enter loop(). – Edgar Bonet Mar 4 '16 at 16:21
  • Thank you Edgar, qualifying Once as static fixed it! I didn't realize it was getting cleared everytime, driving me crazy! – Tony M Mar 4 '16 at 17:48
  • Now comes the second part. When I first boot up the device connected to portOne I see the boot-up message, however after I press the switch and see d1 on the serial monitor I get nothing back from the other device. – Tony M Mar 4 '16 at 17:50
  • When I connect the device directly to my computer and press d I see a five line status output from the device. Based on my Sketch that should be echo'd back to the serial monitor however I see nothing but the d1, any ideas? – Tony M Mar 4 '16 at 17:53
4

As pointed out by Edgar you should debounce the button. Perhaps increasing the delay() should do the trick.

Try with 100-200 milliseconds.

With this edit you can delete Once unless you are planning on using it somewhere else.

| improve this answer | |
  • 2
    If he deletes Once, it will repeatedly send characters when the button is held down. – NobodyNada Mar 4 '16 at 19:36
0

As Edgar stated my issue was I didn't make the Once variable static

"1) You should debounce your button. 2) You should qualify Once as static, otherwise it's reset to zero (actually, recreated) every time you enter loop()."

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.