#include <EEPROM.h>
unsigned int receivedValue = 0;
// Define pins
const int ledPin1 = 9; // PB0, PWM-capable pin for LED1
const int ledPin2 = 10; // PB4, pin for LED2
const int buttonMode = 2; // PB1
const int buttonUp = 3; // PB2
const int buttonDown = 4; // PB3
// Variables to store the state of the LEDs, brightness, and blink rate
enum Mode { ALL_OFF, LED1_ON, LED2_ON, LED1_BLINK };
Mode currentMode = ALL_OFF;
unsigned long blinkStartTime = 0;
int brightness = 128; // Initial brightness (range 0-255)
int blinkRate = 1000; // Initial blink rate in milliseconds
const int brightnessStep = 10; // Adjust brightness by this value
const int blinkRateStep = 200; // Adjust blink rate by this value
void setup() {
Serial.begin(9600);
// Initialize the LED pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
//Serial.begin(9600);
// Initialize the button pins as inputs with internal pull-up resistors
pinMode(buttonMode, INPUT_PULLUP);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
// Read the last saved brightness and blink rate from EEPROM
brightness = EEPROM.read(0);
blinkRate = EEPROM.read(1) * 10;
// If the values read from EEPROM are not in the valid range, reset to defaults
if (brightness < 0 || brightness > 255) {
brightness = 128;
}
if (blinkRate < 10 || blinkRate > 2000) {
blinkRate = 1000;
}
// Set initial LED state to all off
updateLEDs();
}
void loop() {
if (Serial.available()) {
Serial.println("Data From VC-02");
// Read the incoming bytes
byte highByte = Serial.read();
byte lowByte = Serial.read();
// Combine the two bytes into a single 16-bit value
receivedValue = (highByte << 8) | lowByte;
// Print the received value in HEX format
Serial.print("Received HEX value: 0x");
Serial.println(receivedValue, HEX);
}
if (receivedValue == 0xA190) // Check if the value is A2 in HEX
{
//myservo.write(90); // sets the servo position according to the scaled value
delay(15);
}
else if (receivedValue == 0xA145) {
//myservo.write(45); // sets the servo position according to the scaled value
delay(15);
}
else
{
}
delay(10);
receivedValue = 0;
// Check the mode button
if (digitalRead(buttonMode) == LOW) {
delay(50); // Debounce delay
if (digitalRead(buttonMode) == LOW) {
// Cycle through the modes
if (currentMode == ALL_OFF) {
currentMode = LED1_ON;
} else if (currentMode == LED1_ON) {
currentMode = LED2_ON;
} else if (currentMode == LED2_ON) {
currentMode = LED1_BLINK;
} else {
currentMode = ALL_OFF;
}
updateLEDs();
while (digitalRead(buttonMode) == LOW); // Wait for button release
}
}
// Handle blinking mode
if (currentMode == LED1_BLINK) {
unsigned long currentTime = millis();
if (currentTime - blinkStartTime >= blinkRate) {
blinkStartTime = currentTime;
digitalWrite(ledPin1, !digitalRead(ledPin1));
}
}
// Check the brightness up button
if (digitalRead(buttonUp) == LOW) {
delay(50); // Debounce delay
if (digitalRead(buttonUp) == LOW) {
if (currentMode != LED1_BLINK) {
// Increase brightness
brightness = min(255, brightness + brightnessStep);
updateLEDs();
EEPROM.write(0, brightness);
} else {
// Increase blink rate (decrease delay)
blinkRate = max(200, blinkRate - blinkRateStep);
EEPROM.write(1, blinkRate / 10);
}
while (digitalRead(buttonUp) == LOW); // Wait for button release
}
}
// Check the brightness down button
if (digitalRead(buttonDown) == LOW) {
delay(50); // Debounce delay
if (digitalRead(buttonDown) == LOW) {
if (currentMode != LED1_BLINK) {
// Decrease brightness
brightness = max(0, brightness - brightnessStep);
updateLEDs();
EEPROM.write(0, brightness);
} else {
// Decrease blink rate (increase delay)
blinkRate = min(2000, blinkRate + blinkRateStep);
EEPROM.write(1, blinkRate / 10);
}
while (digitalRead(buttonDown) == LOW); // Wait for button release
}
}
}
void updateLEDs() {
switch (currentMode) {
case LED1_ON:
analogWrite(ledPin1, brightness);
digitalWrite(ledPin2, LOW);
break;
case LED2_ON:
analogWrite(ledPin1, 0);
digitalWrite(ledPin2, HIGH);
break;
case LED1_BLINK:
blinkStartTime = millis(); // Reset blink timer
digitalWrite(ledPin2, LOW);
break;
case ALL_OFF:
analogWrite(ledPin1, 0);
digitalWrite(ledPin2, LOW);
break;
}
}
//**********************************************************************************************//