2016-10-05 78 views
0

我使用四個fsr壓力傳感器並聯在一起。以下是我的項目編碼。壓力傳感器上的計數器迴路arduino

我有一個計數器值的問題,它爲每個傳感器顯示四個增加的計數器值。

我需要得到一個計數器值,在每次檢測到壓力時加上,如果沒有壓力,它將保持先前的計數器值。

int fsrPin[] = {0, 1, 2, 3};  
 
int fsrReading;  
 
int fsrVoltage;  
 

 
unsigned long fsrResistance; 
 
unsigned long fsrConductance; 
 
long fsrForce; 
 
int pinCount = 4; 
 
int counter = 0; 
 

 
    
 
void setup(void) { 
 
    for(int thisPin = 0; thisPin < pinCount; thisPin++) { 
 
    pinMode(fsrPin[thisPin], OUTPUT); 
 
    } 
 
    Serial.begin(9600); 
 
} 
 
    
 
void loop(void) { 
 
    
 
    for(int thisPin = 0; thisPin < pinCount; thisPin++) { 
 
    fsrReading = analogRead(fsrPin[thisPin]); 
 
    Serial.print("Analog reading "); 
 
    Serial.print(fsrPin[thisPin]); 
 
    Serial.print("=> "); 
 
    Serial.println(fsrReading); 
 
    fsrVoltage = map(fsrReading, 0, 1023, 0, 5000); 
 
    Serial.print("Voltage reading in mV = "); 
 
    Serial.println(fsrVoltage); 
 
    
 
    if (fsrVoltage == 0) { 
 
    Serial.println("No pressure"); 
 
    } else { 
 
    // The voltage = Vcc * R/(R + FSR) where R = 10K and Vcc = 5V 
 
    // so FSR = ((Vcc - V) * R)/V 
 
    fsrResistance = 5000 - fsrVoltage;  
 
    fsrResistance *= 10000;     
 
    fsrResistance /= fsrVoltage; 
 
    Serial.print("FSR resistance in ohms = "); 
 
    Serial.println(fsrResistance); 
 
    
 
    fsrConductance = 1000000;   
 
    fsrConductance /= fsrResistance; 
 
    Serial.print("Conductance in microMhos: "); 
 
    Serial.println(fsrConductance); 
 
    
 
    if (fsrConductance <= 1000) { 
 
     fsrForce = fsrConductance/80; 
 
     Serial.print("Force in Newtons: "); 
 
     Serial.println(fsrForce);  
 
    } else { 
 
     fsrForce = fsrConductance - 1000; 
 
     fsrForce /= 30; 
 
     Serial.print("Force in Newtons: "); 
 
     Serial.println(fsrForce);    
 
    } 
 
    } 
 

 
    for(int thisPin=0; thisPin < pinCount; thisPin++){ 
 
    if (fsrForce != 0) {  
 
    counter++; 
 
    Serial.print("Counter = "); 
 
    Serial.println(counter); 
 
    } else { 
 
    counter; 
 
    Serial.print("Counter = "); 
 
    Serial.println(counter); 
 
    } 
 
    } 
 
    Serial.println("--------------------"); 
 
    } 
 
    delay(3000); 
 
}

的實際問題如下,我需要找到一個正確的條件循環計數器的值:

for(int thisPin=0; thisPin < pinCount; thisPin++){ 
 
    if (fsrForce != 0) {  
 
    counter++; 
 
    Serial.print("Counter = "); 
 
    Serial.println(counter); 
 
    } else { 
 
    counter; 
 
    Serial.print("Counter = "); 
 
    Serial.println(counter); 
 
    } 
 
    }

FOUR COUNTER VALUES IN EACH ANALOG READING

+0

那麼什麼是現在怎麼辦?每次對4個傳感器中的任何一個施加壓力時,是否只需要增加計數器?在if(fsrVoltage == 0){'help?'後加'fsrForce = 0' –

+0

它在每個傳感器讀數上顯示四個計數器值。對,就是這樣。只要傳感器上有壓力,我就希望計數器增加。 – Mira

+0

現在在做什麼?它是否只是一直顯示'Counter = 0'? –

回答

0

我相信主要的問題是你正在檢查fsrForce != 0,但是fsrForce實際上沒有機會達到零,因爲它取決於fsrVoltage,並且只有在fsrVoltage爲正值時才設置。

只需設置fsrForce = 0fsrVoltage < VOLTAGE_THRESHOLD,你應該得到預期的行爲。

試試這個:

int fsrPin[] = { 
    0, 
    1, 
    2, 
    3 
}; 
int fsrReading; 
int fsrVoltage; 

unsigned long fsrResistance; 
unsigned long fsrConductance; 
long fsrForce; 
int pinCount = 4; 
int counter = 0; 

// add threshold 
const float VOLTAGE_THRESHOLD = 0.2; 

void setup(void) { 
    for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
     pinMode(fsrPin[thisPin], OUTPUT); 
    } 
    Serial.begin(9600); 
} 

void loop(void) { 

    for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
     fsrReading = analogRead(fsrPin[thisPin]); 
     Serial.print("Analog reading "); 
     Serial.print(fsrPin[thisPin]); 
     Serial.print("=> "); 
     Serial.println(fsrReading); 
     fsrVoltage = map(fsrReading, 0, 1023, 0, 5000); 
     Serial.print("Voltage reading in mV = "); 
     Serial.println(fsrVoltage); 

     if (fsrVoltage < VOLTAGE_THRESHOLD) { 

      // reset the fsrForce when there is no pressure 
      fsrForce = 0 

      Serial.println("No pressure"); 
     } else { 
      // The voltage = Vcc * R/(R + FSR) where R = 10K and Vcc = 5V 
      // so FSR = ((Vcc - V) * R)/V 
      fsrResistance = 5000 - fsrVoltage; 
      fsrResistance *= 10000; 
      fsrResistance /= fsrVoltage; 
      Serial.print("FSR resistance in ohms = "); 
      Serial.println(fsrResistance); 

      fsrConductance = 1000000; 
      fsrConductance /= fsrResistance; 
      Serial.print("Conductance in microMhos: "); 
      Serial.println(fsrConductance); 

      if (fsrConductance <= 1000) { 
       fsrForce = fsrConductance/80; 
       Serial.print("Force in Newtons: "); 
       Serial.println(fsrForce); 
      } else { 
       fsrForce = fsrConductance - 1000; 
       fsrForce /= 30; 
       Serial.print("Force in Newtons: "); 
       Serial.println(fsrForce); 
      } 
     } 

     for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
      if (fsrForce != 0) { 
       counter++; 
      } 
      Serial.print("Counter = "); 
      Serial.println(counter); 
     } 
     Serial.println("--------------------"); 
    } 
    delay(3000); 
} 
+0

試過了,它不工作,因爲我需要:(它仍然加起來以前的計數器的值 – Mira

+0

你想有一個計數器的每個傳感器?現在它只是增加1計數器每個週期的閱讀是非零的。 –