Wątek przeniesiony 2020-06-02 20:19 z C/C++ przez kq.

sterowanie diodą - particle & iftt

0

Witam, chciałbym sterować diodami na RPI za pomocą głosu, jednak mam problem z stworzeniem poprawnego kodu.

int relay = D1; //pin to which relay is connected

int relay2 = D2;


int boardLed = D7;
bool vin = LOW; //a virtual boolean variable
bool vin2 = LOW; //a virtual boolean variable
// setup() is run only once, it's where we set up GPIO and //initialise peripherals
void setup() {
    
  // Setup GPIO
  pinMode(relay,OUTPUT); // relay pin is set as output
  digitalWrite(relay,HIGH);
  // Setup GPIO
  pinMode(relay2,OUTPUT); // relay pin is set as output
  digitalWrite(relay2,HIGH);     
  
 // Subscribe to events published by IFTTT using  Particle.subscribe
  Particle.subscribe("LED_on", myHandler); 
      //turning off function declaration
Particle.subscribe("LED_off", thisHandler); 
      //turning on function declaration
      
      
 // Subscribe to events published by IFTTT using  Particle.subscribe
  Particle.subscribe("LED_on_kitchen", myHandler2); 
      //turning off function declaration
Particle.subscribe("LED_off_kitchen", thisHandler2); 
      //turning on function declaration      
      
}

// loop() runs continuously, it's our infinite loop.

void loop() {
     if (vin==HIGH){
         digitalWrite(relay,LOW);
         
     }
else if (vin==LOW){
         digitalWrite(relay,HIGH);
     }

     if (vin2==HIGH){
         digitalWrite(relay2,LOW);
         
     }
else if (vin2==LOW){
         digitalWrite(relay2,HIGH);
     }

}

//our events are called when IFTTT applets are triggered

void myHandler(const char *event, const char *data){
    vin=LOW;
}
void thisHandler(const char *event, const char *data){
     vin=HIGH;
}

void myHandler2(const char *event, const char *data){
    vin2=LOW;
}
void thisHandler2(const char *event, const char *data){
     vin2=HIGH;
}

Potrafię sterować jedną diodą, ale chcę sterować dwiema niezależnie. - prosiłbym o wskazówki

up. wystarczyło zrobić jedną pętlę, - ale teraz przy wyłączonych diodach po wypowiedzeniu polecenia LED_on_kitchen uruchamiają się obie diody, w czym może być problem?

0

zastanawiało mnie dlaczego miałeś loop2(), przecież nigdzie to nie jest wywoływane. Funkcja main() w skrócie wygląda tak:

int main() {
    setup();
    for (;;) { // forever
        loop();
    }
}

Czy te diody świecą pełną mocą, czy może trochę słabiej? Funkcja loop wykonuje się tysiące razy na sekundę i może twój program działa ale wykonuje się za szybko. Druga możliwość to kompilator robi jakieś optymalizacje i pewne zmienne może usunąć z ostatecznego kodu. Spróbuj z zadeklarowaniem pewnych zmiennych jako volatile:

volatile bool vin = LOW; //a virtual boolean variable
volatile bool vin2 = LOW; //a virtual boolean variable
0

Nic się nie zmieniło. Jednak polecenia LED_on_kitchen oraz również LED_off_kitchen sterują obiema diodami. A nie tą jedną przypisaną do nich.

1 użytkowników online, w tym zalogowanych: 0, gości: 1