Радио модуль выдаёт странное значение
- Войдите на сайт для отправки комментариев
Чт, 21/11/2019 - 20:42
Всем доброго времени суток!
Столкнулся с проблемой, что радиомодуль 443 мгц после нужного символа присылает странное нигде не прописанное число (25208).
Код отправки:
#include <VirtualWire2.h>
#include <VirtualWire2_Config.h>
/////////////////////////////METAL/////////////////////////////////////////////////////////////////////////////////////
#define sound 0 // 0 - в стиле счётчика гейгера, 1 - пищалка верещалка
#define default_mode 0 // если нет переключателя режимов, то по умолчанию стоит: 1 - статический, 0 - динамический
#define sens_change 0 // 0 - нет регулировки чувствительности, 1 - есть
float SENSITIVITY = 1000.0; // ручная установка чувствительности
//------------НАСТРОЙКИ-------------
#define resetPinGND 12
#define resetPin 0
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+
unsigned long t0 = 0;
int t = 0;
unsigned char tflag = 0;
float clf;
int v0 = 0;
float f = 0;
unsigned int FTW = 0;
unsigned int PCW = 0;
unsigned long timer = 0;
// Срабатываем каждые 1000 импульсов с генератора
SIGNAL(TIMER1_COMPA_vect)
{
OCR1A += 1000;
t = micros() - t0;
t0 += t;
tflag = 1;
}
/////////////////////////////MASHINA/////////////////////////////////////////////////////////////////////////////////////
#include <LiquidCrystal_I2C.h> //including libraries of I2C-LCD1602 liquid crystal
#include <Wire.h>
#define Bluetooth_speed_up -
#define Bluetooth_speed_down +
#define IR_Bluetooth_Control 0x00ff38c7
unsigned char Bluetooth_val; // ultrasonic module TRIG to A1
LiquidCrystal_I2C lcd(0x27,16,2); //defining liquid crystal
#define Lpwm_pin 11 //pin of controlling speed---- ENA of motor driver board
#define Rpwm_pin 3 //pin of controlling speed---- ENB of motor driver board
int pinLB=4; //2-4 //pin of controlling turning---- IN1 of motor driver board
int pinLF=2; //4-2 //pin of controlling turning---- IN2 of motor driver board
int pinRB=7; //pin of controlling turning---- IN3 of motor driver board
int pinRF=8; //pin of controlling turning---- IN4 of motor driver board
unsigned char Lpwm_val = 150; //initialized left wheel speed at 250
unsigned char Rpwm_val = 150; //initialized right wheel speed at 250
int Car_state=0; //the working state of car
void M_Control_IO_config(void)
{
pinMode(pinLB,OUTPUT); // /pin 2
pinMode(pinLF,OUTPUT); // pin 4
pinMode(pinRB,OUTPUT); // pin 7
pinMode(pinRF,OUTPUT); // pin 8
pinMode(Lpwm_pin,OUTPUT); // pin 11 (PWM)
pinMode(Rpwm_pin,OUTPUT); // pin10(PWM)
}
void Set_Speed(unsigned char Left,unsigned char Right) //function of setting speed
{
analogWrite(Lpwm_pin,Left);
analogWrite(Rpwm_pin,Right);
}
void advance() // going forward
{
digitalWrite(pinRB,LOW); // making motor move towards right rear
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,LOW); // making motor move towards left rear
digitalWrite(pinLF,HIGH);
Car_state = 1;
show_state();
}
void turnR() //turning right(dual wheel)
{
digitalWrite(pinRB,LOW); //making motor move towards right rear
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,LOW); //making motor move towards left front
Car_state = 4;
show_state();
}
void turnL() //turning left(dual wheel)
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,LOW ); //making motor move towards right front
digitalWrite(pinLB,LOW); //making motor move towards left rear
digitalWrite(pinLF,HIGH);
Car_state = 3;
show_state();
}
void stopp() //stop
{
digitalWrite(pinRB,HIGH);
digitalWrite(pinRF,HIGH);
digitalWrite(pinLB,HIGH);
digitalWrite(pinLF,HIGH);
Car_state = 5;
show_state();
}
void back() //back up
{
digitalWrite(pinRB,HIGH); //making motor move towards right rear
digitalWrite(pinRF,LOW);
digitalWrite(pinLB,HIGH); //making motor move towards left rear
digitalWrite(pinLF,LOW);
Car_state = 2;
show_state() ;
}
void show_state(void)
{
lcd.setCursor(0, 1);
switch(Car_state)
{
case 1:lcd.print(" Go ");
// Serial.print(" \r\n GO");
break;
case 2:lcd.print("Back ");
// Serial.print(" \r\n Back");
break;
case 3:lcd.print("Left ");
//Serial.print(" \r\n Left");
break;
case 4:lcd.print("Right");
// Serial.print(" \r\n Right");
break;
case 5:lcd.print("Stop ");
//Serial.print(" \r\n Stop");
break;
default:
break;
}
}
void LCD1602_init(void) //including initialized function of liquid crystal
{
lcd.init(); //invoking initialized function of LCD in LiquidCrystal_I2C.h
delay(10); //delaying for 10 millisecond
lcd.backlight(); //open backlight of LCD1602
lcd.clear(); //clear screen
}
void Show_V(unsigned char V)
{
lcd.setCursor(11, 0);
lcd.print("V= ");
lcd.setCursor(13, 0);
lcd.print(V,DEC);
Serial.print("\n Speed = ");
Serial.print(V,DEC);
}
void Bluetooth_Control() //Bluetooth remote control
{
lcd.setCursor(0, 0); //setting cursor in the first row and column
lcd.print("Bluetooth ");
if(Serial.available()) //to judge whether serial port receive data
{
Bluetooth_val=Serial.read(); //reading value of Bluetooth serial port, giving the value to val
switch(Bluetooth_val)
{
case 'U':advance(); //UP
break;
case 'D': back(); //back
break;
case 'L':turnL(); //Left
break;
case 'R':turnR(); //Right
break;
case 'S':stopp(); //stop
break;
case '+': if( Rpwm_val+10<=250 && Rpwm_val+10<=250){ //increasing speed
if( Rpwm_val+10<=250 && Lpwm_val+10<=250){ //increasing speed
Lpwm_val+=10; Rpwm_val+=10;
Set_Speed(Lpwm_val,Rpwm_val);
Show_V(Lpwm_val);
}
break;
case '-':if(Rpwm_val-10>=0&&Rpwm_val-10>=0){ //decreasing speed
Lpwm_val-=10; Rpwm_val-=10;
Set_Speed(Lpwm_val,Rpwm_val);
Show_V(Lpwm_val);
}
break;
}
}
}}
void setup()
{
LCD1602_init(); //initializing 1602
M_Control_IO_config(); //motor controlling the initialization of IO
Set_Speed(Lpwm_val,Rpwm_val); //setting initialized speed
// Sensor_IO_Config(); //initializing IO of line tracking module
// pinMode(inputPin, INPUT); //starting receiving IR remote control signal
// pinMode(outputPin, OUTPUT); //IO of ultrasonic module
Serial.begin(9600); //initialized serial port , using Bluetooth as serial port, setting baud
lcd.setCursor(0, 0); //setting cursor at 0.0
pinMode(resetPin, INPUT_PULLUP);
pinMode(resetPinGND, OUTPUT);
digitalWrite(resetPinGND, 0);
//-Set up counter1 to count at pin 5
TCCR1A = 0;
TCCR1B = 0x07;
SET(TIMSK1, OCF1A);
stopp();
vw_set_ptt_inverted(true); // Необходимо для DR3100
vw_setup(2000); // Устанавливаем скорость передачи (бит/с)
}
// функция модуля
float absf(float f)
{
if (f < 0.0)
return -f;
else
return f;
}
void loop()
{
{
char symbol = 'c';
// Ждем пока передача будет окончена
while(IR_Bluetooth_Control)
{
Bluetooth_Control(); //pressing “OK” on remote controller, and entering remote control mode
if (tflag)
{
Serial.print(f);
lcd.setCursor(7, 1);
lcd.print("f= ");
lcd.setCursor(9, 1);
lcd.print(f);
if (f>10)
{
stopp();
Serial.println("metall");
String strMsg = "z ";
strMsg += symbol;
char msg[255];
strMsg.toCharArray(msg, 255);
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
}
{ // если кнопка режимов не нажата
if (!digitalRead(resetPin)) // если нажата кнопка сброса частоты
v0 = t; // запомнить текущую частоту
}
}
f = f * 0.85 + absf(t - v0) * 0.15; // фильтруем сигнал
tflag = 0; //-Reset flag
delay(50);
}
}
}
Код приёма:
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600);
vw_set_ptt_inverted(true); // Необходимо для DR3100
vw_setup(2000); // Задаем скорость приема
vw_rx_start(); // Начинаем мониторинг эфира
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN]; // Буфер для сообщения
uint8_t buflen = VW_MAX_MESSAGE_LEN; // Длина буфера
if (vw_get_message(buf, &buflen)) // Если принято сообщение
{
// Начинаем разбор
int i;
// Если сообщение адресовано не нам, выходим
if (buf[0] != 'z')
{
return;
}
char command = buf[2]; // Команда находится на индексе 2
// Числовой параметр начинается с индекса 4
i = 4;
int number = 0;
// Поскольку передача идет посимвольно, то нужно преобразовать набор символов в число
while (buf[i] != ' ')
{
number *= 10;
number += buf[i] - '0';
i++;
}
Serial.print(command);
Serial.print(" ");
Serial.println(number);
Ссылка на VirtualWire: https://voltiq.ru/arduino-libraries/VirtualWire.zip
CR LF?
Хвост кода пропал. Вставьте ещё раз, там интересно.