Hi, direkt zum nächsten Problem wo mich Google nicht direkt weiter bringt.
Frage vorab: Was ist für eine int-Variable besser und performanter? Mehrere IF-Abfragen oder ein switch-case?
Anwendung: NeoPixel mit Arduino UNO als Controller.
Stolperstein: Bei Tastendruck soll das Programm gewechselt werden.
Problem: Wenn ich z.B. bei Case "2" auf den Button drücke, macht er erst noch seinen Task komplett zu Ende, dann hat er gemerkt, hey der Zähler wurde erhöht und springt zum nächsten passenden case
Ziel: Er soll sofort zum nächsten Case gehen, wie kann ich hier ein Abbruch o.ä. erzwingen?
Hier mein Versuch mit Interrupt:
// NeoPixel
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 60
#define BRIGHT 50
#define DELAY 15
const int potPin1 = 1;
const int potPin2 = 2;
const int potPin3 = 3;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = LOW; // current state of the button
int lastButtonState = 0; // previous state of the button
int nrProgs = 3; // number of different programs in switch-case
void setup() {
pixels.begin();
pixels.clear();
pinMode(buttonPin, INPUT_PULLUP);
// "Bei wechselnder Flanke auf dem Interruptpin" --> "Führe die ISR aus"
attachInterrupt(digitalPinToInterrupt(buttonPin), increment, RISING);
// ONLY FOR DEBUGGING & TESTING
Serial.begin(9600);
Serial.println("Starte 5Prog:");
}
void loop() {
// reset buttonPushCounter
if (buttonPushCounter > nrProgs)
buttonPushCounter = 0;
// Switch-Case to run different LED programs
switch (buttonPushCounter) {
Serial.println("SW");
case 0:
Serial.println("0");
FillPot();
break;
case 1:
Serial.println("1");
Fade3In();
Fade3Out();
break;
case 2:
Serial.println("2");
PartyModus(10, 3);
break;
case 3:
Serial.println("3");
for (int i = 0; i < NUMPIXELS; i++)
setWhite(i);
break;
default:
pixels.clear();
break;
}
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
// if (buttonPushCounter % 4 == 0) {
// digitalWrite(ledPin, HIGH);
// } else {
// digitalWrite(ledPin, LOW);
// }
}
void increment(){
Serial.println("increment"); // ONLY FOR DEBUGGING
buttonPushCounter++;
}
// LED Programs - ONLY FOR TEST PURPOSE, STILL NOT FINAL
// ReadIn from 3 potentiometers with gamma-correction
void FillPot() {
pixels.fill(pixels.Color(pixels.gamma8(analogRead(potPin2) >> 2), pixels.gamma8(analogRead(potPin1) >> 2), pixels.gamma8(analogRead(potPin3) >> 2)), 0, NUMPIXELS);
pixels.show();
delay(50);
}
void setWhite(unsigned int px) {
setColorDelay(px, 255, 255, 255);
}
// FadeIn Begin2End
void Fade3In() {
for (int i = 0; i < NUMPIXELS; i += 3) {
setColorDelay(i, BRIGHT, 0, 0);
setColorDelay(i + 1, 0, BRIGHT, 0);
setColorDelay(i + 2, 0, 0, BRIGHT);
}
}
// FadeOff End2Begin
void Fade3Out() {
for (int i = NUMPIXELS - 1; i > 1; i -= 3) {
setColorDelay(i, 0, 0, 0);
setColorDelay(i - 1, 0, 0, 0);
setColorDelay(i - 2, 0, 0, 0);
}
}
void PartyModus(unsigned int del, unsigned int crazyness) {
// Party Modus!
// More Random pixels.setPixelColor(random(60), 0); makes it more crazy
// When less set 0, then reduce random to (200/120/120)
// delay(30); makes it more quite
pixels.setPixelColor(random(60), pixels.Color(pixels.gamma8(random(200)), pixels.gamma8(random(120)), pixels.gamma8(random(120))));
for (int i = 0; i < crazyness; i++)
pixels.setPixelColor(random(60), 0);
pixels.show();
delay(del);
}
void setColorDelay(unsigned int px, unsigned int r, unsigned int g, unsigned int b) {
pixels.setPixelColor(px, pixels.Color(r, g, b));
delay(DELAY);
pixels.show();
}