****This is my RX code. ****
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
#include <time.h>
#define RADIO_PIN_1 9
#define RADIO_PIN_2 10
#define RXPIN 4
#define TXPIN 3
#define GPSBAUD 9600 //set this value equal to the baud rate(보드 속도/데이터 전송 속도) of your GPS
//각도를 위한 핀 조정
#define ENGINE_PIN 2 //엔진 온오프를 조정하는 핀 번호
#define SERVO_PIN_1 3
#define SERVO_PIN_2 4
#define MAX_SERVO 12 //기준 최대 servo 값
RF24 radio(RADIO_PIN_1, RADIO_PIN_2);
const byte address[6] = "00001";
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
TinyGPSPlus gps;
SoftwareSerial ss(RXPIN, TXPIN);
int angle = MAX_SERVO;
int engine_state = 0;
int servo_state = 0;
void changeEngineState(bool state) {
if (state) {
digitalWrite(ENGINE_PIN, HIGH);
} else {
digitalWrite(ENGINE_PIN, LOW);
}
}
void goRight() {
if (angle < MAX_SERVO * 2) {
angle++;
digitalWrite(SERVO_PIN_1, HIGH);
digitalWrite(SERVO_PIN_2, LOW);
} else {
stopServo();
}
}
void goLeft() {
//Serial.println("goLeft()");
if (angle > 0) {
angle--;
digitalWrite(SERVO_PIN_1, LOW);
digitalWrite(SERVO_PIN_2, HIGH);
} else {
stopServo();
}
}
void stopServo() {
digitalWrite(SERVO_PIN_1, LOW);
digitalWrite(SERVO_PIN_2, LOW);
}
void setServoState(int state) {
switch (state) {
case 0:
if (angle > MAX_SERVO) {
goLeft();
}
if (angle < MAX_SERVO) {
goRight();
}
if (angle == MAX_SERVO) {
stopServo();
}
break;
case 1:
if (angle < MAX_SERVO * 2 - 1) {
goRight();
}
if (angle >= MAX_SERVO * 2 - 1) {
stopServo();
}
break;
case 2:
if (angle > 1) {
goLeft();
}
if (angle <= 1) {
stopServo();
}
break;
}
}
struct landToBoatPacket {
int command;
};
void receiveData(int &command) {
struct landToBoatPacket receivedData;
//Serial.println("receiveData");
if (radio.available()) {
radio.read(&receivedData, sizeof(receivedData));
command = receivedData.command;
Serial.println("\n\n\nradio available!");
}
delay(500);
}
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
pinMode(ENGINE_PIN, OUTPUT);
pinMode(SERVO_PIN_1, OUTPUT);
pinMode(SERVO_PIN_2, OUTPUT);
}
void loop() {
int command;
receiveData(command);
command = 1;
Serial.print("recieved command>>> ");
Serial.println(command);
switch (command) {
case 1:
Serial.println("case 1!");
engine_state = 1;
servo_state = 0;
break;
case 2:
engine_state = 0;
servo_state = 0;
break;
case 3:
servo_state = 1;
break;
case 4:
servo_state = 2;
break;
case 5: //
digitalWrite(SERVO_PIN_1, LOW);
digitalWrite(SERVO_PIN_2, HIGH);
delay(50);
digitalWrite(SERVO_PIN_1, LOW);
digitalWrite(SERVO_PIN_2, LOW);
break;
case 6: //
digitalWrite(SERVO_PIN_1, HIGH);
digitalWrite(SERVO_PIN_2, LOW);
delay(50);
digitalWrite(SERVO_PIN_1, LOW);
digitalWrite(SERVO_PIN_2, LOW);
break;
case 7: //방향 OFF
servo_state = 0;
break;
default:
Serial.println("default");
break;
}
setServoState(servo_state);
changeEngineState(engine_state);
delay (1000)
This is my RX code, but the connected arduino is enlighted on the TX side, not the RX side.
Could you please check what the incorrect part of the code is?
I learned this method just a few days ago. PLEASE help me.. :sob