Hi there,
So I'm kinda new to Arduino and C, and have been trying to create a transceiver device that takes button inputs as morse dots and dashes, and sends them over 433MHz, where it's translated and printed on the lcd screen.
The issue i have is that after installing the nRF905, i ran out of pins and decided to take up an I/o expander shield running on the mcp23017. So far i've tried my best, but just am not very good at this.
My code is as follows
#include <LiquidCrystal.h>
#include <RadioHead.h>
#include <RH_NRF905.h>
#include <RH_Serial.h>
#include <Arduino.h>
#include <string.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <RH_NRF905.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp;
const int
rs = 8,
en = 11,
d4 = 5,
d5 = 4,
d6 = 3,
d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int contrast = 75;
String morse;
int sizeOf = morse.length();
String morse1;
uint8_t buf[RH_NRF905_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
// constants won't change. They're used here to set pin numbers:
const int buttonPinShort = A0;
const int buttonPinLong = A1;
const int buttonPinSend = A3;
const int buttonPinSpace = A2;// the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
const int buzzer = 7;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
// Variables will change:
int ledState = HIGH;
String text = "";
int characterAscii = 0;
int startPos = 0, endPos = 0;
int startPos1 = 0, endPos1 = 0;
String characterCode = "";
int buttonStateLong = 0;
int buttonStateShort = 0;
int buttonStateSpace = 0;
int buttonStateSend = 0;
int lastButtonStateShort = LOW; // the previous reading from the input pin
int lastButtonStateLong = LOW;
int lastButtonStateSpace = LOW;
int lastButtonStateSend = LOW;
char p[150];
using namespace std;
// Singleton instance of the radio driver
RH_NRF905 nrf905;
void setup()
{
mcp.begin();
morse = "";
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
if (!nrf905.init())
Serial.println("init failed");
// Defaults after init are 433.2 MHz (channel 108), -10dBm
pinMode(buttonPinShort, INPUT); //set up for buttons
pinMode(buttonPinLong, INPUT);
pinMode(buttonPinSpace, INPUT);
pinMode(buttonPinSend, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
digitalWrite(6, contrast);
lcd.begin(16, 2);
lcd.setCursor(0, 15);
}
void loop()
{
// read the state of the switch into a local variable:
int readingShort = digitalRead(buttonPinShort);
int readingLong = digitalRead(buttonPinLong);
int readingSpace = digitalRead(buttonPinSpace);
int readingSend = digitalRead(buttonPinSend);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
if (buttonStateShort == HIGH) {
ledState = HIGH;
}
if (buttonStateLong == HIGH) {
ledState = HIGH;
}
else ledState = LOW;
// If the switch changed, due to noise or pressing:
if (readingShort != lastButtonStateShort) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if (readingLong != lastButtonStateLong) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
if (readingShort != buttonStateShort) {
buttonStateShort = readingShort;
// only toggle the LED if the new button state is HIGH
if (buttonStateShort == HIGH) {
morse = morse += ".";
} // press for a dot
}
if (readingLong != buttonStateLong) {
buttonStateLong = readingLong;
// only toggle the LED if the new button state is HIGH
if (buttonStateLong == HIGH) {
morse = morse += "-";
} // press for a dash
}
if (readingSpace != buttonStateSpace) {
buttonStateSpace = readingSpace;
// only toggle the LED if the new button state is HIGH
if (buttonStateSpace == HIGH) {
morse = morse += " ";
} // press for a space between letters
// press 3 times between words
}
if (readingSend != buttonStateSend) {
buttonStateSend = readingSend;
// only toggle the LED if the new button state is HIGH
if (buttonStateSend == HIGH) {
lcd.println(morse);
Serial.println("Sending to nrf905_server");
morse.toCharArray(p, sizeOf);
uint8_t data[sizeof(p)] = {p};
nrf905.send(data, sizeof(p));
Serial.println(morse);
nrf905.waitPacketSent();
if (nrf905.waitAvailableTimeout(500))
{ Serial.println("waitavTimeout");
// Should be a reply message for us now
if (nrf905.recv(buf, &len))
{ Serial.println("recv buffer");
morse1 = (char*)buf;
morse1.replace(" .- ", "a ");
morse1.replace(" -... ", "b ");
morse1.replace(" -.-. ", "c ");
morse1.replace(" -.. ", "d ");
morse1.replace(" . ", "e ");
morse1.replace(" ..-. ", "f ");
morse1.replace(" --. ", "g ");
morse1.replace(" .... ", "h ");
morse1.replace(" .. ", "i ");
morse1.replace(" .--- ", "j ");
morse1.replace(" -.- ", "k ");
morse1.replace(" .-.. ", "l ");
morse1.replace(" -- ", "m ");
morse1.replace(" -. ", "n ");
morse1.replace(" --- ", "o ");
morse1.replace(" .--. ", "p ");
morse1.replace(" --.- ", "q ");
morse1.replace(" .-. ", "r ");
morse1.replace(" ... ", "s ");
morse1.replace(" - ", "t ");
morse1.replace(" -.. ", "u ");
morse1.replace(" ...- ", "v ");
morse1.replace(" .-- ", "w ");
morse1.replace(" -..- ", "x ");
morse1.replace(" -.-- ", "y ");
morse1.replace(" --.. ", "z ");
morse1.replace(" .---- ", "1 ");
morse1.replace(" ..--- ", "2 ");
morse1.replace(" ...-- ", "3 ");
morse1.replace(" ....- ", "4 ");
morse1.replace(" ..... ", "5 ");
morse1.replace(" ....- ", "6 ");
morse1.replace(" ...-- ", "7 ");
morse1.replace(" ..--- ", "8 ");
morse1.replace(" .---- ", "9 ");
morse1.replace(" ----- ", "0 ");
morse1.replace(" ", " ");
Serial.println(morse1);
lcd.print(morse1);
delay(1000);
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(500);
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
// wait a bit:
delay(200);
lcd.clear();
}
}
}
else
{
}
}
}
}
if (ledState == HIGH) {
tone(buzzer, 3423);
}
else noTone(buzzer) ;
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonStateShort = readingShort;
lastButtonStateLong = readingLong;
lastButtonStateSpace = readingSpace;
}
else
{
}
delay(400);
}
But it isn't working. The lcd doesn't turn on and the 'send' button doesn't call for the array to be properly sent through the rf module...
I'm trying to use this for a school project, but clearly haven't been successful.
Is anyone able to guide me in the right direction, how to fix the lcd problem, the sending issue.
I also dont get any sound when I press the '-' and '.' buttons.
Thanks!