mycontroller-org / MyControllerDevice

MyController supported MQTT ESP8266 library
http://www.mycontroller.org
Apache License 2.0
4 stars 4 forks source link

Freeze with attachInterrupt? #9

Closed Stephan35 closed 6 years ago

Stephan35 commented 7 years ago

Hello, Inside sketch i used attachInterrupt . When i put GPIO5 or GPIO14 to 1 -> go immediatly to 1 When i release GPIO5 or GPIO14 to 0 -> nothing do

i was playing with these GPIO to understand whant append, then suddenly, all i done went at 1 time (so full of 1 and 0 at serial print)


void sendPin5(){
  McMessage _message;
  _message.update(SENSOR_GPIO5, C_SET, V_TRIPPED);
  if (digitalRead(PIN_SENSOR_GPIO5) == HIGH){
    send(_message.set(1));
    Serial.println("BP = 1");
  }
  if (digitalRead(PIN_SENSOR_GPIO5) == LOW){
    send(_message.set(0));
    Serial.println("BP = 0");
  }
}
void sendPin14(){
  McMessage _message;
  _message.update(SENSOR_GPIO14, C_SET, V_TRIPPED);
  if (digitalRead(PIN_SENSOR_GPIO14) == HIGH){
    send(_message.set(1));
    Serial.println("BP2 = 1");
  }
  if (digitalRead(PIN_SENSOR_GPIO14) == LOW){
    send(_message.set(0));
    Serial.println("BP2 = 0");
  }
}

void Pin5Change(){
    delayMicroseconds(32000);
    Serial.println("\n*** Pin5 Interrupt");
    Gpio5_Checked = true;
}
void Pin14Change(){
    delayMicroseconds(32000);
    Serial.println("\n*** Pin14 Interrupt");
    Gpio14_Checked = true;
}

void setup() {

...
..
..
  if(!isSystemConfigured()){
    //You can check, does this node/device configured. If no you can display/blink some led from here.
  }
  attachInterrupt(PIN_SENSOR_GPIO5, (Pin5Change), CHANGE);
  attachInterrupt(PIN_SENSOR_GPIO14, (Pin14Change), CHANGE); //digitalPinToInterrupt(x)
}

void loop() {
long now = millis();
  mc.loop();
  if (Gpio5_Checked) {
    Gpio5_Checked = false;
    sendPin5();
  }
  if (Gpio14_Checked) {
    Gpio14_Checked = false;
    sendPin14();
  }
  //You can add your logic here.
  if (now - lastRecu > 30000 ) {
    lastRecu = now;
    sendRSSI();
  }
}

Should i remove sendRSSI(); ?

Thanks

jkandasa commented 7 years ago

@Stephan35 How are you handling 1 and 0 by hand with the push button? I guess it is creating debounce. This link will explain more about this.

When you enter into the interrupt loop. First, you have to disable the interrupt. Once completed your actions reenable it.

Stephan35 commented 7 years ago

I know about debounce , i already have same kind with another app. Normally adding delayMicroseconds(32000); solve it. In another way i also add this :

void Pin5Change(){
    if (millis() - Gpio5_millis > 75){
        delayMicroseconds(32000);
        Serial.println("\n*** Pin5 Interrupt");
        Gpio5_Checked = true;
        Gpio5_millis = millis();
        detachInterrupt(PIN_BP_GPIO5);
    }
}

Gpio5_millis force to wait before another look of gpio state, but still not working As you can see , i also add , in a second way detachInterrupt, to wait end of gpio reading in loop.

The real problem of attachInterrupt is that is stopping all processus to do it . millis() still not work during , keep last value in fact.

Anyway, i do it another way, but i don't like it , removing attachinterrupt , and look at every loop the gpio state .... bad for me.

void loop() {
    mc.loop();
    long now = millis();
    if (now - Scan_Gpio > 200 ){
        Gpio4_S = digitalRead(PIN_BP_GPIO4);
        Gpio5_S = digitalRead(PIN_BP_GPIO5);
        Gpio14_S = digitalRead(PIN_BP_GPIO14);
        if (Gpio4_S != Gpio4_LS ){
            sendPin4(((Gpio4_S == HIGH) ? 1 : 0));
            Gpio4_LS = Gpio4_S;
        }
        if (Gpio5_S != Gpio5_LS ){
            sendPin5(((Gpio5_S == HIGH) ? 1 : 0));
            Gpio5_LS = Gpio5_S;
        }
        if (Gpio14_S != Gpio14_LS ){
            sendPin14(((Gpio14_S == HIGH) ? 1 : 0));
            Gpio14_LS = Gpio14_S;
        }
        Scan_Gpio = now;
        Serial.print(".");

    }

  /*
  if (Gpio5_Checked) {
    Gpio5_Checked = false;
    sendPin5();
    yield();
    attachInterrupt(PIN_BP_GPIO5, (Pin5Change), CHANGE);
  }
  if (Gpio14_Checked) {
    Gpio14_Checked = false;
    sendPin14();
    yield();
    attachInterrupt(PIN_BP_GPIO14, (Pin14Change), CHANGE);
  }
  */
  //You can add your logic here.
    if (now - lastRecu > 5000 ) {
        lastRecu = now;
        //sendRSSI();
        sendLUX();
        //send(SENSOR_LUXMETRE, analogRead(A0));
    }
}

What do you think about it ?

jkandasa commented 7 years ago

@Stephan35 I hope the following line might resolve your issue. But I never tested ;) If it is not working please do not mind :)

void setup() {
...
..
..
  attachInterrupt(PIN_BP_GPIO5, (Pin5Change), CHANGE);
  attachInterrupt(PIN_BP_GPIO14, (Pin14Change), CHANGE); //digitalPinToInterrupt(x)
}

void Pin5Change(){
  detachInterrupt(PIN_BP_GPIO5);
  if(!Gpio5_Checked){
    delayMicroseconds(32000);
    Serial.println("\n*** Pin5 Interrupt");
    Gpio5_Checked = true;
  }    
  attachInterrupt(PIN_BP_GPIO5, (Pin5Change), CHANGE);
}
void Pin14Change(){
  detachInterrupt(PIN_BP_GPIO14);
  if(!Gpio14_Checked){
    delayMicroseconds(32000);
    Serial.println("\n*** Pin14 Interrupt");
    Gpio14_Checked = true;
  }
  attachInterrupt(PIN_BP_GPIO14, (Pin14Change), CHANGE);
}
jkandasa commented 6 years ago

Seems submitter not interested anymore on this issue. I am closing this issue.