Проблема прерывания в модуле NodeMcu esp8266 - PullRequest
0 голосов
/ 18 апреля 2020

Я пишу этот код:

/*My Ventilator
 * Pressure controlled movement of a servo motor
 * This program read time interval between two low and high pressure
 * and set that time interval for breathing time.
 * inspiration time : experiation time = 1:2
 * PEP or lower lavel of pressure select by a potentiometer
 * Due to lack of Pressure Sensor I am using a potentiometer to test
 * the code.
 * I am gonna using NODEMCU ESP8266 module
 * 
 */

//#include <Ticker.h>
#include <Servo.h>
#define redLedPin 14  //D5 pin
#define servoPin 12 //D6 Pin

//just adding two led to represent inspiration and expiration
#define redLedPin 16//D0 Pin;this led is ON when expiration is going
#define blueLedPin 13//D7 Pin;this led is ON when inspiration is going
//Three pep buttons
//pep =5
//#define pepButton5 5
//int pepButton5State = 0;
//pep =10
//#define pepButtonPin10 4
//int pepButton10State = 0;
//pep = 15
//#define pepButtonPin15 3
//int pepButton15State = 0;
//Ading a Interrupt Pin to count the value of pep
#define pepPin   5//
volatile byte  pepCount = 0;

int potPin = A0;//This Pot. control breathing rate per minute

int potVal;
int potFlag = 0;
Servo myservo;
int mapPotVal;
int breathNo;
int breathTime;
int inspTime;
int expTime;
float servoSpeed;

void setup() {
  Serial.begin(115200);
  attachInterrupt(digitalPinToInterrupt(pepPin), handelInterrupt, CHANGE);
  myservo.attach(servoPin);
  myservo.write(0);


  pinMode(redLedPin,OUTPUT);
  pinMode(blueLedPin,OUTPUT);
//  pinMode(pepButton5,INPUT);
//  pinMode(pepButton10,INPUT);
//  pinMode(pepButton15,INPUT);
  pinMode(pepPin, INPUT_PULLUP);
  Serial.print("Starting ...");

}

void loop() {
 // pepButton5State = digitalRead(pepButton5);
  //pepButton5State = digitalRead(pepButton5);
  //pepButton5State = digitalRead(pepButton5);
  potVal = analogRead(potPin);
  mapPotVal = map(potVal,0,1023,10,30);
  if(mapPotVal <=2){
    potFlag = 1;
  }
  if(mapPotVal >=8){
    potFlag = 0;
  }
  breathNo = mapPotVal;
  breathTime = 60000/(3*breathNo);
  inspTime = breathTime/3;
  expTime = 2*inspTime;
  Serial.print("Breathing Per Minute = ");
  Serial.println(pepCount);//for checking pepCount value we write this instead of breathTime
  float inspTimeS = inspTime/1000;//inspiration time in seconds
  float expTimeS = expTime/1000;//expiration time in secinds
  Serial.print("\nInspiration Time in second =");
  Serial.print(inspTimeS,4);
  Serial.print("\nExpiration Time in second = ");
  Serial.println(expTimeS,4);
  for (int i = 0;i<90;i++){
    Serial.print("\nInspiration ...");
    myservo.write(i);
    servoSpeed = inspTime/90;
    digitalWrite(redLedPin,LOW);
    digitalWrite(blueLedPin,HIGH);
    delay(servoSpeed);
  }
  for(int i = 45;i>0;i--){
    Serial.print("\nExpiration ...");
    myservo.write(2*i);
    digitalWrite(redLedPin,HIGH);
    digitalWrite(blueLedPin,LOW);

    servoSpeed = expTime/90;
    delay(servoSpeed);

  }


}


//This routine count number of pep count
void handelInterrupt(){
    if(pepCount < 2){
      pepCount++;
    }
    else {
      pepCount = 0;
    }
  }

До добавления «Прерывания» он работал правильно, но когда я добавляю Прерывания в GPI0 5 Pin, он компилируется и успешно загружается в плату ESP8266, но он не работает правильно .... Красный светодиод мигает непрерывно, без синего мигания светодиодного штифта или без вращения сервопривода Что я должен изменить в этом коде ... Любая помощь будет оценена. Спасибо

...