#include <FastLED.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
unsigned long prevTime = millis();
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int progValue1 = digitalRead(2);
int progValue2 = digitalRead(3);
int redLED = 10;
int blueLED = 11;
volatile int buttonState1;
volatile int buttonState2;
volatile int buttonState3;
int progValue = 0;
int progValuePrec = 0;
#define DATA_PIN 6
#define NUM_LEDS 30
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS 255
#define FRAMES_PER_SECOND 5
CRGB leds[NUM_LEDS];
int sensorPin = A0;
int sensorValue = 500;
void buttonInterrupt2() {
buttonState2 = digitalRead(buttonPin2);
sensorValue = analogRead(sensorPin);
if (buttonState2 == HIGH){
progValue = 1;
}
}
void buttonInterrupt3() {
buttonState3 = digitalRead(buttonPin3);
sensorValue = analogRead(sensorPin);
if (buttonState3 == HIGH){
progValue = 2;
}
}
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight();
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip).setDither(BRIGHTNESS < 255);
FastLED.setBrightness(BRIGHTNESS);
attachInterrupt(digitalPinToInterrupt(buttonPin2), buttonInterrupt2, CHANGE );
attachInterrupt(digitalPinToInterrupt(buttonPin3), buttonInterrupt3, CHANGE );
}
void lcdProgValue(){
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Prg:"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print(progValue);
}
void heartPulsing(int currentTime, int prevTime) {
// Set all LEDs to red high
fill_solid(leds, NUM_LEDS, CRGB(128,0,0));
lcdProgValue();
FastLED.show();
if (progValue != 1){
return;
}
delay(analogRead(sensorPin));
// Set all LEDs to black
fill_solid(leds, NUM_LEDS, CRGB::Black);
lcdProgValue();
FastLED.show();
if (progValue != 1){
return;
}
delay(analogRead(sensorPin));
// Set all LEDs to red low
fill_solid(leds, NUM_LEDS, CRGB(32,0,0));
lcdProgValue();
FastLED.show();
if (progValue != 1){
return;
}
delay(analogRead(sensorPin));
// Set all LEDs to black
fill_solid(leds, NUM_LEDS, CRGB::Black);
lcdProgValue();
FastLED.show();
if (progValue != 1){
return;
}
delay((analogRead(sensorPin))*2);
}
void heartPulsingBis(int currentTime, int prevTime) {
// Set all LEDs to green high
fill_solid(leds, NUM_LEDS, CRGB(0,128,0));
FastLED.show();
if (progValue != 2){
return;
}
delay(analogRead(sensorPin));
// Set all LEDs to black
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
if (progValue != 2){
return;
}
delay(analogRead(sensorPin));
// Set all LEDs to green low
fill_solid(leds, NUM_LEDS, CRGB(0,32,0));
FastLED.show();
if (progValue != 2){
return;
}
delay(analogRead(sensorPin));
// Set all LEDs to black
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
if (progValue != 2){
return;
}
delay((analogRead(sensorPin))*2);
}
void loop() {
unsigned long currentTime = millis();
if (progValue == 0) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
}
if (progValue == 1) {
heartPulsing(currentTime, prevTime);
digitalWrite(blueLED, LOW);
digitalWrite(redLED, HIGH);
}
if (progValue == 2) {
heartPulsingBis(currentTime, prevTime);
digitalWrite(blueLED, HIGH);
digitalWrite(redLED, LOW);
}
// Serial.println(FRAMES_PER_SECOND);
// Serial.println((sensorValue / 20) +1);
Serial.println(progValue);
}