2017-03-02 50 views
0

所以我正在做一个项目。我的任务是创建一个包含三种模式的交通信号灯系统,我可以通过向串行监视器输入数字1,2或3来选择。一切都还好,直到我决定向面包板添加三个按钮,所以我也可以通过按钮选择任何模式。到目前为止,我一直无法让Arduino同时接受来自串行监视器和按钮的输入,我不知道我是否在正确的道路上,还是不想实现我的目标。我只需要一个小指导,请。这是我目前的代码:串行监视器和按钮输入

//----------------------- Variables 
#define ECHOPIN 3 
#define TRIGPIN 2 
    char inVal; 
    String inString = ""; 
    const int red_led = 11; 
    const int yellow_led = 12; 
    const int green_led = 13; 
    const int on_delay= 2000, off_delay= 1000; //led delays 
    const int min_distance = 10; // Distance sensor min distance 
    const int The_buzzer = 4; 
    float real_distance; // Distance obtained from function 
    int ldrPin = A0; // LDR pin   
    unsigned int sensorValue = 0; 
    float voltage; 
    float light_amount; 
    int brightness = 600; // amount of light treshhold 
    int button_one = 5; 
    String ButtonOne; 


void setup() { 

    pinMode(red_led, OUTPUT); 
    pinMode(yellow_led, OUTPUT); 
    pinMode(green_led, OUTPUT); 
    pinMode(The_buzzer, OUTPUT); 
    Serial.begin(9600); 
    pinMode(ECHOPIN, INPUT); 
    pinMode(TRIGPIN, OUTPUT); 
    pinMode(button_one, INPUT); 
} 


void loop() { 


if (Serial.available()>0) 

distanceSensor(0); // distance sensor function 

{ 

    inVal=Serial.read(); 
    switch((inVal) | (ButtonOne == "HIGH")) 
{ 
    case '1': // ------------------------- Regular Mode 
    while (true) 
    { 
    red_light(); 
    yellow_light(); 
    green_light(); 
    yellow_light(); 
    } 
    break; 


case '2': // ------------------------ Pedestrian Mode 
while (true) 
{ 

real_distance = distanceSensor(0); 

if (real_distance < min_distance) 
{ 
    for (int a= 0; a < 10; a++) 
    { 
    tone(The_buzzer,1000); 
    delay(1000); 
    noTone(The_buzzer); 
    delay(1000); 
    digitalWrite(yellow_led, HIGH); 
    delay(100); 
    digitalWrite(yellow_led,LOW); 
    } 
} 

real_distance = distanceSensor(0); 

if (real_distance > min_distance) 
{ 
red_light(); 
yellow_light(); 
green_light(); 
yellow_light(); 
} 
} 
break; 


case '3': // --------------------------- NIGHT MODE 

while (true) 
{ 
light_amount = LDRSensor(0); 
real_distance = distanceSensor(0); 



if (light_amount > brightness) 
{ 
red_light(); 
yellow_light(); 
green_light(); 
yellow_light(); 
red_light(); 
delay(100); 
} 

if (light_amount < brightness || real_distance < min_distance) 
{ 

real_distance = distanceSensor(0); // distance sensor reading 

    if (real_distance > min_distance) 
    { 
    digitalWrite(green_led, LOW); 
    digitalWrite(red_led, HIGH); 
    } 

    if (real_distance < min_distance) 
    { 

    while(real_distance < min_distance && light_amount < brightness) 
    { //maybe change this 
    digitalWrite(red_led, LOW); 
    digitalWrite(green_led, HIGH); 
    real_distance = distanceSensor(0); 
    } 

    digitalWrite(green_led, LOW); 
    } 
    } 



} 
break; 

default: 
standby_mode(); // blinks all leds until 1,2 or 3 is selected 



} 
} 
} 

//--------------------------------------- FUNCTIONS ----------------------- 


//----------------------------------- Red light function 
void red_light() 
{ 
digitalWrite(red_led, HIGH); 
delay(on_delay); 
digitalWrite(red_led,LOW); 
} 

//---------------------------------- Yellow light function 
void yellow_light() 
{ 
digitalWrite(yellow_led, HIGH); 
delay(off_delay); 
digitalWrite(yellow_led,LOW); 
} 

//--------------------------------- Green light function 
void green_light() 
{ 
digitalWrite(green_led, HIGH); 
delay(on_delay); 
digitalWrite(green_led,LOW); 

} 

//------------------------------ --- Distance sensor function 
float distanceSensor(int x) 
{ 
digitalWrite(TRIGPIN, LOW); 
delayMicroseconds(2); 
digitalWrite(TRIGPIN,HIGH); 
delayMicroseconds(10); 
digitalWrite(TRIGPIN,LOW); 
float distance = pulseIn(ECHOPIN, HIGH); 
distance = distance/58; 
Serial.print(distance); 
Serial.println("cm"); 
delay(200); 

float distance_reading = distance; 

return distance_reading; 
} 

//------------------------------------- LDR sensor function 

float LDRSensor(int h) 
{ 
    sensorValue = analogRead(ldrPin);  
    voltage = sensorValue * (5000.0/1024.0); 
    Serial.print("Sensor Output: "); 
    Serial.println(sensorValue); 
    Serial.print("Voltage (mv): "); 
    Serial.println(voltage); 
    Serial.println(); 
    delay(5000); 
    return sensorValue; 
} 


//------------------------------------- Buzzer Function 

void buzzer(unsigned char delayms) 
{ 
    analogWrite(The_buzzer, 20); 
    delay(delayms); 
    analogWrite(The_buzzer, 0); 
    delay(delayms); 

} 

// ------------------------------------- Standby Mode 

void standby_mode() 
{ 
    for (int a= 10; a < 14; a++) 
    { 
    digitalWrite(a,HIGH); 
    } 

    delay(off_delay); 

    for (int b=10; b < 14; b++) 
    { 
    digitalWrite(b,LOW); 
    } 
    delay(off_delay); 

} 
+0

那是什么意外的'distanceSensor(0); //在'if(Serial.available()> 0)'和块'{...}'之间的'loop()'函数的开始处使用//距离传感器函数? –

+0

没有调用你的'loop'函数的代码,所以你可能需要发布更多。但是,AFAICT,一旦你在'switch'语句中输入'case',你就会有一个无限循环。 (例如'case'1':',while(true)'while'break'),对于每个case的while语句也是如此,所以你可能需要重构一些东西,所以你有_one_'while'和每个'case'只做一次迭代,即将循环移动到开关/外壳的外部 –

+0

问题的解决方案==>状态机 –

回答

0

我想你没有得到arduino草图的工作方式。每次在连续循环中调用loop()函数(如while(true)),所以您应该让自己的逻辑充分利用这一事实。

您在loop()函数中使用了无限循环(这已经是一个无限循环),因此您的代码会停留在这些循环中的一个循环中,永远不会跳出,因此它将永远不会读取串行缓冲区或GPIO引脚。

1

正如我在上面的评论中提到,一旦你进入一个给定的case,你永远不会离开它(即事情变得“卡住”)

所以,我说,有一个外循环,每个case只是做一个迭代。

此外,请注意,如果串行端口具有可用的输入数据,则仅在inVal中进行更改。所以,单循环方法模仿多个循环,但仍然响应输入的变化。

这里是我觉得让你更接近你的意图的东西[请原谅无偿风格清理]:

//----------------------- Variables 
#define ECHOPIN 3 
#define TRIGPIN 2 
char inVal; 
String inString = ""; 
const int red_led = 11; 
const int yellow_led = 12; 
const int green_led = 13; 
const int on_delay = 2000, 
    off_delay = 1000;     // led delays 
const int min_distance = 10;   // Distance sensor min distance 
const int The_buzzer = 4; 
float real_distance;     // Distance obtained from function 
int ldrPin = A0;      // LDR pin 
unsigned int sensorValue = 0; 
float voltage; 
float light_amount; 
int brightness = 600;     // amount of light treshhold 
int button_one = 5; 
String ButtonOne; 

void 
setup() 
{ 

    pinMode(red_led, OUTPUT); 
    pinMode(yellow_led, OUTPUT); 
    pinMode(green_led, OUTPUT); 
    pinMode(The_buzzer, OUTPUT); 
    Serial.begin(9600); 
    pinMode(ECHOPIN, INPUT); 
    pinMode(TRIGPIN, OUTPUT); 
    pinMode(button_one, INPUT); 
} 

void 
loop() 
{ 

    // distance sensor function 
    if (Serial.available() > 0) 
     distanceSensor(0); 

    while (1) { 
     if (Serial.available() > 0) 
      inVal = Serial.read(); 

     switch ((inVal) | (ButtonOne == "HIGH")) { 
     case '1': // Regular Mode 
      red_light(); 
      yellow_light(); 
      green_light(); 
      yellow_light(); 
      break; 

     case '2': // Pedestrian Mode 
      real_distance = distanceSensor(0); 

      if (real_distance < min_distance) { 
       for (int a = 0; a < 10; a++) { 
        tone(The_buzzer, 1000); 
        delay(1000); 
        noTone(The_buzzer); 
        delay(1000); 
        digitalWrite(yellow_led, HIGH); 
        delay(100); 
        digitalWrite(yellow_led, LOW); 
       } 
      } 

      real_distance = distanceSensor(0); 

      if (real_distance > min_distance) { 
       red_light(); 
       yellow_light(); 
       green_light(); 
       yellow_light(); 
      } 
      break; 

     case '3': // NIGHT MODE 
      light_amount = LDRSensor(0); 
      real_distance = distanceSensor(0); 

      if (light_amount > brightness) { 
       red_light(); 
       yellow_light(); 
       green_light(); 
       yellow_light(); 
       red_light(); 
       delay(100); 
      } 

      if (light_amount < brightness || real_distance < min_distance) { 

       real_distance = distanceSensor(0); // distance sensor reading 

       if (real_distance > min_distance) { 
        digitalWrite(green_led, LOW); 
        digitalWrite(red_led, HIGH); 
       } 

       if (real_distance < min_distance) { 

        while (real_distance < min_distance && light_amount < brightness) { // maybe change this 
         digitalWrite(red_led, LOW); 
         digitalWrite(green_led, HIGH); 
         real_distance = distanceSensor(0); 
        } 

        digitalWrite(green_led, LOW); 
       } 
      } 
      break; 

     default: // blinks all leds until 1,2 or 3 is selected 
      standby_mode(); 
      break; 
     } 
    } 
} 

//--------------------------------------- FUNCTIONS ----------------------- 

//----------------------------------- Red light function 
void 
red_light() 
{ 
    digitalWrite(red_led, HIGH); 
    delay(on_delay); 
    digitalWrite(red_led, LOW); 
} 

//---------------------------------- Yellow light function 
void 
yellow_light() 
{ 
    digitalWrite(yellow_led, HIGH); 
    delay(off_delay); 
    digitalWrite(yellow_led, LOW); 
} 

//--------------------------------- Green light function 
void 
green_light() 
{ 
    digitalWrite(green_led, HIGH); 
    delay(on_delay); 
    digitalWrite(green_led, LOW); 

} 

//------------------------------ --- Distance sensor function 
float 
distanceSensor(int x) 
{ 

    digitalWrite(TRIGPIN, LOW); 
    delayMicroseconds(2); 
    digitalWrite(TRIGPIN, HIGH); 
    delayMicroseconds(10); 
    digitalWrite(TRIGPIN, LOW); 
    float distance = pulseIn(ECHOPIN, HIGH); 

    distance = distance/58; 
    Serial.print(distance); 
    Serial.println("cm"); 
    delay(200); 

    float distance_reading = distance; 

    return distance_reading; 
} 

//------------------------------------- LDR sensor function 

float 
LDRSensor(int h) 
{ 

    sensorValue = analogRead(ldrPin); 
    voltage = sensorValue * (5000.0/1024.0); 
    Serial.print("Sensor Output: "); 
    Serial.println(sensorValue); 
    Serial.print("Voltage (mv): "); 
    Serial.println(voltage); 
    Serial.println(); 
    delay(5000); 

    return sensorValue; 
} 

//------------------------------------- Buzzer Function 

void 
buzzer(unsigned char delayms) 
{ 

    analogWrite(The_buzzer, 20); 
    delay(delayms); 
    analogWrite(The_buzzer, 0); 
    delay(delayms); 
} 

// ------------------------------------- Standby Mode 

void 
standby_mode() 
{ 

    for (int a = 10; a < 14; a++) { 
     digitalWrite(a, HIGH); 
    } 

    delay(off_delay); 

    for (int b = 10; b < 14; b++) { 
     digitalWrite(b, LOW); 
    } 
    delay(off_delay); 
} 
+0

我的先生刚刚在一个非常特殊的基座上获得了一席之地,我回顾了我的鳕鱼e和你的,发现了我所包含和纠正的小错误。这总是那么简单的事情。感谢您的支持。 @克雷格 –

+0

不客气。只是好奇。你是如何在自己的代码中修复的? (即与我的相似/不同?) –