【问题标题】:Need expertice on code for Arduino based security system需要有关基于 Arduino 的安全系统的代码专业知识
【发布时间】:2015-07-31 15:31:07
【问题描述】:

到目前为止,这是我的代码,系统包含一个 SD 卡读卡器、两个蜂鸣器、两个 LED、一个 PIR 运动传感器和一个 IR 接收器。

我得到的错误信息如下:

1) Final_Build:100: 错误:重新定义'int val'

2) Final_Build:5: error: 'int val' 之前在这里定义

3) Final_Build:101: error: 'if' 之前的预期 unqualified-id

4) 重新定义'int val'

//PIR sensor
int ledPin = 7;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int pinSpeaker = 10;  //Set up a speaker on a PWM pin (digital 9, 10, or 11)

//IR_Sensor
int dtect=3;
int sense=A0;
int buzzpin=9;
int lightPin=6;
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  //PIR sensor
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);

  //IR sensor
  pinMode(dtect,OUTPUT);
  pinMode(lightPin,OUTPUT);
  pinMode(sense,INPUT);
  pinMode(buzzpin,OUTPUT);
  digitalWrite(dtect,HIGH);
  Serial.begin(9600);

  while (!Serial) {
; // wait for serial port to connect.
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
  Serial.write(myFile.read());
}
// close the file:
myFile.close();
  } else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
  }
}

void loop(){
  //IR sensor
  int val=analogRead(sense);
  Serial.println(val);
  if(val>=800)
  {
    digitalWrite(lightPin, HIGH);
    buzz(50);
  }
  else {
    digitalWrite(lightPin, LOW);
  }


}
void buzz(unsigned char time)
  {
    analogWrite(buzzpin,170);
    delay(time);
    analogWrite(buzzpin,0);
    delay(time);
  }

  //PIR sensor
 int val = digitalRead(inputPin);  // read input value
 if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(100);


    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }

}

【问题讨论】:

  • 很明显,你真的定义了int val 两次。

标签: security arduino


【解决方案1】:

第一个val 定义不是必需的。

int val = 0;                    // variable for reading the pin status

您在 loop() 的开头重新定义 val

更新: 我认为这就是你想要的。您的问题是添加的 buzz 函数是在 loopfunction 中定义的,而您丢失了一些 parentesis{}。我刚刚发布并加入了loopcode。

//PIR sensor
int ledPin = 7;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int pinSpeaker = 10;  //Set up a speaker on a PWM pin (digital 9, 10, or 11)

//IR_Sensor
int dtect=3;
int sense=A0;
int buzzpin=9;
int lightPin=6;
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  //PIR sensor
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);

  //IR sensor
  pinMode(dtect,OUTPUT);
  pinMode(lightPin,OUTPUT);
  pinMode(sense,INPUT);
  pinMode(buzzpin,OUTPUT);
  digitalWrite(dtect,HIGH);
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect.
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop(){
  //IR sensor
  int val=analogRead(sense);
  Serial.println(val);
  if(val>=800)
  {
    digitalWrite(lightPin, HIGH);
    buzz(50);
  }
  else {
    digitalWrite(lightPin, LOW);
  }

  //PIR sensor
 int val = digitalRead(inputPin);  // read input value
 if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(100);


    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(ledPin, LOW); // turn LED OFF
    playTone(0, 0);
    delay(300);    
    if (pirState == HIGH){
      // we have just turned off
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

void buzz(unsigned char time)
  {
    analogWrite(buzzpin,170);
    delay(time);
    analogWrite(buzzpin,0);
    delay(time);
  }

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }

}

【讨论】:

  • 好的,我删除了那行代码,但现在它给了我错误“Final_Build:100: error: expected unqualified-id before 'if' expected unqualified-id before 'if'” on line if val = high" 在 //PIR 传感器下
  • 好的。您的问题是您丢失了}{,因为这部分代码不在任何函数中。我无法对其进行更好的审查,但我认为这很容易找到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
  • 2016-09-06
  • 2011-01-25
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 2012-04-20
相关资源
最近更新 更多