Tact switch mimicking a keypress

I want to use 4 tact switches to mimic the WASD keys on my keyboard and then send that over bluetooth to my robot. Here is my code which is a huge work in progress. The bluetooth will connect but the buttons wont do anything.

const int button = 4;
const int button1 = 5;
const int button2 = 2;
const int button3 = 3;
int buttonState; 
int buttonState1;
int buttonState2;
int buttonState3;
int lastButtonState = LOW;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 6
#define TxD 7
 
#define DEBUG_ENABLED  1

String retSymb = "+RTINQ=";//start symble when there's any return
String slaveName = ";Bluetooth_Bee_V2";// caution that ';'must be included, and make sure the slave name is right.
int nameIndex = 0;
int addrIndex = 0;

String recvBuf;
String slaveAddr;

String connectCmd = "\r\n+CONN=";

SoftwareSerial blueToothSerial(RxD,TxD);
 

void setup(){
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  //wait 1s and flush the serial buffer
  delay(1000);
  Serial.flush();
  blueToothSerial.flush();
  pinMode(button, INPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
}

void loop(){
  int reading = digitalRead(button);
  int reading1 = digitalRead(button1);
  int reading2 = digitalRead(button2);
  int reading3 = digitalRead(button3);
  
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
  
  
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if (reading2 != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if (reading3 != lastButtonState3) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

but the buttons wont do anything.

And, you haven't told us how they are wired, so you can't really be expecting help.

Your code isn't even complete. It won't compile as posted, and it isn't properly posted.

The loop() function is called over and over, in an infinite loop. There is no reason to be running an infinite loop in an infinite loop.

Try again.

pickel98:
I want to use 4 tact switches to mimic the WASD keys on my keyboard and then send that over bluetooth to my robot. Here is my code which is a huge work in progress. The bluetooth will connect but the buttons wont do anything.

I'm struggling to figure out what you're trying to achieve. You have a 'bot which is designed to be controlled from a keyboard? Is that your PC keyboard? If so, it seems that either you want your Arduino to connect to your PC and emulate a keyboard, in which case you should be looking into USB HID emulation, or you have an application on the PC which receives commands from the Arduino and generates the keystrokes for you (if the PC is running Windows then you could use Gobetwino for that part).

Perhaps this would be a good time to step back, explain what devices you have in your system and how they are connected to each other and exactly what you expect to happen in between you pressing a 'tact switch' (?) and your 'bot receiving the corresponding command.

oops, accidentally didn't copy the whole thing. I have the four buttons wired up identically. I took a picture but cant figure out how to post it so if you want me to send it to you some how i can. I want the four tact switches to make the robot move forward, backwards, left, and right over bluetooth. I have an arduino uno with a bluetooth shield and a robotshop rover. I can get the bluetooth to connect but i cant get the button presses to be sent and have the robot act accordingly. Here it is:

const int button = 4;
const int button1 = 5;
const int button2 = 2;
const int button3 = 3;
int buttonState; 
int buttonState1;
int buttonState2;
int buttonState3;
int lastButtonState = LOW;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 6
#define TxD 7
 
#define DEBUG_ENABLED  1

String retSymb = "+RTINQ=";//start symble when there's any return
String slaveName = ";Bluetooth_Bee_V2";// caution that ';'must be included, and make sure the slave name is right.
int nameIndex = 0;
int addrIndex = 0;

String recvBuf;
String slaveAddr;

String connectCmd = "\r\n+CONN=";

SoftwareSerial blueToothSerial(RxD,TxD);
 

void setup(){
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  //wait 1s and flush the serial buffer
  delay(1000);
  Serial.flush();
  blueToothSerial.flush();
  pinMode(button, INPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
}

void loop(){
  int reading = digitalRead(button);
  int reading1 = digitalRead(button1);
  int reading2 = digitalRead(button2);
  int reading3 = digitalRead(button3);
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  if (reading1 != lastButtonState1) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if (reading2 != lastButtonState2) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  if (reading3 != lastButtonState3) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
 if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        Serial.write(119);
        }
    }
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading1 != buttonState1) {
      buttonState1 = reading1;

      // only toggle the LED if the new button state is HIGH
      if (buttonState1 == HIGH) {
        Serial.write(97);
      }
    }
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading2 != buttonState2) {
      buttonState2 = reading2;

      // only toggle the LED if the new button state is HIGH
      if (buttonState2 == HIGH) {
        Serial.write(100);
      }
    }
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading3 != buttonState3) {
      buttonState3 = reading3;

      // only toggle the LED if the new button state is HIGH
      if (buttonState3 == HIGH) {
        Serial.println(115);
      }
    }
  } 
  
  
  lastButtonState = reading;
  lastButtonState1 = reading1;
  lastButtonState2 = reading2;
  lastButtonState3 = reading3;
     }
     }	
 	
 }
 void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=1\r\n");//set the bluetooth work in master mode
  blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n");//set the bluetooth name as "SeeedBTMaster"
  blueToothSerial.print("\r\n+STPIN=1234\r\n");//set pincode for master. Has to be the same as slave
  blueToothSerial.print("\r\n+STAUTO=0\r\n");// Auto-connection is forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.flush();
  blueToothSerial.print("\r\n+INQ=1\r\n");//make the master inquire
  Serial.println("Master is inquiring!");
  delay(2000); // This delay is required.
    
  //find the target slave
  char recvChar;
  while(1){
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      recvBuf += recvChar;
      nameIndex = recvBuf.indexOf(slaveName);//get the position of slave name
      //nameIndex -= 1;//decrease the ';' in front of the slave name, to get the position of the end of the slave address
      if ( nameIndex != -1 ){
        //Serial.print(recvBuf);
 	addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());//get the start position of slave address	 		
 	slaveAddr = recvBuf.substring(addrIndex, nameIndex);//get the string of slave address 			
 	break;
      }
    }
  }
  //form the full connection command
  connectCmd += slaveAddr;
  connectCmd += "\r\n";
  int connectOK = 0;
  Serial.print("Connecting to slave:");
  Serial.print(slaveAddr);
  Serial.println(slaveName);
  //connecting the slave till they are connected
  do{
    blueToothSerial.print(connectCmd);//send connection command
    recvBuf = "";
    while(1){
      if(blueToothSerial.available()){
        recvChar = blueToothSerial.read();
 	recvBuf += recvChar;
 	if(recvBuf.indexOf("CONNECT:OK") != -1){
          connectOK = 1;
 	  Serial.println("Connected!");
 	  blueToothSerial.print("Connected!");
 	  break;
 	}else if(recvBuf.indexOf("CONNECT:FAIL") != -1){
 	  Serial.println("Connect again!");
 	  break;
 	}
      }
    }
  }while(0 == connectOK);
}

The code is still not posted correctly. There is a sticky at the top of the forum. It DOES apply to you. Read it.

The code STILL has a useless infinite loop that needs explaining or removing.

The fact that the switches are all wired the same way doesn't mean squat if they are all wired wrong.

There is nothing in your post that explains why you are only reading the switch states when serial data arrives.

I don't know how you would like me to post this code then. Also what sticky? Can you also give me some pointers here?Can you explain these loops. I'm still relatively new to this. The switch is hooked up right because i ran it with another code and all of them worked correctly.

If you want the Arduino and the rover to communicate directly, then the Arduino will have to send what the rover is expecting to receive. What is the rover, and what is it expecting to receive over the bluetooth link?

here is what the rover is running:

#include <Servo.h>
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
#define trigpin 11
#define echopin 10
Servo servo;
unsigned long duration, distance;
int servoPin = 9;
int minPulse = 900;
int maxPulse = 2100;
int turnRate = 100;
int refreshTime = 20;
int centerServo = 90 ;
int pulseWidth;
int moveServo;
int leftspeed = 255; //255 is maximum speed 
 int rightspeed = 255;
 int slowspeed = 0;
 int nospeed = 0;
long lastPulse = 0;


void setup(void)
{
 pinMode(servoPin, OUTPUT);
 pinMode(trigpin, OUTPUT);
 pinMode(echopin, INPUT);
  centerServo = maxPulse - ((maxPulse - minPulse)/2);
  pulseWidth = centerServo; 
 int i;
 for(i=5;i<=8;i++)
 pinMode(i, OUTPUT);
 Serial.begin(9600);
}
void loop(void)
{
    digitalWrite(trigpin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigpin, LOW);
 duration = pulseIn(echopin, HIGH);
 distance = (duration/2) / 29.1;
 Serial.print("Distance: ");
Serial.println(distance);
  if (distance < 8 ) {
    halt (slowspeed, nospeed);
    Serial.println(distance);}
 while (Serial.available() < 1) {} // Wait until a character is received
 char val = Serial.read(); 
 switch(val) // Perform an action depending on the command
 {
 case 'w'://Move Forward
 forward (leftspeed,rightspeed);
 Serial.println("Moving Forward");
 break;
 case 's'://Move Backwards
 reverse (leftspeed,rightspeed);
 break;
 case 'a'://Turn Left
 left (leftspeed,rightspeed);
 break;
 case 'd'://Turn Right
 right (leftspeed,rightspeed);
 break;
 case 'f': //Stop
 halt (slowspeed,nospeed);
 break;
 case 'e':
 servo.write(170);
 break;
 case 'q':
 servo.write(30);
 break;
 case 'r':
 servo.write(90);
 break;
 default:
 stop();
 break;
 } 
 
 if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
   if (pulseWidth < minPulse) { pulseWidth = minPulse; }
   
 if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // start the pulse
    delayMicroseconds(pulseWidth);  // pulse width
    digitalWrite(servoPin, LOW);    // stop the pulse
    lastPulse = millis();           // save the time of the last pulse
  }
}
void stop(void) //Stop
{
 digitalWrite(E1,LOW);
 digitalWrite(E2,LOW);
}
void forward(char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,LOW);
 analogWrite (E2,b);
 digitalWrite(M2,LOW);}
void reverse (char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,HIGH);
 analogWrite (E2,b);
 digitalWrite(M2,HIGH);
}
void left (char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,HIGH);
 analogWrite (E2,b);
 digitalWrite(M2,LOW);
}
void right (char a,char b)
{
 analogWrite (E1,a);
 digitalWrite(M1,LOW);
 analogWrite (E2,b);
 digitalWrite(M2,HIGH);
}
void halt (char a, char b)
{
  analogWrite (E1,a);
  digitalWrite(M1, LOW);
  analogWrite (E2,b);
  digitalWrite(M2, LOW);
}

Any particular reason to use the hardware serial port on the rover? You're using software serial on the sender, and if that works on the rover too then it would free up the hardware serial port for diagnostic logging.

Given that you don't have basic comms working yet I suggest you write a simple sender that just sends a simple fixed string over bluetooth at regular intervals e.g. once per second, and write a simple receiver that just logs everything it receives to the hardware serial port, and use the serial monitor to see what it receives.

@pickel98:
What PaulS is desiring you to do (what is expected of all forum members) is to adhere to the guidelines here:
http://forum.arduino.cc/index.php/topic,148850.0.html

Go back and edit your posts to use code tags, looks like the pound-sign icon in the ribbon bar or a hash-tag. When clicked, the editor will have two tags: leading one the start of code and the closing one the end of code. You paste your code between these two tags! Be certain to read all the rules on formatting, etc.

Ray

@mrburnette. Thank you for explaining this to me with curtsy and respect. Something that PaulS was lacking. I have fixed my posts. Thank you again for the information.

@PeterH, I was using Serial because that was what the rover manual had as an example. I am still new to this so I noticed that you said there was another option. If you could please help me out there that would be excellent. Thank you again for all your help!

pickel98:
If you could please help me out there that would be excellent.

You are already using SoftwareSerial in the sender sketch.

I guess I'm not tracking what your wanting me to do then, can you explain further please

I guess I'm not tracking what your wanting me to do then, can you explain further please

How difficult are these instructions?
Put every { on a new line.
Put every } on a new line.
Use Tools + Auto Format.
Post your code properly.

Code that wanders all over the page like a drunken sailor is hard to read. Since it is trivial for you to make sure that doesn't happen, I, for one, refuse to even try to follow code that isn't properly formatted.

if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
   if (pulseWidth < minPulse) { pulseWidth = minPulse; }
   
 if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // start the pulse
    delayMicroseconds(pulseWidth);  // pulse width
    digitalWrite(servoPin, LOW);    // stop the pulse
    lastPulse = millis();           // save the time of the last pulse
  }

What is this mess doing? The Servo class takes care of all that!

I tried using the auto format already and it stated that it was canceled because of too many right curly braces. Also If you don't mind PaulS this forum is an open place for knowledge. Just because I am here seeking your help on this matter does not give you the right to be disrespectful. Please adjust your attitude before posting again, Thanks.

I tried using the auto format already and it stated that it was canceled because of too many right curly braces

That usually implies you sketch is syntactically incorrect and won't compile either.

The bluetooth will connect but the buttons wont do anything.

Your posted code doesn't even compile, so stop whining about PaulS asking you to format it properly.

How can you complain that it doesn't work, if you can't even upload the sketch?

pickel98:
I don't know how you would like me to post this code then. Also what sticky?

The "sticky" at the head of every part of the forum. That sticky.

Read this before posting a programming question

How to use this forum

Nick, I have already corrected the stated issues. We have moved on from that point. Thanks. If you read further you will find the corrected code.
Thank you AWOL for explaining that to me. Like I believe I mentioned in my first post I am new to this so any information is helpful. Thank you again.

pickel98:
Nick, I have already corrected the stated issues. We have moved on from that point. Thanks. If you read further you will find the corrected code.

Now would you follow PaulS' advice please, and correct the formatting in your code and then repost it? It makes it much easier for us to understand.

Put every { on a new line.
Put every } on a new line.
Use Tools + Auto Format.
Post your code properly.

I have previously suggested using SoftwareSerial instead of the hardware Serial port. I guess your reply "I guess I'm not tracking what your wanting me to do then, can you explain further please" was in response to that. You're already using SoftwareSerial in one of your sketches and I'm suggesting you use it in the other one too.