我有两个独立的sketches,压力传感器和脉冲传感器工作正常。独自一人,这些都很好,但是当我试图将两者结合起来时,他们就停止了我的工作。我该如何解决这个问题?
//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
答案 0 :(得分:1)
这很棘手,我不知道你的经历(而且我不想让你发疯)。
如果您不熟悉编程
锶。 Richie问这是不是一个文件?在Arduino sketch(和大多数计算机语言)中,每个函数都必须有自己的名称。此外,Arduino具有名为 setup()和 loop()的特殊功能。
如果您已经在街区附近
ISR(TIMER2_COMPA_vect){ // Triggered when Timer2 counts to 124
这可能会让你的控制流程变得混乱。无论您在哪里/做什么,您都将停止并执行此ISR请求。当你跳开并做ISR时,也许你正在做下面的事情。
RGB = Serial.read() - '0';
这可能会破坏您的串行通信(或任何时间依赖性)。
我认为是你在论坛帖子
Arduino UNO - Controling LED strip with presure sensor & pulse sensor 。
答案 1 :(得分:1)
第二组代码包含错误。
pinMode()不是设置模拟输出的方法。所有这些都不正确:
//orange
pinMode(analogPinR,255);
pinMode(analogPinG,128);
pinMode(analogPinB,0);
您尚未将引脚配置为输出。
您需要在设置中配置一次:
pinMode(ledG, OUTPUT);
并写入循环:
analogWrite(ledG, 128);
修复此问题,至少LED应该按预期运行。