2016-10-05 8 views
0

私は4つのfsr圧力センサーを互いに並列接続して使用しています。以下は私のプロジェクトのコーディングです。圧力センサー上のカウンターループarduino

カウンタ値に問題があります。カウンタ値には、センサーごとに4つのカウンタ値が表示されます。

圧力が検出されるたびに追加されるカウンタ値を取得する必要があります。圧力が検出されなくなると、以前のカウンタ値のままになります。

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){'の後に' fsrForce = 0'を追加すると助けになりますか? –

+0

各センサーの読み取り値に4つのカウンター値が表示されます。はい、正確に。センサーに圧力がかかっている限り、カウンタを増やしたい。 – Mira

+0

今何してる?常に 'Counter = 0'と表示されているだけですか? –

答えて

0

私は主な問題はfsrForce != 0であるかどうかをチェックしていると思うが、fsrVoltageに依存しているためfsrForceに正の値が設定されているため、fsrForceは実際にゼロになる機会がない。

fsrVoltage < VOLTAGE_THRESHOLDの場合はfsrForce = 0と設定してください。期待される動作が得られるはずです。だから、今何をするのか

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

各センサーのカウンターを持っていますか?ゼロ以外です。 –

関連する問題