Всем привет. Написал код для AVR Attiny 2313 - для управления светодиодной лентой. Не являюсь проф. разработчиком, так хобби. Подскажите на сколько код корректный ??? В живую все работает, а с точки зрения программирования , что можно улучшить ??? Как добавить управление контрастностью??? СПАСИБО.
const int BLED=7; //Blue LED on Pin 9
const int RLED=11; //Green LED on Pin 10
const int GLED=12; //Red LED on Pin 11
const uint16_t PWM_FREQ = 5000; // PWM frequency in Hertz
uint16_t fadeValue = 0; // Current PWM value
bool fadeUp = true; // Direction of PWM fade
const int BUTTON = 5; //The Button is connected to pin 2
const int power = 9; //power led
boolean lastButton = LOW; //Last Button State
boolean currentButton = LOW; //Current Button State
int ledMode = 0; //Cycle between LED states
void setup()
{
// Set Timer/Counter1 control registers for Fast PWM mode with ICR1 as top
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
// Set top value for Timer/Counter1 based on desired PWM frequency
ICR1 = F_CPU / (2 * PWM_FREQ) - 1;
pinMode (BLED, OUTPUT); //Set Blue LED as Output
pinMode (GLED, OUTPUT); //Set Green LED as Output
pinMode (RLED, OUTPUT); //Set Red LED as Output
pinMode (power, OUTPUT);
pinMode (BUTTON, INPUT); //Set button as input (not required)
}
void fadeLed() {
de
// Fade LED 1
analogWrite(BLED, fadeValue);
// Fade LED 2 with inverted PWM value of LED 1
analogWrite(RLED, ICR1 - fadeValue);
// Fade LED 3 with inverted PWM value of LED 2
analogWrite(GLED, fadeValue);
// Update fade value for next cycle
if (fadeUp) {
fadeValue++;
if (fadeValue >= ICR1) {
fadeUp = false;
}
}
else {
fadeValue--;
if (fadeValue <= 0) {
fadeUp = true;
}
}
delay(10); // Delay between steps of fade effect
}
void setMode(int mode)
{
//ALL
if (mode == 1)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, HIGH);
}
//BLUE
else if (mode == 2)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
//GREEN
else if (mode == 3)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
//RED
else if (mode == 4)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
else if (mode == 5)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
}
else if (mode == 6)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, HIGH);
}
else if (mode == 7)
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, HIGH);
}
else if (mode == 8)
{
fadeLed();
}
//OFF (mode = 0)
else
{
digitalWrite(RLED, LOW);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
}
}
boolean debounce(boolean last)
{
boolean current = digitalRead(BUTTON); //Read the button state
if (last != current) //if it's different...
{
delay(5); //wait 5ms
current = digitalRead(BUTTON); //read it again
}
return current; //return the current value
}
void loop()
{
digitalWrite(power, HIGH);
currentButton = debounce(lastButton); //read debounced state
if (lastButton == LOW && currentButton == HIGH) //if it was pressed...
{
ledMode++; //increment the LED value
}
lastButton = currentButton; //reset button value
if (ledMode == 9) ledMode = 0;
setMode(ledMode); //change the LED state
}