Very new to coding and have been pulling my hair out trying to figure out how to add a few features to this transmitter receiver code i found online. my problem is i want to have channel 9 to 11 to trigger a LOW or HIGH signals when their corresponding buttons are pressed on the transmitter instead of being set up to control servos. Again im very new to coding. here are the codes in question Use code tags to format code for the forum
TRANSMITTER CODE
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const uint64_t pipeOut = 000322; // NOTE: The same as in the receiver 000322 | Alıcı kodundaki adres ile aynı olmalı
RF24 radio(9, 10); // select CE,CSN pin | CE ve CSN pinlerin seçimi
struct Signal {
byte throttle;
byte pitch;
byte roll;
byte yaw;
byte aux1;
byte aux2;
byte aux3;
byte aux4;
byte aux5;
byte aux6;
byte aux7;
};
Signal data;
void ResetData()
{
data.throttle = 0;
data.pitch = 127;
data.roll = 127;
data.yaw = 127;
data.aux1 = 0; // Signal lost position | Sinyal kesildiğindeki pozisyon
data.aux2 = 0;
data.aux3 = 0;
data.aux4 = 0;
data.aux5 = 0;
data.aux6 = 0;
data.aux7 = 0;
}
void setup()
{
//Configure the NRF24 module | NRF24 modül konfigürasyonu
radio.begin();
radio.openWritingPipe(pipeOut);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS); // The lowest data rate value for more stable communication | Daha kararlı iletişim için en düşük veri hızı.
radio.setPALevel(RF24_PA_MAX); // Output power is set for maximum | Çıkış gücü maksimum için ayarlanıyor.
radio.stopListening(); // Start the radio comunication for Transmitter | Verici için sinyal iletişimini başlatır.
ResetData();
}
// Joystick center and its borders | Joystick merkez ve sınırları
int Border_Map(int val, int lower, int middle, int upper, bool reverse)
{
val = constrain(val, lower, upper);
if ( val < middle )
val = map(val, lower, middle, 0, 128);
else
val = map(val, middle, upper, 128, 255);
return ( reverse ? 255 - val : val );
}
void loop()
{
// Control Stick Calibration for channels | Her bir kanal için kumanda Kol Kalibrasyonları
data.roll = Border_Map( analogRead(A3), 0, 512, 1023, true ); // "true" or "false" for signal direction | "true" veya "false" sinyal yönünü belirler
data.pitch = Border_Map( analogRead(A2), 0, 512, 1023, true );
data.throttle = Border_Map( analogRead(A1),0, 512, 1023, false ); // For Single side ESC | Tek yönlü ESC için
data.yaw = Border_Map( analogRead(A0), 0, 512, 1023, true );
data.aux1 = digitalRead(2);//forward
data.aux2 = digitalRead(3);
data.aux3 = digitalRead(4);//eye
data.aux4 = digitalRead(5);
data.aux5 = digitalRead(6);//talk
data.aux6 = digitalRead(7);//reverse
data.aux7 = digitalRead(8);//reverse
radio.write(&data, sizeof(Signal));
}
RECEIVER CODE
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
int ch_width_1 = 0;
int ch_width_2 = 0;
int ch_width_3 = 0;
int ch_width_4 = 0;
int ch_width_5 = 0;
int ch_width_6 = 0;
int ch_width_7 = 0;
int ch_width_8 = 0;
int ch_width_9 = 0;
int ch_width_10 = 0;
int ch_width_11 = 0;
Servo ch1;
Servo ch2;
Servo ch3;
Servo ch4;
Servo ch5;
Servo ch6;
Servo ch7;
Servo ch8;
Servo ch9;
Servo ch10;
Servo ch11;
struct Signal {
byte throttle;
byte pitch;
byte roll;
byte yaw;
byte aux1;
byte aux2;
byte aux3;
byte aux4;
byte aux5;
byte aux6;
byte aux7;
};
Signal data;
const uint64_t pipeIn = 000322;
RF24 radio(9, 10);
void ResetData()
{
data.throttle = 0;
data.roll = 127;
data.pitch = 127;
data.yaw = 127;
data.aux1 = 0; // Define the inicial value of each data input. | Veri girişlerinin başlangıç değerleri
data.aux2 = 0;
data.aux3 = 0;
data.aux4 = 0;
data.aux5 = 0;
data.aux6 = 0;
data.aux7 = 0;
}
void setup()
{
// Set the pins for each PWM signal | Her bir PWM sinyal için pinler belirleniyor.
ch1.attach(0);
ch2.attach(2);
ch3.attach(3);
ch4.attach(4);
ch5.attach(5);
ch6.attach(6);
ch7.attach(7);
ch8.attach(8);
ch9.attach(A3);
ch10.attach(A4);
ch11.attach(A5);
ResetData(); // Configure the NRF24 module | NRF24 Modül konfigürasyonu
radio.begin();
radio.openReadingPipe(1,pipeIn);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS); // The lowest data rate value for more stable communication | Daha kararlı iletişim için en düşük veri hızı.
radio.setPALevel(RF24_PA_MAX); // Output power is set for maximum | Çıkış gücü maksimum için ayarlanıyor.
radio.startListening(); // Start the radio comunication for receiver | Alıcı için sinyal iletişimini başlatır.
}
unsigned long lastRecvTime = 0;
void recvData()
{
while ( radio.available() ) {
radio.read(&data, sizeof(Signal));
lastRecvTime = millis(); // Receive the data | Data alınıyor
}
}
void loop()
{
recvData();
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
ResetData(); // Signal lost.. Reset data | Sinyal kayıpsa data resetleniyor
}
ch_width_1 = map(data.roll, 0, 255, 600, 2000);
ch_width_2 = map(data.pitch, 0, 255, 1200, 2000);
ch_width_3 = map(data.throttle, 0, 255, 1000, 2000);
ch_width_4 = map(data.yaw, 0, 255, 600, 2000);
ch_width_5 = map(data.aux5, 1, 0, 1000, 2000);
ch_width_6 = map(data.aux4, 1, 0, 800, 2300);
ch_width_7 = map(data.aux3, 1, 0, 1200, 2000);
ch_width_8 = map(data.aux2, 1, 0, 1350, 2000);
ch_width_9 = map(data.aux1, 1, 0, 1000, 2000); //FORWARD
ch_width_10 = map(data.aux6, 0, 1, 1000, 2000); //REVERSE
ch_width_11 = map(data.aux7, 0, 1, 1000, 2000); //IRIS OPEN
ch1.writeMicroseconds(ch_width_1); // Write the PWM signal | PWM sinyaller çıkışlara gönderiliyor
ch2.writeMicroseconds(ch_width_2);
ch3.writeMicroseconds(ch_width_3);
ch4.writeMicroseconds(ch_width_4);
ch5.writeMicroseconds(ch_width_5);
ch6.writeMicroseconds(ch_width_6);
ch7.writeMicroseconds(ch_width_7);
ch8.writeMicroseconds(ch_width_8);
ch9.writeMicroseconds(ch_width_9);
ch10.writeMicroseconds(ch_width_10);
ch11.writeMicroseconds(ch_width_11);
}