1

我有两个单独的草图,压力传感器和脉搏传感器工作正常。单独,这些都很好,但是当我尝试将两者结合起来时,它们就停止了对我的工作。我该如何解决这个问题?

//Code for pressure sensor
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin0 = 11;     // Connect red LED to pin 11 (PWM pin)
int LEDpin1 = 10;
int LEDpin2 = 9;
int fsrReading;       // The analog reading from the FSR resistor divider.
int LEDbrightness;

void setup(void) {
    Serial.begin(9600); // We'll send debugging information via the Serial monitor.
    pinMode(LEDpin0, OUTPUT);
    pinMode(LEDpin1, OUTPUT);
    pinMode(LEDpin2, OUTPUT);
}

void loop(void) {
    fsrReading = analogRead(fsrAnalogPin);
    Serial.print("Analog reading = ");
    Serial.println(fsrReading);

    // We'll need to change the range from the analog reading (0-1023) down to the range
    // used by analogWrite (0-255) with map!
    LEDbrightness = map(fsrReading, 0, 1023, 1, 255);
    // LED gets brighter the harder you press.
    analogWrite(LEDpin0, LEDbrightness);
    analogWrite(LEDpin1, LEDbrightness);
    analogWrite(LEDpin2, LEDbrightness);

    delay(100);
}

//Code for pulse sensor comes in two tabs this is the first tab.
int analogPinR = 11;
int analogPinG = 10;
int analogPinB = 9;

//The buffer
int RGB[9];

//Values of red, green and blue.
int R=0;
int G=0;
int B=0;
int pulsePin = 1;                // Pulse Sensor purple wire connected to analog pin 0.
int fadeRate = 0;                // Used to fade LED on with PWM on fadePin.
//int fadePin = 9;               // Pin to do fancy classy fading blink at each beat.
//int fadePin1 = 10;             // Pin to do fancy classy fading blink at each beat.
//int fadePin2 = 11;             // Pin to do fancy classy fading blink at each beat.

// These variables are volatile, because they are used during the interrupt service routine!
volatile int BPM;                // Used to hold the pulse rate..
volatile int Signal;             // Holds the incoming raw data.
volatile int IBI = 600;          // Holds the time between beats, the Inter-Beat Interval.
volatile boolean Pulse = false;  // True when pulse wave is high, false when it's low.
volatile boolean QS = false;     // Becomes true when Arduino finds a beat.

void setup()
{
    Serial.begin(9600);          // We agree to talk fast!
    interruptSetup();            // Sets up to read the pulse sensor signal every 2 ms.
}

void loop(){
    sendDataToProcessing('S', Signal);  // Send Processing the raw pulse sensor data.
    if (QS == true){                    // Quantified Self flag is true when Arduino finds a heartbeat
        fadeRate = 255;                 // Set 'fadeRate' variable to 255 to fade the LED with a pulse.
        sendDataToProcessing('B',BPM);  // Send heart rate with a 'B' prefix.
        sendDataToProcessing('Q',IBI);  // Send time between beats with a 'Q' prefix.
        QS = false;                     // Reset the Quantified Self flag for next time.
    }
    delay(20);                          // Take a break

    if (BPM > 120) {
        pinMode(analogPinR,255);     // Pin that will blink to your heartbeat!
        pinMode(analogPinG,0);       // Pin that will blink to your heartbeat!
        pinMode(analogPinB,0);       // Pin that will blink to your heartbeat!
    }
    else if (BPM > 90){ //Yellow
        pinMode(analogPinR,255);     // Pin that will blink to your heartbeat!
        pinMode(analogPinG,80);      // Pin that will blink to your heartbeat!
        pinMode(analogPinB,5);       // Pin that will blink to your heartbeat!
    }
    else if (BPM > 85){//turk
        pinMode(analogPinR,0);       // Pin that will blink to your heartbeat!
        pinMode(analogPinG,204);     // Pin that will blink to your heartbeat!
        pinMode(analogPinB,102);     // Pin that will blink to your heartbeat!
    }
    else if (BPM > 80){//green/blue
        pinMode(analogPinR,0);       // Pin that will blink to your heartbeat!
        pinMode(analogPinG,255);     // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);     // Pin that will blink to your heartbeat!
    }
    else if (BPM > 75){//blue/green
        pinMode(analogPinR,0);       // Pin that will blink to your heartbeat!
        pinMode(analogPinG,128);     // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);     // Pin that will blink to your heartbeat!
    }
    else if (BPM > 70){//blue/purple
        pinMode(analogPinR,102);     // Pin that will blink to your heartbeat!
        pinMode(analogPinG,102);     // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);     // Pin that will blink to your heartbeat!
    }
    else if (BPM > 65){//purple
        pinMode(analogPinR,178);     // Pin that will blink to your heartbeat!
        pinMode(analogPinG,102);     // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);     // Pin that will blink to your heartbeat!
    }
    else if (BPM > 60){//blue
        pinMode(analogPinR,0);       // Pin that will blink to your heartbeat!
        pinMode(analogPinG,0);       // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);     // Pin that will blink to your heartbeat!
    }

    //AURORA
    else {
        //Blue
        pinMode(analogPinR,0);    // Pin that will blink to your heartbeat!
        pinMode(analogPinG,0);    // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);  // Pin that will blink to your heartbeat!
        delay(300);

        //Green
        pinMode(analogPinR,0);    // Pin that will blink to your heartbeat!
        pinMode(analogPinG,255);  // Pin that will blink to your heartbeat!
        pinMode(analogPinB,0);    // Pin that will blink to your heartbeat!
        delay(300);

        //Orange
        pinMode(analogPinR,255);  // Pin that will blink to your heartbeat!
        pinMode(analogPinG,128);  // Pin that will blink to your heartbeat!
        pinMode(analogPinB,0);    // Pin that will blink to your heartbeat!
        delay(300);

        //Yellow
        pinMode(analogPinR,255);  // Pin that will blink to your heartbeat!
        pinMode(analogPinG,255);  // Pin that will blink to your heartbeat!
        pinMode(analogPinB,0);    // Pin that will blink to your heartbeat!
        delay(300);

        //Light blue
        pinMode(analogPinR,0);    // Pin that will blink to your heartbeat!
        pinMode(analogPinG,255);  // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);  // Pin that will blink to your heartbeat!
        delay(300);

        //Purple
        pinMode(analogPinR,255);  // Pin that will blink to your heartbeat!
        pinMode(analogPinG,0);    // Pin that will blink to your heartbeat!
        pinMode(analogPinB,255);  // Pin that will blink to your heartbeat!
        delay(300);
    }
}

void ledFadeToBeat(){
    fadeRate -= 15;                        // Set LED fade value
    fadeRate = constrain(fadeRate,0,255);  // Keep LED fade value from going into negative numbers!
    // analogWrite(fadePin2,fadeRate);     // Fade LED
}

void sendDataToProcessing(char symbol, int data ){
    Serial.print(symbol);                  // Symbol prefix tells Processing what type of data is coming
    Serial.println(data);                  // The data to send culminating in a carriage return
    Serial.println(BPM);                   // Print to the laptop screen
    ledFadeToBeat();

    if(Serial.available()==9){
        for(int i =0;i<9;i++){
            RGB = Serial.read() - '0';
        }

        //Get the data from the integer array
        R = RGB[0]*100+RGB[1]*10+RGB[2];
        G = RGB[3]*100+RGB[4]*10+RGB[5];
        B = RGB[6]*100+RGB[7]*10+RGB[8];
    }
}

//This is the second tab
volatile int rate[10];                     // Used to hold last ten IBI values
volatile unsigned long sampleCounter = 0;  // Used to determine pulse timing
volatile unsigned long lastBeatTime = 0;   // Used to find the inter beat interval
volatile int P =512;                       // Used to find peak in pulse wave
volatile int T = 512;                      // Used to find trough in pulse wave
volatile int thresh = 512;                 // Used to find instant moment of heart beat
volatile int amp = 100;                    // Used to hold amplitude of pulse waveform
volatile boolean firstBeat = true;         // Used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = true;        // Used to seed rate array so we startup with reasonable BPM

void interruptSetup(){
    // Initializes Timer2 to throw an interrupt every 2mS.
    TCCR2A = 0x02;  // Disable PWM on digital pins 3 and 11, and go into CTC mode.
    TCCR2B = 0x06;  // Don't force compare, 256 prescaler.
    OCR2A = 0X7C;   // Set the top of the count to 124 for 500 Hz sample rate.
    TIMSK2 = 0x02;  // Enable interrupt on match between TIMER2 and OCR2A.
    sei();          // Make sure global interrupts are enabled.
}

// This is the timer 2 interrupt service routine.
// Timer 2 makes sure that we take a reading
// every 2 miliseconds.
ISR(TIMER2_COMPA_vect){                         // Triggered when Timer2 counts to 124
    cli();                                      // Disable interrupts while we do this
    Signal = analogRead(pulsePin);              // Read the pulse sensor
    sampleCounter += 2;                         // Keep track of the time in ms with this variable.
    int N = sampleCounter - lastBeatTime;       // Monitor the time since the last beat to avoid noise.

    // Find the peak and trough of the pulse wave
    if (Signal < thresh && N > (IBI/5)*3){      // Avoid dichrotic noise by waiting 3/5 of last IBI.
        if (Signal < T){                        // T is the trough.
            T = Signal;                         // Keep track of lowest point in pulse wave.
        }
    }

    if (Signal > thresh && Signal > P) {        // A threshold condition helps avoid noise.
        P = Signal;                             // P is the peak.
    }                                           // Keep track of highest point in pulse wave.

    // Now it's time to look for the heart beat
    // signal surges up in value every time there is a pulse
    if (N > 250){                               // Avoid high frequency noise
        if ( (Signal > thresh) &&
             (Pulse == false) &&
             (N > (IBI/5)*3) ){

            Pulse = true;                        // Set the pulse flag when we think there is a pulse.
            digitalWrite(analogPinR,HIGH);       // Turn on bluepin LED.
            digitalWrite(analogPinG,HIGH);       // Turn on redpin LED.
            digitalWrite(analogPinB,HIGH);       // Turn on greenpin LED.

            IBI = sampleCounter - lastBeatTime;  // Measure time between beats in ms.
            lastBeatTime = sampleCounter;        // Keep track of time for next pulse.

            if (firstBeat){                      // If it's the first time we found a beat, if firstBeat == TRUE
                firstBeat = false;               // Clear firstBeat flag.
                return;                          // IBI value is unreliable so discard it.
            }
            if (secondBeat){                     // If this is the second beat, if secondBeat == TRUE.
                secondBeat = false;              // Clear secondBeat flag.
                for(int i=0; i<=9; i++){          // Seed the running total to get a realisitic BPM at startup.
                    rate = IBI;
                }
            }

            // Keep a running total of the last 10 IBI values.
            word runningTotal = 0;       // Clear the runningTotal variable

            for(int i=0; i<=8; i++){     // Shift data in the rate array
                rate = rate[i+1];      // And drop the oldest IBI value
                runningTotal += rate;  // Add up the 9 oldest IBI values
            }

            rate[9] = IBI;               // Add the latest IBI to the rate array
            runningTotal += rate[9];     // Add the latest IBI to runningTotal
            runningTotal /= 10;          // Average the last 10 IBI values
            BPM = 60000/runningTotal;    // How many beats can fit into a minute? that's BPM!
            QS = true;                   // Set Quantified Self flag
            // QS flag is not cleared inside this interupt service routine (ISR)
        }
    }

    if (Signal < thresh && Pulse == true){  // When the values are going down, the beat is over.
        // digitalWrite(analogPinR,LOW);    // Turn off red LED.
        // digitalWrite(analogPinG,LOW);    // Turn off green LED.
        // digitalWrite(analogPinB,LOW);    // Turn off blue LED.
        Pulse = false;                      // Reset the pulse flag, so we can do it again.
        amp = P - T;                        // Get amplitude of the pulse wave.
        thresh = amp/2 + T;                 // Set thresh at 50% of the amplitude.
        P = thresh;                         // Reset these for next time.
        T = thresh;
    }

    if (N > 2500){                     // If 2.5 seconds go by without a beat
        thresh = 512;                  // Set thresh default
        P = 512;                       // Set P default
        T = 512;                       // Set T default
        lastBeatTime = sampleCounter;  // Bring the lastBeatTime up to date
        firstBeat = true;              // Set these to avoid noise
        secondBeat = true;             // When we get the heartbeat back
    }
    sei();                             // Enable interrupts when youre done!
}// end ISR
4

2 回答 2

1

第二组代码包含错误。

pinMode() 不是设置模拟输出的方法。所有这些都不正确:

//orange
pinMode(analogPinR,255);
pinMode(analogPinG,128);
pinMode(analogPinB,0);    

而且您还没有将引脚配置为输出。

您需要在设置中配置一次

pinMode(ledG, OUTPUT);  

并在循环中写入:

analogWrite(ledG, 128);

解决这个问题,至少 LED 应该像您期望的那样工作。

于 2013-05-31T08:30:55.670 回答
1

这很棘手,我不知道你的经历(我不想让你生气)。

如果你是编程新手

里奇先生问这是不是一个文件?在 Arduino草图(和大多数计算机语言)中,每个函数都必须有自己的名称。此外,Arduino 具有名为setup()loop()的特殊函数。

如果你一直在街区附近

ISR(TIMER2_COMPA_vect){               // Triggered when Timer2 counts to 124

这可能真的会扰乱您的控制流程。无论您在哪里/做什么,您都会停下来执行此 ISR 请求。当您跳开并执行 ISR 时,也许您正在执行以下操作。

 RGB = Serial.read() - '0';

这可能会扰乱您的串行通信(或任何与时间相关的东西)。

我假设是您在论坛帖子
Arduino UNO - 控制带有压力传感器和脉冲传感器的 LED 灯条

于 2013-05-30T18:18:00.550 回答