1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
int led = 13;
int buttonPin1 = 7;
int buttonPin2 = 8;
int button1State = 0;
int button2State = 0;
int dataFromBt;
boolean lightBlink = false;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(9600); // Start bluetooth serial at 9600
pinMode(led, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
if (bluetooth.available()) // If the bluetooth sent any characters
{delay(3);
// Send any characters the bluetooth prints to the serial monitor
Serial.println((char)bluetooth.read());
dataFromBt = bluetooth.read();
//Serial.println(dataFromBt);
if (dataFromBt == '1') {
Serial.println("led on");
digitalWrite(led, HIGH);
bluetooth.print("1");
}
if (dataFromBt == '0') {
Serial.println("led off");
digitalWrite(led, LOW);
bluetooth.print("0");
}
if (dataFromBt == 'b') {
Serial.println("a");
lightBlink = true;
} else {
lightBlink = false;
}
}
if (Serial.available()) // If stuff was typed in the serial monitor
{
delay(3);
// Send any characters the Serial monitor prints to the bluetooth
//String myStr = (String)Serial.read();
//char myStr1[] = "hello this is testing!";
//uint8_t payload[myStr.length() + 1];
//myStr.getBytes(payload, myStr.length()+1);
//int bytes=Serial.available();
//Serial.readBytes(buffer, startPosition, bytes);
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
if (lightBlink) {
digitalWrite(led, HIGH);
bluetooth.print("1");
Serial.println("HIGH");
delay(500);
digitalWrite(led, LOW);
bluetooth.print("0");
Serial.println("LOW");
delay(500);
}
//------arduino push button code----------------
button1State = digitalRead(buttonPin1);
button2State = digitalRead(buttonPin2);
if (button1State == HIGH) {
digitalWrite(led, HIGH);
bluetooth.print("1");
Serial.println("on");
}
if (button2State == HIGH) {
digitalWrite(led, LOW);
Serial.println("off");
bluetooth.print("0");
}
} |
Partager