我有一个连接到arduino板上的弯曲传感器,并且我试图计算传感器来回弯曲的次数。
我设置了一个嵌套的if循环,以便必须将传感器一直弯曲到完全闭合,然后一直弯曲到完全打开,以增加计数,但是当保持打开状态时,它会不断增加计数。
仅当传感器先经过手部闭合然后再经过手部弯曲时,如何才能增加计数?
#include <Servo.h>
Servo myServo;
const int flexPin = A0;
const int ledPin = 7;
bool closed = false;
int handOpenCount = 0;
int handClosed = 150;
int handOpen = 250;
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
int flexValue;
flexValue = analogRead(flexPin);
Serial.print("sensor: ");
Serial.println(" ");
Serial.println(flexValue);
// Counts number of times hand has been open
if(flexValue<handClosed){
closed = true;}
if(closed=true){
if(flexValue>handOpen){
handOpenCount = handOpenCount + 1;
closed = false;}
Serial.println(handOpenCount);}
答案 0 :(得分:1)
您的if语句的比较器需要秒等于(==):
if(closed == true){
if(flexValue>handOpen){
handOpenCount = handOpenCount + 1;
closed = false;}
}
您当前的if语句将关闭设置为true,这样做将返回true。