- Joined
- Apr 21, 2024
- Messages
- 1
- Reaction score
- 0
How can I solve it so that 'rainbowEffect()' function runs smoothly and when I press the button, it exits the while loop and continues in the 'switch' element.
#include <TinyDebug.h>
// Pinek definiálása
#define BUTTON_PIN PB4
#define RED_PIN PB2
#define GREEN_PIN PB1
#define BLUE_PIN PB0
int buttonState = HIGH;
int lastButtonState = HIGH;
int counter = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(RED_PIN, 0);
digitalWrite(GREEN_PIN, 0);
digitalWrite(BLUE_PIN, 0);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
counter++;
if (counter == 6) {
counter = 1;
}
switch (counter) {
case 1:
setColor(255, 0, 0); // Piros
break;
case 2:
setColor(0, 255, 0); // Zöld
break;
case 3:
setColor(0, 0, 255); // Kék
break;
case 4:
rainbowEffect();
break;
case 5:
setColor(0, 0, 0);
break;
}
}
}
}
lastButtonState = reading;
}
// Ez a függvény beállítja az RGB LED színét a megadott értékek alapján
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
// Ez a függvény létrehozza a szivárvány hatását
void rainbowEffect() {
int delayTime = 10; // Az átmenetek közötti késleltetési idő (milliszekundumban)
int increment = 1; // Színváltozás sebessége
while (digitalRead(BUTTON_PIN) == HIGH) { // Folyamatosan fut a szivárvány hatás
for (int hue = 0; hue < 360; hue += increment) {
float radHue = radians(hue);
int red = int(cos(radHue) * 127 + 128);
int green = int(cos(radHue + radians(120)) * 127 + 128);
int blue = int(cos(radHue + radians(240)) * 127 + 128);
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
delay(delayTime);
if (buttonState == LOW) {
Debug.begin();
Debug.println(digitalRead(BUTTON_PIN));
}
}
}
}
#include <TinyDebug.h>
// Pinek definiálása
#define BUTTON_PIN PB4
#define RED_PIN PB2
#define GREEN_PIN PB1
#define BLUE_PIN PB0
int buttonState = HIGH;
int lastButtonState = HIGH;
int counter = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(RED_PIN, 0);
digitalWrite(GREEN_PIN, 0);
digitalWrite(BLUE_PIN, 0);
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
counter++;
if (counter == 6) {
counter = 1;
}
switch (counter) {
case 1:
setColor(255, 0, 0); // Piros
break;
case 2:
setColor(0, 255, 0); // Zöld
break;
case 3:
setColor(0, 0, 255); // Kék
break;
case 4:
rainbowEffect();
break;
case 5:
setColor(0, 0, 0);
break;
}
}
}
}
lastButtonState = reading;
}
// Ez a függvény beállítja az RGB LED színét a megadott értékek alapján
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}
// Ez a függvény létrehozza a szivárvány hatását
void rainbowEffect() {
int delayTime = 10; // Az átmenetek közötti késleltetési idő (milliszekundumban)
int increment = 1; // Színváltozás sebessége
while (digitalRead(BUTTON_PIN) == HIGH) { // Folyamatosan fut a szivárvány hatás
for (int hue = 0; hue < 360; hue += increment) {
float radHue = radians(hue);
int red = int(cos(radHue) * 127 + 128);
int green = int(cos(radHue + radians(120)) * 127 + 128);
int blue = int(cos(radHue + radians(240)) * 127 + 128);
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
delay(delayTime);
if (buttonState == LOW) {
Debug.begin();
Debug.println(digitalRead(BUTTON_PIN));
}
}
}
}