首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Arduino的自动冲洗

基于Arduino的自动冲洗
EN

Stack Overflow用户
提问于 2018-04-22 14:34:22
回答 1查看 55关注 0票数 0

我想创建一个基于Arduino的自动冲洗使用3个LED和伺服电机。问题是:

  1. 当人被检测到时,第一个发光二极管应该发光,第二个发光二极管应该低发光。
  2. 直到那个人站起来,第二个LED就应该高了。
  3. 当人离开时,第二个LED应该是低的,而3个LED应该是高的,然后再低。

我被困在第二部分了。

代码语言:javascript
复制
int trigPin = 12; //triggor pin
int echoPin = 11; // echo pin
long timeperiod;
float cm, distance;
int r = 10, g = 9, rg = 8;
int PreparingToFlush = 0;
void setup()
{
    Serial.begin(9600); //serial port communication
    pinMode(trigPin, OUTPUT); // defining pinmode for trig
    pinMode(echoPin, INPUT); // defining pinmode for echo pin
    pinMode(r, OUTPUT);
    pinMode(g, OUTPUT);
    pinMode(rg, OUTPUT);
}

void loop()
{
    digitalWrite(trigPin, LOW); // sending 10 us pulse
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(echoPin, LOW);

    timeperiod = pulseIn(echoPin, HIGH); // integrating pulse

    cm = (timeperiod / 2) / 29.1;
    distance = cm;
    Serial.print("   distance in centimeters=");
    Serial.println(cm);
    delay(1000);
    /* while(distance<=20)
      {
        pinMode(r,HIGH);  //person detected
        delay(1000);
        pinMode(r,LOW);
        delay(1000);
        pinMode(rg,HIGH); //wait
        delay(1000);      //Want delay such that till the person is standing in range of 20cm rg should be high
        pinMode(rg,LOW);
        delay(1000);
         pinMode(g,HIGH);// ready to flush
        delay(1000);
        pinMode(g,LOW);
        delay(1000);
        } 
    while(distance<=20)
    {
       pinMode(r,HIGH);  //person detected
        delay(1000);
        pinMode(r,LOW);
        delay(1000);
        pinMode(rg,HIGH);
    }
       pinMode(rg,LOW);
       delay(1000);
       pinMode(g,HIGH);
       delay(1000);
       pinMode(g,LOW);
       */

    if (distance < 100) { //if distance sensor detects someone
        Serial.println("person detected");
        delay(2000); // wait
        digitalWrite(r, HIGH);
        delay(2000);
        digitalWrite(r, LOW);
        //sensor=analogRead(sensorPin);
        if (distance < 100) { // check again to make sure someone is actually there
            Serial.println("again check");
            digitalWrite(g, HIGH);

            PreparingToFlush = 1;
        }
    }

    if (PreparingToFlush == 1) { //if a person has been detected
        if (distance > 100) { // if the person has now left
            digitalWrite(g, LOW);
            delay(2000);
            digitalWrite(rg, HIGH);
            delay(2000);
            digitalWrite(rg, LOW);
            Serial.println("left");
            //servo1.write(175); FLUSH
            delay(5000);
            //servo1.write(3);

            delay(1000);

            PreparingToFlush = 0; //reset the trigger
        }
    }

    delay(10);
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-24 11:10:52

您可以通过简单的状态机解决任务。

  1. 定义变量"State“时,它将保存以下状态之一:”等待“(有人接近时等待)、”接近“(当人移动到传感器时)、”输出“(当人移出)、”刷新“(在进行刷新时)。
  2. 创建一些变量来监视从某个事件中传递的时间。
  3. 当满足某些条件时,从一种状态转移到另一种状态。

初步:

代码语言:javascript
复制
#define STATE_WAIT 0
#define STATE_NEAR 1
#define STATE_OUT 2
#define STATE_FLUSH 3

int state=STATE_WAIT;
int t=0; // Variable to store time for measurement needs
int light_t=0; // Variable to store time for LED switching needs

在循环中:

代码语言:javascript
复制
switch ( state ) {
//We are in a waiting state
case STATE_WAIT:
    // Measure distance
    ...
    distance = cm;
    // If distance is quite close:
    if (distance < 50)
    {
        if (t == 0)
        {
            t = millis();
        }
        // If distance is quite close for 500 milliseconds:
        // (half of a second) 
        else if (millis() - t > 500)
        {
            // let's switch to STATE_NEAR
            state = STATE_NEAR;
            // remember, when we switched the light on
            light_t = millit();
            // switch the light on
            digitalWrite(YourFirstLedPin, HIGH);
            // peson is standing? Yes, so light the second LED too:
            digitalWrite(YourSecondLedPin, HIGH);
            // reset auxilary time variable:
            t = 0;
        }
    }
    else
    {
        t = 0;
    }
break;
// Person is near the device
case STATE_NEAR:
    // Measure distance
    ...
    distance = cm;

    // if light_t is set, and it was set 1000 msecs ago
    // lets switch off the light
    if (light_t != 0 && millis() - light_t > 1000)
    {
        light_t = 0;
        // switch the first light off
        digitalWrite(YourFirstLedPin, LOW); 
    }

    // If person is far:
    if (distance > 100)
    {
        if (t == 0)
        {
            t = millis();
        }
        // Person is far for 500 milliseconds
        else if (millis() - t > 500)
        {
            // Move to OUT state:
            state = STATE_OUT;
            // Anyway, switch first LED off.
            digitalWrite(YourFirstLedPin, LOW); 
            // switch the second light off
            digitalWrite(YourSecondLedPin, LOW);                 
            // switch the third light on
            digitalWrite(YourThirdLedPin, HIGH);
            // remember when it happend:
            light_t = millis();            
        }
    }

break;
case STATE_OUT:
    // And so on...
    // Wait for 1000 milliseconds, switch off the third light, 
    // execute Flushing mechanism, change to STATE_FLUSH
break;
case STATE_FLUSH:
    // Wait while your flushing is done,
    // Move to STATE_WAIT, and everything will star over.
break;
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49967078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档