Hi,
i'm making a morse code translator and there are several problems with it.
The traslator is not working at all
I checked that there's nothing wrong with making whole String dashORdot, but the String dashORdot needs to be converted into char* type and I think there are some errors with this code for it.
or is it because of other errors?
#define SW 8
#define RELEASED 0
#define SHORT_PRESS 1
#define LONG_PRESS 2
#define SW_THRESHOLD 500
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,2,3,4,5);
const int convert_button=9;
boolean buttonStatus;
int oneTimeFlag;
char* letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", ".." , // A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." }; // S-Z
char* alphabet[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
String dashORdot;
void setup() {
pinMode(SW,INPUT);
pinMode(convert_button,INPUT);
digitalWrite(convert_button,LOW);
Serial.begin(9600);
lcd.begin(16,2);
}
void loop() {
static unsigned long last;
int swState = readSw();
if (SHORT_PRESS == swState) {
Serial.print(".");
dashORdot.concat(".");
}
else if (LONG_PRESS == swState) {
Serial.println("-");
dashORdot.concat("-");
}
if (digitalRead(convert_button)==HIGH) {
if (oneTimeFlag==0) {
oneTimeFlag=1;
buttonStatus=!buttonStatus;
}
else {
oneTimeFlag=0;
}
if (buttonStatus==1) {
convert();
}
else {
lcd.clear();
}
}
}
int readSw()
{
static boolean prev = HIGH;
static unsigned long last, pressed;
boolean curr;
//Serial.println("last"+last);
if ((millis()-last) >= 10) {
curr = digitalRead(SW);
if ((HIGH==prev)&&(LOW==curr)) {
pressed = millis();
prev = LOW;
}
else if ((LOW==prev)&&(HIGH==curr)) {
prev = HIGH;
if ((millis()-pressed) > SW_THRESHOLD) {
return LONG_PRESS;
}
else {
return SHORT_PRESS;
}
}
last = millis();
}
return RELEASED;
}
char* string2char(String command){
if(command.length()!=0){
char *p = const_cast<char*>(command.c_str());
return p;
}
}
void convert()
{
int len=(sizeof(letters)/sizeof(letters[0]));
int x;
for (x=0; x<len; x++) {
if (strcmp(string2char(dashORdot),letters[x])==0) {
lcd.print(alphabet[x]);
delay(500);
}
}
}