Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- //Tells Arduino to use pins 12 and 13 to communicate with GSM Shield
- SoftwareSerial GPRS(12, 13);
- //Declaration of integer variables. *State watches the button state *LastState is used to see if there's a change in the state, *Counter is used for counting how many times button was pressed
- int ditState, ditLastState, dahLastState, dahState, spaceState, spaceLastState, spaceCounter, delState, delLastState, delCounter, sendState, sendLastState, sendCounter;
- //Declaration of string variables. txtmsg is the final message, others are buffers for translation and appending
- String txtmsg, codeBuffer, letterBuffer, wordBuffer;
- void setup()
- {
- //Initialization of variables
- spaceCounter = 0; delCounter = 0; sendCounter = 0;
- txtmsg = ""; codeBuffer = ""; letterBuffer = ""; wordBuffer = "";
- //Assigns input values to variables (INPUT_PULLUP turns on pin's internal pullup resistor bc I'm too lazy to add resistors. Note that this inverts the values)
- pinMode(2, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(6, INPUT_PULLUP);
- pinMode(8, INPUT_PULLUP);
- pinMode(10, INPUT_PULLUP);
- //Initializes ports (for communication)
- GPRS.begin(9600); Serial.begin(9600);
- }
- void loop()
- {
- //Displays new messages from GSM Shield to serial monitor
- while(GPRS.available()){
- Serial.write(GPRS.read());
- }
- //redefinition of variables (updates on each loop)
- ditLastState = ditState; ditState = digitalRead(2);
- dahLastState = dahState; dahState = digitalRead(4);
- spaceLastState = spaceState; spaceState = digitalRead(6);
- delLastState = delState; delState = digitalRead(8);
- sendLastState = sendState; sendState = digitalRead(10);
- //Reads dit button input
- if(ditState!=ditLastState){
- if(ditState==LOW){
- //resets counters for buttons with multiple presses for multiple functions
- spaceCounter = 0; delCounter = 0; sendCounter = 0;
- //adds dit to code buffer (to be used for translating)
- codeBuffer = codeBuffer + ".";
- }
- }
- if(dahState!=dahLastState){
- if(dahState==LOW){
- spaceCounter = 0; delCounter = 0; sendCounter = 0;
- //adds dah to code buffer
- codeBuffer = codeBuffer + "-";
- }
- }
- if(spaceState!=spaceLastState){
- if(spaceState==LOW){
- delCounter = 0; sendCounter = 0;
- //counts how many times space button is pressed
- spaceCounter++;
- //on single press starts new letter
- if(spaceCounter==1){
- letterBuffer = morse(); wordBuffer = wordBuffer + letterBuffer; codeBuffer = "";
- }
- //on double press adds space
- else if(spaceCounter==2){
- txtmsg = txtmsg + " " + wordBuffer; wordBuffer = "";
- }
- //on triple pressing adds .
- else{
- txtmsg = txtmsg + "."; spaceCounter = 0;
- }
- }
- }
- if(delState!=delLastState){
- if(delState==LOW){
- spaceCounter = 0; sendCounter = 0;
- //counts how many times del button is pressed
- delCounter++;
- //on single press, clears code buffer (current letter being typed)
- if(delCounter==1){
- codeBuffer = "";
- }
- //on double press, clears word buffer (current word being typed)
- else if(delCounter==2){
- wordBuffer = "";
- }
- //on triple press, clears whole text message
- else{
- txtmsg = "";
- }
- }
- }
- if(sendState!=sendLastState){
- if(sendState==LOW){
- sendCounter++;
- if(sendCounter==3){
- //upon triple pressing send, finishes text
- letterBuffer = morse(); wordBuffer = wordBuffer + letterBuffer; letterBuffer = "";
- txtmsg = txtmsg + " " + wordBuffer; wordBuffer = "";
- //sends
- Serial.println("AT+CMGS=\"+63569737780\"");
- Serial.println(txtmsg);
- GPRS.println("AT+CMGS=\"+63569737780\"");
- GPRS.println(txtmsg);
- GPRS.write(0x1a);
- txtmsg = ""; spaceCounter = 0; delCounter = 0; sendCounter = 0;
- }
- }
- }
- }
- //runs through morse characters and compares to current word upon pressing space, and returns translation. * is for characters that don't translate to anything, and "" is for del button, bc that bugs out without it.
- String morse(){
- if(codeBuffer==".-"){return "a";}
- else if(codeBuffer=="-..."){return "b";}
- else if(codeBuffer=="-.-."){return "c";}
- else if(codeBuffer=="-.."){return "d";}
- else if(codeBuffer=="."){return "e";}
- else if(codeBuffer=="..-."){return "f";}
- else if(codeBuffer=="--."){return "g";}
- else if(codeBuffer=="...."){return "h";}
- else if(codeBuffer==".."){return "i";}
- else if(codeBuffer==".---"){return "j";}
- else if(codeBuffer=="-.-"){return "k";}
- else if(codeBuffer==".-.."){return "l";}
- else if(codeBuffer=="--"){return "m";}
- else if(codeBuffer=="-."){return "n";}
- else if(codeBuffer=="---"){return "o";}
- else if(codeBuffer==".--."){return "p";}
- else if(codeBuffer=="--.-"){return "q";}
- else if(codeBuffer==".-."){return "r";}
- else if(codeBuffer=="..."){return "s";}
- else if(codeBuffer=="-"){return "t";}
- else if(codeBuffer=="..-"){return "u";}
- else if(codeBuffer=="...-"){return "v";}
- else if(codeBuffer==".--"){return "w";}
- else if(codeBuffer=="-..-"){return "x";}
- else if(codeBuffer=="-.--"){return "y";}
- else if(codeBuffer=="--.."){return "z";}
- else if(codeBuffer=="----"){return "ch";}
- else if(codeBuffer==".----"){return "1";}
- else if(codeBuffer=="..---"){return "2";}
- else if(codeBuffer=="...--"){return "3";}
- else if(codeBuffer=="....-"){return "4";}
- else if(codeBuffer=="....."){return "5";}
- else if(codeBuffer=="-...."){return "6";}
- else if(codeBuffer=="--..."){return "7";}
- else if(codeBuffer=="---.."){return "8";}
- else if(codeBuffer=="----."){return "9";}
- else if(codeBuffer=="-----"){return "0";}
- else if(codeBuffer=="..--.."){return "\?";}
- else if(codeBuffer==".-..."){return "&";}
- else if(codeBuffer==".----."){return "\'";}
- else if(codeBuffer==".--.-."){return "@";}
- else if(codeBuffer=="-.--.-"){return ")";}
- else if(codeBuffer=="-.--."){return "(";}
- else if(codeBuffer=="---..."){return ":";}
- else if(codeBuffer=="--..--"){return ",";}
- else if(codeBuffer=="-...-"){return "=";}
- else if(codeBuffer==".-.-.-"){return ".";}
- else if(codeBuffer=="-....-"){return "-";}
- else if(codeBuffer==".-.-."){return "+";}
- else if(codeBuffer==".-..-."){return "\"";}
- else if(codeBuffer=="-..-."){return "/";}
- else if(codeBuffer==""){return "";}
- else{return "*";}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement