/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <Wire.h>
#include <hd44780.h> // main hd44780 header https://www.arduino.cc/reference/en/libraries/hd44780
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <Toggle.h> // https://www.arduino.cc/reference/en/libraries/toggle
const uint8_t nbCols = 16;
const uint8_t nbRows = 2;
hd44780_I2Cexp lcd;
const byte scanPin = 2;
Toggle scanButton;
const uint16_t minFrequency = 8750;
const uint16_t maxFrequency = 10800;
const uint16_t frequencyStep = 10;
const unsigned long pauseDuration = 25; // ms in between 2 scans
uint16_t currentFrequency = minFrequency;
byte currentRssi;
const byte rssiThreshold = 4;
enum : byte {AT_REST, SCANNING} scanState = AT_REST;
const byte customChars[][8] PROGMEM = {
{0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00100},
{0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b00100},
{0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b00100},
{0b00000, 0b00000, 0b00000, 0b00000, 0b01110, 0b01110, 0b01110, 0b00100},
{0b00000, 0b00000, 0b00000, 0b11111, 0b01110, 0b01110, 0b01110, 0b00100},
{0b00000, 0b00000, 0b11111, 0b11111, 0b01110, 0b01110, 0b01110, 0b00100},
{0b00000, 0b11111, 0b11111, 0b11111, 0b01110, 0b01110, 0b01110, 0b00100},
{0b11111, 0b11111, 0b11111, 0b11111, 0b01110, 0b01110, 0b01110, 0b00100},
};
const byte customCharsCnt = sizeof customChars / sizeof * customChars;
void showRssi() {
static byte rssiOnLCD = 100;
if (rssiOnLCD != currentRssi) {
lcd.setCursor(nbCols - 1, 0);
lcd.write(currentRssi);
rssiOnLCD = currentRssi;
}
}
byte updateRSSI() {
currentRssi = map(analogRead(A0), 0, 1024, 0, customCharsCnt);
showRssi();
return currentRssi;
}
void showFrequency() { // freq x 100 => minFrequency to maxFrequency
static uint16_t freqOnLcd = 0;
if (freqOnLcd != currentFrequency) {
char freqString[10];
if (currentFrequency < minFrequency || currentFrequency > maxFrequency) freqString[0] = '\0';
else dtostrf(currentFrequency / 100.0, 6, 2, freqString);
lcd.setCursor(0, 0);
lcd.print(freqString);
freqOnLcd = currentFrequency;
}
}
void handleScan() {
static unsigned long chrono;
updateRSSI();
switch (scanState) {
case AT_REST:
scanButton.poll();
if (scanButton.onPress()) {
chrono = millis() - pauseDuration;
scanState = SCANNING;
}
break;
case SCANNING:
scanButton.poll();
if (scanButton.onPress()) {
// pressing the button will stop the scan
scanState = AT_REST; // we have a signal top there
}
else if (millis() - chrono >= pauseDuration) {
// go to next frequency
currentFrequency += frequencyStep;
if (currentFrequency > maxFrequency) currentFrequency = minFrequency;
showFrequency();
chrono = millis();
} else {
if (currentRssi >= rssiThreshold) {
scanState = AT_REST; // we have a signal top there
}
}
break;
}
}
void setup() {
scanButton.begin(scanPin);
Serial.begin(115200);
int result = lcd.begin(nbCols, nbRows);
if (result) {
Serial.print("LCD initialization failed: ");
Serial.println(result);
hd44780::fatalError(result);
}
for (byte c = 0; c < customCharsCnt; c++) lcd.createChar(c, customChars[c]);
showFrequency();
lcd.print(F(" MHz"));
}
void loop() {
handleScan();
}