Arduino传感器数据

时间:2015-05-27 13:33:53

标签: arduino sensor

您好我正在使用Arduino Flex传感器来控制视频游戏角色。 传感器数据被平均并重新映射到0-6之间的值。 当玩家弯曲他们的二头肌时,它会读取最大值(这是完美的)但是如果玩家弯曲他们的手臂,并且传感器由于某种原因读取最大值6,因为玩家放松他们的手臂,则下降的屈光度值被传递到游戏引擎(不是从6变为零,而是在达到零之前从6降到5到4到3到2到1)。有人可以建议我如何改变我的代码,使传感器读数返回0而不是逐渐下降?

#define NUM_LED 6  //sets the maximum numbers of LEDs
#define MAX_Low 75 //for people with low EMG activity
#define MAX_High 150//for people with high EMG activity
#define Threshold 3 // this sets the light to activate TENS
int reading[10];
int finalReading;
int MAX = 0;
int TENS =3;
int ledState = LOW;  
byte litLeds = 0;
byte multiplier = 1;
byte leds[] = {8, 9, 10, 11, 12, 13};
char ch; 
char contact; 

void setup(){
  Serial.begin(9600); //begin serial communications
  digitalWrite(TENS, LOW); 

  for(int i = 0; i < NUM_LED; i++){ //initialize LEDs as outputs
  pinMode(leds[i], OUTPUT);
  pinMode(TENS, OUTPUT); // Set TENS output to StimPin
  }
  MAX = MAX_High; //This sets the default to people with high EMG activity.
}


void loop(){ 

  for(int i = 0; i < 10; i++){    //take ten readings in ~0.02 seconds
    reading[i] = analogRead(A0) * multiplier;
    delay(2);
  }
  for(int i = 0; i < 10; i++){   //average the ten readings
    finalReading += reading[i];
  }
  finalReading /= 10;
  for(int j = 0; j < NUM_LED; j++)
  {  
    digitalWrite(leds[j], LOW);//write all LEDs low and stim pin low
  }

  finalReading = constrain(finalReading, 0, MAX);
  litLeds = map(finalReading, 0, MAX, 0, NUM_LED);
  Serial.println(litLeds);

  for(int k = 0; k < litLeds; k++){
  digitalWrite(leds[k], HIGH); // This turns on the LEDS 
                           }
     {
        // send data only when you receive data:
        if (Serial.available() > 0) 
        {
                ch = Serial.read();
                contact=digitalRead(TENS);
                if (ch == 'A' && contact==LOW)
                  {
                      digitalWrite(TENS, HIGH);
                    }

        else if (ch == 'B' && contact==HIGH)
              {  
                digitalWrite(TENS, LOW); 
              }      
        }
}

   delay(80);
      }

1 个答案:

答案 0 :(得分:0)

您的传感器不读取6并且立即为0,您读取的速度非常快,您可以读取中间值。例如:如果玩家非常快地打开手臂(250-300毫秒,它很快),您最多可以读取3个读数,然后您可以读取3个中间值。 您可以为最后delay()添加更多时间,或仅使用与上次读数相同的值,以确保没有快速更改。