Olá Amigos.

Iniciei a poucos dias o estudo de programação e eletrônica em Arduino, até o momento considero que já progredi bastante.
Estou trabalhando em uma fita de Led Digital endereçavel, usando a biblioteca "Adafruit_NeoPixel" que ajuda em muito em efeitos para a fita digital.

Porem estou com problemas em fazer a mudança dos efeitos.

Tenho 2 botões;

Botão 1: passa de efeito em efeito "case 1 ao case 4" = Funcionando perfeitamente

Botão 2: Vai direto para o efeito do "case 4" = Sem sucesso

Botão 1 esta funcionando perfeitamente, porem o Botão 2, não consigo fazer, se coloco direto a função ela não funciona, o efeito só funciona dentro do "case" devido ao código do millis.


Alguém teriam alguma ideia ou solução ?

#include <Adafruit_NeoPixel.h>
#define PIN 2
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800);
const int buttonPin = 7;
const int buttonPin2 = 6;

// Variables will change:
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
long previousMillis;

int neoPixel_j = 0;

int nPatterns = 4;
int lightPattern = 0;

uint16_t color;

//----------------------------------------------------------------------------------------------

void setup() {
strip.begin();
strip.show(); // initialize all pixels to 'off'
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);

}

//=============================================================================================

void loop() {

int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
color = 0;
if (buttonState == HIGH) {
lightPattern = (lightPattern + 1) % (nPatterns + 1); //include numOfPrograms + 1, since there is an off state
}
}
}

lastButtonState = reading;

//======================================

switch(lightPattern) {
case 1:
softBlink(strip.Color(50,0,0), 1);
break;
case 2:
softBlink(strip.Color(0,50,0), 1);
break;
case 3:
softBlink(strip.Color(0,0,50), 1);
break;
case 4:
rainbowCycle(10);
break;
default:
softBlink(strip.Color(0,0,0), 1);
break;
}


}

//======================================
// Fill all the dots with one color
void allColor(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}

//======================================
//fades in, then shuts off
void softBlink(uint32_t c, uint8_t wait) {

unsigned long currentMillis = millis();

if(currentMillis - previousMillis > wait) {

//set the color of all pixels
allColor(c);

// save the last time you changed a NeoPixel
previousMillis = currentMillis;

uint16_t i;
int b = neoPixel_j;
strip.show();
neoPixel_j = (neoPixel_j + 1);
}
}

//======================================

void rainbowCycle(uint8_t wait) {

uint16_t i;
if ( color < 256 * 50) {

for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + color) & 255));
}
strip.show();
delay(wait);
color++;
}

}

//======================================

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Se eu chamar direto usando "if buttonPin2 == HIGH (rainbowCycle(10)); ele não cria o efeito fica estatica as cores.

Achei este vídeo que demostra o efeito "rainbowCycle" acima;

https://youtu.be/hEVMkO4RK0Y?t=1m1s

Exibições: 510

As respostas para este tópico estão encerradas.

Respostas a este tópico

Tente:

if (digitalRead(buttonPin2)==HIGH)  lightPattern=4;

ou algo assim

Olá Eduardo

Consegui usando:

if (digitalRead(buttonPin2)==HIGH)  lightPattern=(4);

Muito Obrigado ^^

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço