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);
}
}
{}. – uint128_t Mar 04 '16 at 15:49static, otherwise it's reset to zero (actually, recreated) every time you enterloop().