Hello everybody,
I got some problems with switch case and millis. Basically, what I'm trying to do is to blinking a led according to the position of a rotary encoder. When the encoder goes up (from 0 to x) the assigned switch case starts and the led assigned in it blinks for 2 seconds using millis and after that the led is off. I want to use millis and not delay because I need to have the program keep going in case the encoder moves and a new case has to start.
Furthermore, when the encoder moves from an higher value for instance 4 (and in this case we are in the switch case 4), so the led assigned to that case blinks for 2000 millis and after that move automatically to previous case (therefore switch case 3). It's a timer with leds.
If you look at line 166 in the else/switch/case1 I've already tried to get this, but didn't work!
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
const int buttonPin = 4; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int encoder0PinA = 12;
int encoder0PinB = 11;
int encoder0Pos = -1;
int encoder0PinALast = LOW;
int n = LOW;
int encoderSwitchPin = 4; //push button switch
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 2000; // interval at which to blink (milliseconds)
//LED
#define PIN 6
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 5
int sine[5] = {0, 1, 2, 3, 4}; //these are the pixels in order of animation
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);
pixels.begin();
pixels.setBrightness(40); //adjust brightness here
pixels.show(); // Initialize all pixels to 'off'
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
n = digitalRead(encoder0PinA);
unsigned long currentMillis = millis();
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// turns ON the LED every two button pushes by checking the modulo of the button push counter.
if (buttonPushCounter % 2 == 0) {
if (buttonState == HIGH) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(75, 250, 100)); //change RGB color value here
pixels.show();
for (int j = 0; j < 5; j++) {
pixels.setPixelColor(j, pixels.Color(0, 0, 0));
}
pixels.show();
}
digitalWrite(ledPin, HIGH);
encoder0Pos = 0;
}
}
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
switch (encoder0Pos) {
//n = encoder0Pos;
case 0: // your hand is on the sensor
if ( encoder0Pos == 0) {
for (int i = 0; i <= NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
}
}
Serial.println("OFF");
break;
case 1: // your hand is on the sensor
pixels.setPixelColor(sine[1], pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 1");
break;
case 2: // your hand is close to the sensor
pixels.setPixelColor(sine[2], pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 2");
break;
case 3: // your hand is a few inches from the sensor
pixels.setPixelColor(sine[3], pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 3");
break;
case 4: // your hand is nowhere near the sensor
pixels.setPixelColor(sine[4], pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 4");
break;
}
if (encoder0Pos <= 0) {
encoder0Pos = 0;
}
}
else {
encoder0Pos++;
currentMillis = 0;
switch (encoder0Pos) {
// n = encoder0Pos;
case 0: // your hand is on the sensor
if ( encoder0Pos == 0) {
for (int i = 0; i <= NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
}
}
Serial.println("OFF");
break;
case 1: // your hand is on the sensor
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
pixels.setPixelColor(sine[0], pixels.Color(60, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 1");
}
else {
pixels.setPixelColor(sine[0], pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 1 OFF");
}
break;
case 2: // your hand is close to the sensor
pixels.setPixelColor(sine[1], pixels.Color(60, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 2");
break;
case 3: // your hand is a few inches from the sensor
pixels.setPixelColor(sine[2], pixels.Color(60, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 3");
break;
case 4: // your hand is nowhere near the sensor
pixels.setPixelColor(sine[3], pixels.Color(60, 0, 0)); // Moderately bright green color.
pixels.show();
Serial.println("LED 4");
break;
}
//Maximum value steps 24 that means 2 hrs
if (encoder0Pos >= 24) {
encoder0Pos = 24;
}
}
Serial.print (encoder0Pos); //Display steps
Serial.println (" ");
}
encoder0PinALast = n; //take count of encoder steps
}
I hope you can help me out, I cannot figure out how to deal it.