Arduino Polska Forum

Pełna wersja: zmiana efektu led poprzez IR
Aktualnie przeglądasz uproszczoną wersję forum. Kliknij tutaj, by zobaczyć wersję z pełnym formatowaniem.
witam chciałbym napisać program który po naciśnięciu przycisku na pilocie włącza efekt tęczy na pasku led a drugi wyłącza
mam efekt tęczy z generatora z internetu.Zrobiłem to samo na przycisku i wszystko działa jak powinno a na podczerwieni już nie zauważyłem że jak wyrzucę kod z void zm tam gdzie jest opisane co ma robić jeżeli zmiana = coś to działa podczerwień na pilocie i w monitorze portu jest wyświetlana zmiana czy ktoś wie jak to naprawić


Kod:
#include <Adafruit_NeoPixel.h>
#include <IRremote.h>
IRrecv irrecv(2);
decode_results results;






boolean zmiana  = 0;

////////////////////////////////////////////////
class Strip
{
public:
  uint8_t   effect;
  uint8_t   effects;
  uint16_t  effStep;
  unsigned long effStart;
  Adafruit_NeoPixel strip;
  Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, A0, striptype) {
    effect = -1;
    effects = toteffects;
    Reset();
  }
  void Reset(){
    effStep = 0;
    effect = (effect + 1) % effects;
    effStart = millis();
  }
};

struct Loop
{
  uint8_t currentChild;
  uint8_t childs;
  bool timeBased;
  uint16_t cycles;
  uint16_t currentTime;
  Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {currentTime=0;currentChild=0;childs=totchilds;timeBased=timebased;cycles=tottime;}
};

Strip strip_0(44, A0, 44, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(1, false, 1);
//////////////////////////////////////////////////////
//[GLOBAL_VARIABLES]

void setup() {

Serial.begin(9600);
irrecv.enableIRIn();
strip_0.strip.clear();
  //Your setup here:

  strip_0.strip.begin();
pinMode(5, INPUT_PULLUP);
}

void loop(){
ir();
  Serial.println(zmiana);
if(digitalRead(5) == LOW) {
zmiana = !zmiana;
delay(200);
}
zm();
}

void strips_loop() {
  if(strip0_loop0() & 0x01)
    strip_0.strip.show();
}

uint8_t strip0_loop0() {
  uint8_t ret = 0x00;
  switch(strip0loop0.currentChild) {
    case 0:
           ret = strip0_loop0_eff0();break;
  }
  if(ret & 0x02) {
    ret &= 0xfd;
    if(strip0loop0.currentChild + 1 >= strip0loop0.childs) {
      strip0loop0.currentChild = 0;
      if(++strip0loop0.currentTime >= strip0loop0.cycles) {strip0loop0.currentTime = 0; ret |= 0x02;}
    }
    else {
      strip0loop0.currentChild++;
    }
  };
  return ret;
}

uint8_t strip0_loop0_eff0() {
    // Strip ID: 0 - Effect: Rainbow - LEDS: 90
    // Steps: 411 - Delay: 20
    // Colors: 3 (255.0.0, 0.255.0, 0.0.255)
    // Options: rainbowlen=255, toLeft=true,
  if(millis() - strip_0.effStart < 20 * (strip_0.effStep)) return 0x00;
  float factor1, factor2;
  uint16_t ind;
  for(uint16_t j=0;j<44;j++) {
    ind = strip_0.effStep + j * 1.611764705882353;
    switch((int)((ind % 411) / 137)) {
      case 0: factor1 = 1.0 - ((float)(ind % 411 - 0 * 137) / 137);
              factor2 = (float)((int)(ind - 0) % 411) / 137;
              strip_0.strip.setPixelColor(j, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2);
              break;
      case 1: factor1 = 1.0 - ((float)(ind % 411 - 1 * 137) / 137);
              factor2 = (float)((int)(ind - 137) % 411) / 137;
              strip_0.strip.setPixelColor(j, 0 * factor1 + 0 * factor2, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2);
              break;
      case 2: factor1 = 1.0 - ((float)(ind % 411 - 2 * 137) / 137);
              factor2 = (float)((int)(ind - 274) % 411) / 137;
              strip_0.strip.setPixelColor(j, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2, 255 * factor1 + 0 * factor2);
              break;
    }
  }
  if(strip_0.effStep >= 411) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}




void ir(void)
{
if (irrecv.decode(&results)) // sprawdza, czy otrzymano sygnał IR
{
unsigned long odczyt = results.value; // sygnał zapisuje jako odczyt
switch (odczyt)
{
  break;
case 16236607: // włączenie
zmiana = !zmiana;
  break;
case 16203967 : // wyłączenie
zmiana = !zmiana;
  break;
}
irrecv.resume(); // reseruje czujnik
}
delay(25);
}


byte change (boolean otrz)
{
return (!otrz);
}


void zm()
{
if(zmiana ==1){
  strips_loop();
                }
if(zmiana ==0){
int i = 0;
for (i = 0; i < 44; i++) {
strip_0.strip.setPixelColor(i,  strip_0.strip.Color(0, 0, 0)); //Dioda nr 1 świeci na zielono
}strip_0.strip.show();
                           }}
zmiana = !zmiana;

zmień na

strips_loop();

a to wywal

Serial.println(zmiana);
Ok jutro zastosuje zmiany i powiem jaki rezultat
(27-10-2021, 21:35)Jarewa0606 napisał(a): [ -> ]zmiana = !zmiana;

zmień na

strips_loop();

a to wywal

Serial.println(zmiana);

wywaliłem zmiane całkowicie i dałem strips loop(); do case włącznie a wyłączenie led do wyłączenie teraz ledy się włączają i wyłączają jak chciałem ale tęcza się nie rusza stoi w miejscu jakiś pomysł?


Kod:
#include <Adafruit_NeoPixel.h>
#include <IRremote.h>
IRrecv irrecv(2);
decode_results results;



////////////////////////////////////////////////
class Strip
{
public:
  uint8_t   effect;
  uint8_t   effects;
  uint16_t  effStep;
  unsigned long effStart;
  Adafruit_NeoPixel strip;
  Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, A0, striptype) {
    effect = -1;
    effects = toteffects;
    Reset();
  }
  void Reset(){
    effStep = 0;
    effect = (effect + 1) % effects;
    effStart = millis();
  }
};

struct Loop
{
  uint8_t currentChild;
  uint8_t childs;
  bool timeBased;
  uint16_t cycles;
  uint16_t currentTime;
  Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {currentTime=0;currentChild=0;childs=totchilds;timeBased=timebased;cycles=tottime;}
};

Strip strip_0(44, A0, 44, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(1, false, 1);
//////////////////////////////////////////////////////
//[GLOBAL_VARIABLES]

void setup() {

Serial.begin(9600);
irrecv.enableIRIn();
strip_0.strip.clear();
  //Your setup here:

  strip_0.strip.begin();
pinMode(5, INPUT_PULLUP);
}

void loop(){
ir();
}

void strips_loop() {
  if(strip0_loop0() & 0x01)
    strip_0.strip.show();
}

uint8_t strip0_loop0() {
  uint8_t ret = 0x00;
  switch(strip0loop0.currentChild) {
    case 0:
           ret = strip0_loop0_eff0();break;
  }
  if(ret & 0x02) {
    ret &= 0xfd;
    if(strip0loop0.currentChild + 1 >= strip0loop0.childs) {
      strip0loop0.currentChild = 0;
      if(++strip0loop0.currentTime >= strip0loop0.cycles) {strip0loop0.currentTime = 0; ret |= 0x02;}
    }
    else {
      strip0loop0.currentChild++;
    }
  };
  return ret;
}

uint8_t strip0_loop0_eff0() {
    // Strip ID: 0 - Effect: Rainbow - LEDS: 90
    // Steps: 411 - Delay: 20
    // Colors: 3 (255.0.0, 0.255.0, 0.0.255)
    // Options: rainbowlen=255, toLeft=true,
  if(millis() - strip_0.effStart < 20 * (strip_0.effStep)) return 0x00;
  float factor1, factor2;
  uint16_t ind;
  for(uint16_t j=0;j<44;j++) {
    ind = strip_0.effStep + j * 1.611764705882353;
    switch((int)((ind % 411) / 137)) {
      case 0: factor1 = 1.0 - ((float)(ind % 411 - 0 * 137) / 137);
              factor2 = (float)((int)(ind - 0) % 411) / 137;
              strip_0.strip.setPixelColor(j, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2);
              break;
      case 1: factor1 = 1.0 - ((float)(ind % 411 - 1 * 137) / 137);
              factor2 = (float)((int)(ind - 137) % 411) / 137;
              strip_0.strip.setPixelColor(j, 0 * factor1 + 0 * factor2, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2);
              break;
      case 2: factor1 = 1.0 - ((float)(ind % 411 - 2 * 137) / 137);
              factor2 = (float)((int)(ind - 274) % 411) / 137;
              strip_0.strip.setPixelColor(j, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2, 255 * factor1 + 0 * factor2);
              break;
    }
  }
  if(strip_0.effStep >= 411) {strip_0.Reset(); return 0x03; }
  else strip_0.effStep++;
  return 0x01;
}




void ir(void)
{
if (irrecv.decode(&results)) // sprawdza, czy otrzymano sygnał IR
{
unsigned long odczyt = results.value; // sygnał zapisuje jako odczyt
switch (odczyt)
{
  break;
case 16236607: // włączenie
strips_loop();
  break;
case 16203967 : // wyłączenie
      int i = 0;
for (i = 0; i < 44; i++) {
strip_0.strip.setPixelColor(i,  strip_0.strip.Color(0, 0, 0)); //Dioda nr 1 świeci na zielono
}strip_0.strip.show();
  break;
}
irrecv.resume(); // reseruje czujnik
}
delay(25);
}


byte change (boolean otrz)
{
return (!otrz);
}