1

I am working on a morse interpreter. I can now use light-sensor and a regular key as input device and want to add a mic to the system. I recieve the input convert it to text and show it on nokia 5110 lcd. And also whilst input, an led and a buzzer turns on and off simultaneously. When I use mic the led works just as it should but I do not get any correct input to the lcd. I attached Out and Gnd pins of mic just as I would attach the key and vcc to 5v.

The following is the code which I use

#define RST 12
#define CE  11
#define DC  10
#define DIN  9
#define CLK  8

#include "font.h";



unsigned long signal_len,t1,t2; //time for which button is pressed
int inputPin=2; //input pin for push button
int ledPin=4; //output pin for LED
String code=""; //string in which one alphabet is stored

void LcdWriteString(char *characters){
  while(*characters) LcdWriteCharacter(*characters++);
}

void LcdWriteCharacter(char character){
for(int i=0;i<5;i++) LcdWriteData(ASCII[character-0x20][i]);
LcdWriteData(0x00);
}
void LcdWriteData(byte dat)
{
  digitalWrite(DC, HIGH); //DC pin is low for commands
  digitalWrite(CE, LOW);
  shiftOut(DIN, CLK, MSBFIRST, dat); //transmit serial data
  digitalWrite(CE, HIGH);
}
void LcdWriteCmd(byte cmd)
{
  digitalWrite(DC, LOW); //DC pin is low for commands
  digitalWrite(CE, LOW);
  shiftOut(DIN, CLK, MSBFIRST, cmd); //transmit serial data
  digitalWrite(CE, HIGH);


}
void setup()

{
  Serial.begin(9600);
  pinMode(inputPin, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit
  pinMode(ledPin,OUTPUT);

    pinMode(RST, OUTPUT);
  pinMode(CE, OUTPUT);
  pinMode(DC, OUTPUT);
  pinMode(DIN, OUTPUT);
  pinMode(CLK, OUTPUT);
  digitalWrite(RST, LOW);
  digitalWrite(RST, HIGH);

  LcdWriteCmd(0x21);  // LCD extended commands
  LcdWriteCmd(0xA9);  // set LCD Vop (contrast)
  LcdWriteCmd(0x04);  // set temp coefficent
  LcdWriteCmd(0x14);  // LCD bias mode 1:40
  LcdWriteCmd(0x20);  // LCD basic commands
  LcdWriteCmd(0x0C);  // LCD normal video


  for(int i=0;i<504;i++)LcdWriteData(0x00);
}

void loop()
{
NextDotDash:
  while (digitalRead(inputPin) == HIGH) {}
  t1 = millis();                            //time at button press
  digitalWrite(ledPin, HIGH);               //LED on while button pressed
  while (digitalRead(inputPin) == LOW) {}
  t2 = millis();                            //time at button release
  digitalWrite(ledPin, LOW);                //LED off on button release
  signal_len = t2 - t1;                     //time for which button is pressed
  if (signal_len > 50)                      //to account for switch debouncing
  {
    code += readio();                       //function to read dot or dash
  }
  while ((millis() - t2) < 500)           //if time between button press greater than 0.5sec, skip loop and go to next alphabet
  {     
    if (digitalRead(inputPin) == LOW)
    {
      goto NextDotDash;
    }
  }
  convertor();                          //function to decipher code into alphabet
}
char readio()
{
  if (signal_len < 150 && signal_len > 20)
  {
    return '.';                        //if button press less than 0.6sec, it is a dot
  }
  else if (signal_len > 150)
  {
    return '-';                        //if button press more than 0.6sec, it is a dash
  }
}
void convertor()
{
  static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
                             ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
                            };
  int i = 0;
  if (code == ".-.-.-")
  {
    Serial.print(".");        //for break
    LcdWriteCharacter(".");
  }
  else
  {
    while (letters[i] != "E")  //loop for comparing input code with letters array
    {
      if (letters[i] == code)
      {
        Serial.print(char('A' + i));
        LcdWriteCharacter(char('A' + i));
        break;
      }
      i++;
    }
    if (letters[i] == "E")
    {
      Serial.println("");  //if input code doesn't match any letter, error
      LcdWriteCharacter("");
    }
  }
  code = "";                            //reset code to blank string
}
  • print the signal_len – Juraj May 8 '19 at 14:56
  • Please don't use goto, unless you really really need it. Using this can lead to big problems. There is a reason, why it is seldomly used in C/C+. You can just put the convertor() inside the if, where the goto is, and add a break after it. – chrisl May 10 '19 at 7:04
  • Depending on what "mic" you use, you may also need a pre-amplifier – chrisl May 10 '19 at 7:04
1

I attached Out and Gnd pins of mic just as I would attach the key and vcc to 5v.

This can't work. Unlike the key, the microphone is an analog device that will output a continuously varying analog signal. If you expose it to a pure tone it will output a sine wave. If you sing a note in front of it, it will pick not only the sine but also a lot of harmonics. In any case, it will also pick the ambient noise.

What you need to do is somehow determine the amplitude of the sound signal, then compare that to some threshold in order to decide whether the amplitude is high enough to be considered “on”. You could use a diode-based peak detector and a comparator. Doing all this in code is kind of tricky. You may want to take a look at this sound-level meter sketch, but note that a software peak detection could be simpler than the true-RMS intensity detection implemented in that sketch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.