Hallo, ich bastle gerade an einem alten Wählscheibentelefon und habe dort einen MP3-Player untergebracht. Man soll MP3-Files über eine Nummer abrufen können.
Das Ganze funktioniert auch schon ganz gut. Ich hätte aber gerne, daß nach dem man gewählt hat gewartet wird um festzustellen ob noch noch weitere zahlen gewählt werden da ich gerne zwei Vorwahlen benutzen möchte.
Ich habe aktuell zwei Nummern hinterlegt z.B 0049 und 009 die auch perfekt die entsprechenden MP3-Files auslösen. Bis hierhin ist alles super.
Wenn ich jetzt aber ein weiteres MP3-File mit der Nummer 00492 triggern möchte, wird das File mit der Nummer 0049 abgespielt ohne zu warten, das ich die Zahl "2" wählen kann.
Wie könnte ich das lösen?
Danke!
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int needToPrint = 0;
int count;
int dial = 2; // pin for rotary dial
int handset = 7; // pin for handset
// NeoPixel pin
#define RGBPIN 12
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int handsetnow = LOW;
int handsetbefore = LOW;
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(17, RGBPIN, NEO_GRB + NEO_KHZ800);
unsigned long previousMillis = 0;
unsigned long interval = 200;
int takt = 0;
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;
const int NUMBER_OF_DIGITS = 9; //const int NUMBER_OF_DIGITS;
char number [NUMBER_OF_DIGITS + 1]; // where we store the number
int currentDigit; // which digit we are up to
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void setup()
{
mySoftwareSerial.begin(9600);
Serial.begin(9600); // I will be printing the phone's input over serial
pinMode(dial, INPUT);
pinMode(handset, INPUT_PULLUP); // obviously we have two digital inputs
Serial.println();
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){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(25); //Set volume value. From 0 to 30
// start LED-strip ////////////////////////////////////////////////////////////
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(255);
idle(); // NeoPixel Animation
}
void loop()
{
// first we need to know if the handset is picked up or not
handsetnow = digitalRead(handset);
if( handsetnow != handsetbefore ){ // check if handset has changed
if( handsetnow == HIGH ){
Serial.println("Handset Up"); // someone picked up the handset
currentDigit = 0;
}
if( handsetnow == LOW ){
Serial.println("Handset Down"); // someone put it down
idle(); // NeoPixel Animation
}
delay(50); // 50 millisecs for stability
}
// if the handset is picked up, begin reading the numbers being dialed.
if( handsetnow == handsetbefore && handsetnow == HIGH ){
pickedUpNeo(); // NeoPixel Animation
// read what the dial pin sending us
int reading = digitalRead(dial);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
unsigned long diledMillis = millis();
// the dial isn't being used, or has just finished being dialed.
if (needToPrint) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count.
Serial.print (F("Finished a digit:"));
Serial.println(count, DEC);
number [currentDigit] = count | '0';
currentDigit++;
number [currentDigit] = 0;
Serial.print (F("Current Digit: "));
Serial.println (currentDigit);
Serial.print (F("Number acquired! Got: "));
Serial.println (number);
needToPrint = 0;
count = 0;
if (strcmp(number, "0049") == 0){
currentDigit = 0;
myDFPlayer.play(2); //Play mp3
}
else if (strcmp(number, "009") == 0){
currentDigit = 0;
myDFPlayer.play(3); //Play mp3
}
}
}
// if reading has just changed, let's record the time of change
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high: we got a pulse!
count++;
// Since '0' comes after 9 on the rotary wheel, 10 pulses really mean '0'
if ( count == 10)
{ count = 0; }
needToPrint = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading; // reset the dialed number state
}
handsetbefore = handsetnow; // reset the handset state
}
void idle(){
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(255, 255, 255));
strip.show();
}
}
void pickedUpNeo(){
if (millis() - previousMillis >= interval) {
takt = takt + 1;
previousMillis = millis();
}
if (takt == 7){
takt = 0;
}
if (takt == 0) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
}
}
if (takt == 1) {
strip.setPixelColor(6 ,strip.Color(255,255,255)); //turn every pixel on
strip.show();
}
if (takt == 2) {
strip.setPixelColor(0 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(7 ,strip.Color(255,255,255)); //turn every pixel on
strip.show();
}
if (takt == 3) {
strip.setPixelColor(1 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(8 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(13 ,strip.Color(255,255,255)); //turn every pixel on
strip.show();
}
if (takt == 4) {
strip.setPixelColor(2 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(9 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(14 ,strip.Color(255,255,255)); //turn every pixel on
strip.show();
}
if (takt == 5) {
strip.setPixelColor(5 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(3 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(12 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(10 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(15 ,strip.Color(255,255,255)); //turn every pixel on
strip.show();
}
if (takt == 6) {
strip.setPixelColor(4 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(11 ,strip.Color(255,255,255)); //turn every pixel on
strip.setPixelColor(16 ,strip.Color(255,255,255)); //turn every pixel on
strip.show();
}
if (takt == 7) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
}
}
}