#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define NUMLED 3
#define DATAPIN 5
int buzzer= 9;
int button= 2;
char c;
bool notifica=true;
SoftwareSerial bluetooth (6, 7); //TX and RX pins
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMLED, DATAPIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(buzzer,OUTPUT);
pinMode(5,OUTPUT);
pinMode(button,INPUT_PULLUP);
bluetooth.begin(38400);
delay(100);
pixels.begin();
delay(100);
attachInterrupt(digitalPinToInterrupt(2), buttonpressed, FALLING); //I chose FALLING because it works when the pin goes from high to low
}
void loop() {
if(bluetooth.available() > 0){ //slave (reader) search the main device (master as sender)
c = bluetooth.read(); //slave reads data from master
if(c=='1'){
if (notifica){
analogWrite(buzzer,200);
pixels.setPixelColor(0, pixels.Color(0, 0, 150));
pixels.setPixelColor(1, pixels.Color(0, 0, 150));
pixels.setPixelColor(2, pixels.Color(0, 0, 150));
pixels.show();
delay(2000);
analogWrite(buzzer,0);
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.setPixelColor(1, pixels.Color(0, 0, 0));
pixels.setPixelColor(2, pixels.Color(0, 0, 0));
pixels.show();
}
}
}
}
void buttonpressed() { //button as interrupt allows the user to have notification or not
if (notifica==true) { //condition 1
notifica=false;
}
else {
notifica=true; //condition 2
}
}