我使用四個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
那麼什麼是現在怎麼辦?每次對4個傳感器中的任何一個施加壓力時,是否只需要增加計數器?在if(fsrVoltage == 0){'help?'後加'fsrForce = 0' –
它在每個傳感器讀數上顯示四個計數器值。對,就是這樣。只要傳感器上有壓力,我就希望計數器增加。 – Mira
現在在做什麼?它是否只是一直顯示'Counter = 0'? –