Hi, Guys.
Thanks for reading my topic. and always I appreciate this Forum.
Can I get some help?
first, my setting is..
Can you see this photo? Anyway.
I want to send if I press switch in arduino, then receive other arduino and turn on led via bluetooth.
So, 1.press switch 2. send data via bluetooth 3.receive data other arduino 4.then turn on led.
Here is my code. It is master HC-06 module.
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); //bluetooth pin set each Tx, Rx
int adc_key_val[5] = {30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key = -1;
int oldkey = -1;
int pin_OUT = A0;
void setup()
{
Serial.begin(9600);
pinMode(pin_OUT, INPUT);
while (!Serial) {
; //If the serial communication is not connected, stop the code execution and repeat the infinite loop
BTSerial.begin(9600);
}
}
void loop() {
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) { // if keypress is detected
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) {
oldkey = key;
if (key >= 0) {
switch (key)
{
case 0: {
Serial.println("Switch 1");
if (Serial.available()){
BTSerial.write('a');
}
}
break;
case 1: {
Serial.println("Switch 2");
if (Serial.available()){
BTSerial.write('b');
}
}
break;
case 2: {
Serial.println("Switch 3");
if (Serial.available()){
BTSerial.write('c');
}
}
break;
case 3: {
Serial.println("Switch 4");
if (Serial.available()){
BTSerial.write('d');
}
}
break;
case 4: {
Serial.println("Switch 5");
if (Serial.available()){
BTSerial.write('e');
}
}
break;
}
}
}
}
delay(10);
}
// Convert ADC value to key number
int get_key(unsigned int input) {
int k;
for (k = 0; k < NUM_KEYS; k++) {
if (input < adc_key_val[k]) {
return k;
}
}
if (k >= NUM_KEYS)k = -1; // No valid key pressed
return k;
}
and slave code.
I think it is completed. Just in case.
#include "FastLED.h"
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3);
// How many leds in your strip?
#define NUM_LEDS 255
#define COLOR_ORDER BGR
// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 11
#define CLOCK_PIN 12
// Define the array of leds
CRGB leds[NUM_LEDS];
// Set the brightness to use (the maximum is 31).
const uint8_t brightness = 1;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Serial.println("Hello World!");
BTSerial.begin(9600);
FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR>(leds, NUM_LEDS);
FastLED.setBrightness(brightness);
}
void R(uint16_t delayms, uint16_t delayms1){
fill_solid( leds, NUM_LEDS, CRGB::Yellow);
FastLED.show();
delay(delayms);
fill_solid( leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(delayms1);
}
void L(uint16_t delayms, uint16_t delayms1){
fill_solid( leds, NUM_LEDS, CRGB::Blue);
FastLED.show();
delay(delayms);
fill_solid( leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(delayms1);
}
void S(uint16_t delayms){
fill_solid( leds, NUM_LEDS, CRGB::Red);
FastLED.show();
delay(delayms);
}
void Lightsoff(uint16_t delayms){
fill_solid( leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(delayms);
}
void loop() {
if(BTSerial.available())
{
char bt;
bt = BTSerial.read();
Serial.write(BTSerial.read());
if(bt == 'a')
R(500,500);
if(bt == 'b')
L(500,500);
if(bt == 'c')
S(1000);
if(bt == 'd')
Lightsoff(1000);
}
}
Thank you!