1

我正在尝试制作一个 Arduino 项目,我需要光的价值来确定歌曲何时在 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.
  //  }
}
4

1 回答 1

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.
  //  }
}
于 2019-04-25T07:22:12.800 回答