从串口监视器和按钮输入

时间:2017-03-02 22:49:05

标签: arduino arduino-uno

所以我正在做一个项目。我的任务是通过将数字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);

}

2 个答案:

答案 0 :(得分:1)

As I mentioned in my top comments, once you enter a given case, you never leave it (i.e. things get "stuck")

So, as I said, have a single outer loop, and each case just does one iteration.

Also, note that, below, inVal only gets changed if the serial port has input data available. So, the single loop approach mimics the multiple loops but still responds to changes in input.

Here is something that I think gets you closer to your intent [please pardon the gratuitous style cleanup]:

//----------------------- 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);
}

答案 1 :(得分:0)

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

你在loop()函数中使用了无限循环(这已经是一个无限循环),所以你的代码卡在其中一个循环中而永远不会出来,所以它永远不会读取串行缓冲区或GPIO引脚