压力传感器arduino上的计数器回路

时间:2016-10-05 14:10:01

标签: arduino counter

我使用四个fsr压力传感器并联连接。以下是我项目的编码。

我的计数器值有问题,它为每个传感器显示四个附加的计数器值。

我需要获得一个计数器值,每次检测到压力时它都会被添加,如果没有压力,它将保持之前的计数器值。



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);
}
&#13;
&#13;
&#13;

实际问题如下我需要找到循环计数器值的正确条件:

&#13;
&#13;
 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);
  }
  }
&#13;
&#13;
&#13;

FOUR COUNTER VALUES IN EACH ANALOG READING

1 个答案:

答案 0 :(得分:0)

我认为主要的问题是你正在检查是否fsrForce != 0,但是fsrForce实际上没有机会达到零,因为它依赖于fsrVoltage并且仅在fsrVoltage为正时才设置。

只需在fsrForce = 0时设置fsrVoltage < 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);
}