Hello!
I’m trying to use Arduino mega 2560, display with 433Mhz reciever and wireless trinket. Here’s my code.
#include <MD_MAX72xx.h>
#include <MD_Parola.h>
#include <SPI.h>
#include <RCSwitch.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 16
#define CLK_PIN 52
#define DATA_PIN 51
#define CS_PIN 53
#define RF_RX_PIN 2 // Используем пин 2 для избежания конфликтов
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
RCSwitch mySwitch = RCSwitch();
// Ваши коды кнопок
unsigned long buttonCodes[] = {5393, 5396, 5380, 5397};
const char* buttonNames[] = {"BTN1", "BTN2", "BTN3", "BTN4"};
int codeCount = 4;
unsigned long lastRFCheck = 0;
unsigned long lastDisplayUpdate = 0;
int currentDisplay = 0;
void setup() {
Serial.begin(9600);
Serial.println("Initializing system with your button codes...");
// 1. Сначала инициализируем дисплей
myDisplay.begin();
myDisplay.setZone(0, 0, 15);
myDisplay.setIntensity(10);
myDisplay.displayClear();
// 2. Затем RF приемник с задержкой
delay(100);
mySwitch.enableReceive(digitalPinToInterrupt(RF_RX_PIN));
// 3. Показываем стартовое сообщение
displayText("RF REMOTE READY");
Serial.println("Button codes loaded:");
for (int i = 0; i < codeCount; i++) {
Serial.print("Button ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(buttonCodes[i]);
Serial.print(" (0x");
Serial.print(buttonCodes[i], HEX);
Serial.println(")");
}
Serial.println("Waiting for button presses...");
}
void displayText(const char* text) {
myDisplay.displayClear();
myDisplay.displayZoneText(0, text, PA_CENTER, 100, 0, PA_PRINT, PA_NO_EFFECT);
myDisplay.displayAnimate();
}
void displayScrollText(const char* text) {
myDisplay.displayZoneText(0, text, PA_LEFT, 60, 2000, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
while (!myDisplay.displayAnimate()) {
delay(10);
}
}
void handleRemoteCommand(unsigned long command) {
Serial.print("RF Command received: ");
Serial.print(command);
Serial.print(" (0x");
Serial.print(command, HEX);
Serial.println(")");
for (int i = 0; i < codeCount; i++) {
if (buttonCodes[i] == command) {
Serial.print(">>> Button ");
Serial.print(i + 1);
Serial.println(" pressed!");
switch(i) {
case 0: // Button 1 - 5393
displayText("TEMP: 25C");
Serial.println("Display: Temperature");
break;
case 1: // Button 2 - 5396
displayText("TIME: 14:30");
Serial.println("Display: Time");
break;
case 2: // Button 3 - 5380
displayText("VOLT: 12.5V");
Serial.println("Display: Voltage");
break;
case 3: // Button 4 - 5397
displayScrollText("Hello! This is scrolling text from button 4 press ");
Serial.println("Display: Scrolling text");
break;
}
return;
}
}
// Если код не распознан
Serial.println(">>> Unknown button code!");
char unknownMsg[25];
sprintf(unknownMsg, "UNKNOWN: %lu", command);
displayText(unknownMsg);
delay(2000);
displayText("PRESS KNOWN BTN");
}
void showDefaultMessages() {
const char* messages[] = {
"PRESS BUTTON 1",
"FOR TEMPERATURE",
"BUTTON 2: TIME",
"BUTTON 3: VOLT",
"BUTTON 4: TEXT"
};
static int msgIndex = 0;
static unsigned long lastMsgChange = 0;
if (millis() - lastMsgChange > 3000) {
lastMsgChange = millis();
displayText(messages[msgIndex]);
msgIndex = (msgIndex + 1) % 5;
}
}
void loop() {
// Проверяем RF команды каждые 50мс
if (mySwitch.available()) {
unsigned long value = mySwitch.getReceivedValue();
if (value != 0) {
handleRemoteCommand(value);
}
mySwitch.resetAvailable();
lastDisplayUpdate = millis(); // Сбрасываем таймер демо-режима
}
// Если прошло 10 секунд без команд, показываем демо-сообщения
if (millis() - lastDisplayUpdate > 10000) {
showDefaultMessages();
}
delay(50);
}
I wrote a code for determining button codes without using display (but it is connected all the time), and I got the codes.
But as soon as I code with using display, my 433 reciever refuses to get codes. What am I doing wrong? TIA