Init fork from Stuart Robinson's repo
This commit is contained in:
@ -0,0 +1,189 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 19/03/20
|
||||
|
||||
This program is supplied as is, it is up to the user of the program to decide if the program is
|
||||
suitable for the intended purpose and free from errors.
|
||||
*******************************************************************************************************/
|
||||
|
||||
/*******************************************************************************************************
|
||||
Program Operation - This is a remote control transmitter that uses a LoRa link to transmit the positions
|
||||
from a simple joystick to a remote receiver. The receiver uses the sent joystick positions to adjust the
|
||||
positions of servos. The postions of the joysticks potentiometers on the transmitter are read with the
|
||||
analogueRead() function.
|
||||
|
||||
If the joystick has a switch, often made by pressing on the joystick, then this can be used to remote
|
||||
control an output on the receiver. The switch is read by an interrupt, the interrupt routine sets a flag
|
||||
byte which is read in loop().
|
||||
|
||||
The program is intended as a proof of concept demonstration of how to remote control servos, the program
|
||||
is not designed as a practical remote control device for RC model cars for instance.
|
||||
|
||||
To have the transmitter program print out the values read from the joystick, comment in the line;
|
||||
|
||||
//#define DEBUG
|
||||
|
||||
Which is just above the loop() function. With the DEBUG enabled the transmission rate, the rate at which
|
||||
the control packets are transmitted will be slowed down.
|
||||
|
||||
To reduce the risk of the receiver picking up LoRa packets from other sources, the packet sent contains a
|
||||
'TXidentity' number, valid values are 0 - 65535. The receiver must be setup with the matching identity
|
||||
number or the received packets will be ignored.
|
||||
|
||||
The pin definitions, LoRa frequency and LoRa modem settings are in the Settings.h file. These settings
|
||||
are not necessarily optimised for long range.
|
||||
|
||||
Serial monitor baud rate is set at 115200.
|
||||
*******************************************************************************************************/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <SX128XLT.h>
|
||||
#include "Settings.h"
|
||||
#include <ProgramLT_Definitions.h>
|
||||
|
||||
SX128XLT LT;
|
||||
|
||||
#include "PinChangeInterrupt.h" //get the library here; https://github.com/NicoHood/PinChangeInterrupt
|
||||
|
||||
uint32_t TXpacketCount;
|
||||
uint8_t TXPacketL;
|
||||
|
||||
uint8_t joystickX1value; //variable to read the value from the analog pin
|
||||
uint8_t joystickY1value; //variable to read the value from the analog pin
|
||||
|
||||
volatile bool switch1flag = false;
|
||||
|
||||
//#define DEBUG //comment in thie line (remove the two // at the beggining) for debug output
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint8_t switchByte = 0xFF;
|
||||
|
||||
joystickX1value = (uint8_t) (analogRead(joystickX1) / 4) ; //read the joystick X1 pot, turn 0-1023 into 0 to 255
|
||||
joystickY1value = (uint8_t) (analogRead(joystickY1) / 4); //read the joystick Y1 pot
|
||||
|
||||
if (switch1flag)
|
||||
{
|
||||
bitClear(switchByte, 1); //if the switch is down clear the bit
|
||||
digitalWrite(LED1, HIGH); //turn on LED as switch indicator
|
||||
switch1flag = false;
|
||||
}
|
||||
|
||||
if (!sendJoystickPacket(joystickX1value, joystickY1value, switchByte))
|
||||
{
|
||||
Serial.print(F("Send Error - IRQreg,"));
|
||||
Serial.print(LT.readIrqStatus(), HEX);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint8_t sendJoystickPacket(uint16_t X1value, uint16_t Y1value, uint8_t switches)
|
||||
{
|
||||
//The SX12XX buffer is filled with variables of a known type and in a known sequence. Make sure the
|
||||
//receiver uses the same variable types and sequence to read variables out of the receive buffer.
|
||||
|
||||
LT.startWriteSXBuffer(0); //start the write packet to buffer process
|
||||
LT.writeUint8(RControl1); //this is the packet type
|
||||
LT.writeUint8(TXIdentity); //this value represents the transmitter number
|
||||
LT.writeUint8(X1value); //this byte contains joystick pot AD X1 value to be sent
|
||||
LT.writeUint8(Y1value); //this byte contains joystick pot AD Y1 value to be sent
|
||||
LT.writeUint8(switches); //switches value
|
||||
LT.endWriteSXBuffer(); //close the packet, thee are 5 bytes to send
|
||||
|
||||
//now transmit the packet, 10 second timeout, and wait for it to complete sending
|
||||
TXPacketL = LT.transmitSXBuffer(0, PacketLength, 10000, TXpower, WAIT_TX);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print(TXIdentity);
|
||||
Serial.print(F(",X1,"));
|
||||
Serial.print(joystickX1value);
|
||||
Serial.print(F(",Y1,"));
|
||||
Serial.print(joystickY1value);
|
||||
Serial.print(F(","));
|
||||
Serial.print(switches, BIN);
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
digitalWrite(LED1, LOW); //LED off, may have been on due to switch press
|
||||
|
||||
return TXPacketL; //TXPacketL will be 0 if there was an error sending
|
||||
}
|
||||
|
||||
|
||||
void led_Flash(uint16_t flashes, uint16_t delaymS)
|
||||
{
|
||||
uint16_t index;
|
||||
for (index = 1; index <= flashes; index++)
|
||||
{
|
||||
digitalWrite(LED1, HIGH);
|
||||
delay(delaymS);
|
||||
digitalWrite(LED1, LOW);
|
||||
delay(delaymS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void attachInterrupts()
|
||||
{
|
||||
if (SWITCH1 >= 0)
|
||||
{
|
||||
attachPCINT(digitalPinToPCINT(SWITCH1), wake1, FALLING);
|
||||
switch1flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void detachInterrupts()
|
||||
{
|
||||
if (SWITCH1 >= 0)
|
||||
{
|
||||
detachPCINT(digitalPinToPCINT(SWITCH1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void wake1()
|
||||
{
|
||||
switch1flag = true;
|
||||
}
|
||||
|
||||
|
||||
void setupSwitches()
|
||||
{
|
||||
if (SWITCH1 >= 0)
|
||||
{
|
||||
pinMode(SWITCH1, INPUT_PULLUP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(LED1, OUTPUT);
|
||||
led_Flash(2, 125);
|
||||
|
||||
setupSwitches();
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
SPI.begin();
|
||||
|
||||
if (LT.begin(NSS, NRESET, RFBUSY, DIO1, LORA_DEVICE))
|
||||
{
|
||||
led_Flash(2, 125);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("Device error"));
|
||||
while (1)
|
||||
{
|
||||
led_Flash(50, 50);
|
||||
}
|
||||
}
|
||||
|
||||
LT.setupLoRa(Frequency, Offset, SpreadingFactor, Bandwidth, CodeRate);
|
||||
|
||||
attachInterrupts();
|
||||
|
||||
Serial.println(F("35_Remote_Control_Servo_Transmitter ready"));
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
/*******************************************************************************************************
|
||||
Programs for Arduino - Copyright of the author Stuart Robinson - 19/03/20
|
||||
|
||||
This program is supplied as is, it is up to the user of the program to decide if the program is
|
||||
suitable for the intended purpose and free from errors.
|
||||
*******************************************************************************************************/
|
||||
|
||||
|
||||
//******* Setup hardware pin definitions here ! ***************
|
||||
|
||||
//These are the pin definitions for one of my own boards, the Easy Pro Mini,
|
||||
//be sure to change the definitions to match your own setup.
|
||||
|
||||
const int8_t NSS = 10; //select on LoRa device
|
||||
const int8_t NRESET = 9; //reset on LoRa device
|
||||
const int8_t RFBUSY = 7; //RF busy on LoRa device
|
||||
const int8_t DIO1 = 3; //DIO1 on LoRa device, used for RX and TX done
|
||||
const int8_t LED1 = 8; //On board LED, logic high is on
|
||||
|
||||
#define LORA_DEVICE DEVICE_SX1280 //this is the device we are using
|
||||
|
||||
const int8_t joystickX1 = A2; //analog pin for the joystick 1 X pot
|
||||
const int8_t joystickY1 = A3; //analog pin for the joystick 1 Y pot
|
||||
const int8_t SWITCH1 = 2; //switch on joystick, set to -1 if not used
|
||||
|
||||
const uint32_t TXIdentity = 123 ; //define a transmitter number, the receiver must use the same number
|
||||
//range is 0 to 255
|
||||
|
||||
|
||||
|
||||
//******* Setup LoRa Test Parameters Here ! ***************
|
||||
|
||||
//LoRa Modem Parameters
|
||||
#define Frequency 2445000000 //frequency of transmissions
|
||||
#define Offset 0 //offset frequency for calibration purposes
|
||||
#define Bandwidth LORA_BW_0400 //LoRa bandwidth
|
||||
#define SpreadingFactor LORA_SF7 //LoRa spreading factor
|
||||
#define CodeRate LORA_CR_4_5 //LoRa coding rate
|
||||
|
||||
const uint8_t PacketLength = 5; //packet length is fixed
|
||||
const int8_t TXpower = 10; //LoRa transmit power in dBm
|
||||
Reference in New Issue
Block a user