I bought a bluetooth shield from elecfreaks [http://www.elecfreaks.com/store/bluetooth-shield-shd18-p-233.html]
I inserted the following code when i press (a) the led at pin 13 should light but nothing happens the rx led only light i need your help to make the arduino receive not send data and making the code work because i want to use the bluetooth shield to control a simple robot car thanks for your help .
#include <SoftwareSerial.h>
const int rxpin = 2; // pin used to receive
const int txpin = 3;
// pin used to send to
SoftwareSerial bluetooth(rxpin, txpin); // new serial port on given pins
int led=13;
char c;
void setup()
{
Serial.begin(9600);
bluetooth.begin(9600); // initialize the software serial port
Serial.println("Serial ready");
bluetooth.println("Bluetooth ready");
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
pinMode(rxpin,INPUT);
pinMode(txpin,OUTPUT);
}
void loop()
{
if (bluetooth.available())
{
char c = (char)bluetooth.read();
Serial.write(c);
}
if (Serial.available())
{
char c = (char)Serial.read();
bluetooth.write(c);
}
if(c=='a'){
digitalWrite(led,HIGH);
}
if(c=='b'){
digitalWrite(led,LOW);
}
}
I inserted the following code when i press (a) the led at pin 13 should light
Really? Why do you think that?
You have 3 variables named c. One never gets assigned a value, but that is the one you test. The other two get assigned values, but they go out of scope immediately afterwards.
Get your act together and use different names for the three variables, or use just ONE variable. Then, something might actually happen.
I modified the code as u said but the led lights only when i send (a) from the serial monitor not the bluetooth terminal
this is the new code
#include <SoftwareSerial.h>
const int rxpin = 2; // pin used to receive
const int txpin = 3;
// pin used to send to
SoftwareSerial bluetooth(rxpin, txpin); // new serial port on given pins
int led=13;
char c;
void setup()
{
Serial.begin(9600);
bluetooth.begin(9600); // initialize the software serial port
Serial.println("Serial ready");
bluetooth.println("Bluetooth ready");
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
pinMode(rxpin,INPUT);
pinMode(txpin,OUTPUT);
}
void loop()
{
if (bluetooth.available())
{
c = bluetooth.read();
Serial.write(c);
}
if (Serial.available())
{
c = Serial.read();
bluetooth.write(c);
}
if(c=='a'){
digitalWrite(led,HIGH);
}
if(c=='b'){
digitalWrite(led,LOW);
}
}