Arduino光电电阻

时间:2019-04-22 22:51:48

标签: arduino arduino-uno arduino-ide arduino-c++

我正在尝试制作一个Arduino项目,在该项目中我需要light的值来确定mp3模块上何时播放歌曲。我正在尝试遍历发送到光敏电阻的值,但是我只收到1个数字,如何获得一个连续的值/数据循环?

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX

DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

void photoLoop() {
  Serial.begin(2400);
  pinMode(lrdPin, INPUT);
  int ldrStatus = analogRead(ldrPin);
  Serial.println(ldrStatus);
}

void setup() {

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  } else {
    photoLoop();
  }

  myDFPlayer.volume(30);  //Set volume value. From 0 to 30
  myDFPlayer.play(3);  //Play the first mp3
}

void loop() {

  while (!Serial.available()); //wait until a byte was received
  analogWrite(9, Serial.read());//output received byte

  static unsigned long timer = millis();

  if (millis() - timer > 3000) {
    timer = millis();
    //myDFPlayer.next();  //Play next mp3 every 3 second.
  }

  //  if (myDFPlayer.available()) {
  //    printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  //  }
}

1 个答案:

答案 0 :(得分:0)

您不是在回路功能中从光敏电阻读取值。

您只能读取一次值

  int ldrStatus = analogRead(ldrPin);

在设置中也是如此。因此您只会收到一个号码。

为什么要使用两个 Serial.begin()语句-115200和2400?

尝试一下

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(10, 11); // RX, TX

DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

void photoLoop() {
  //  Serial.begin(2400); // YOU DONT NEED THIS
  pinMode(lrdPin, INPUT);
  int ldrStatus = analogRead(ldrPin);
  Serial.println(ldrStatus);
}

void setup() {

  mySoftwareSerial.begin(9600);
  Serial.begin(115200);

  Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));

  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true);
  } 
  else {
    photoLoop();
  }

  myDFPlayer.volume(30);  //Set volume value. From 0 to 30
  myDFPlayer.play(3);  //Play the first mp3
}

void loop() {
  //CALL photoLoop in LOOP
  photoLoop()
    while (!Serial.available()); //wait until a byte was received
  analogWrite(9, Serial.read());//output received byte

  static unsigned long timer = millis();

  if (millis() - timer > 3000) {
    timer = millis();

    //myDFPlayer.next();  //Play next mp3 every 3 second.
  }

  //  if (myDFPlayer.available()) {
  //    printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
  //  }
}