Code Update

This commit is contained in:
Make It Smart
2023-01-06 20:49:09 +03:00
parent 3944ca1511
commit 9d31b45a2f
5 changed files with 454 additions and 1 deletions

View File

@@ -0,0 +1,107 @@
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
struct MyData {
byte j1PotX;
byte j1PotY;
byte j2PotX;
byte j2PotY;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
};
MyData data;
unsigned long lastRecvTime = 0;
RF24 radio(8, 7);
const uint64_t pipeIn = 0xE8E8F0F0E1LL;
#define servo_pin 3
#define motor_forward_pin 5
#define motor_backward_pin 6
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(servo_pin);
pinMode(motor_forward_pin, OUTPUT);
pinMode(motor_backward_pin, OUTPUT);
resetData();
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipeIn);
radio.startListening();
}
void loop() {
while (radio.available()) {
radio.read(&data, sizeof(MyData));
//int angle = min(130, max(45, data.j2PotX - 35));
int angle = min(130, max(45, (87 + (data.j2PotX - 127)/2)));
servo.write(angle);
/*Serial.print("throttle : ");
Serial.print(data.throttle);
Serial.print(", pot1 : ");
Serial.println(data.pot1);
Serial.print(", angle : ");
Serial.println(angle);*/
if (data.j1PotY < 100) {
digitalWrite(motor_forward_pin, LOW);
//digitalWrite(motor_backward_pin, HIGH);
//analogWrite(motor_forward_pin, 0);
analogWrite(motor_backward_pin, map(data.j1PotY, 100, 0, 180, 255));
} else if (data.j1PotY > 150) {
//digitalWrite(motor_forward_pin, HIGH);
digitalWrite(motor_backward_pin, LOW);
analogWrite(motor_forward_pin, map(data.j1PotY, 150, 255, 180, 255));
//analogWrite(motor_backward_pin, 0);
} else {
digitalWrite(motor_forward_pin, LOW);
digitalWrite(motor_backward_pin, LOW);
}
lastRecvTime = millis();
}
if (millis() - lastRecvTime > 1000) {
resetData();
servo.write(min(140, max(35, data.j2PotX - 35)));
digitalWrite(motor_forward_pin, LOW);
digitalWrite(motor_backward_pin, LOW);
}
}
void resetData() {
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
}

View File

@@ -0,0 +1,150 @@
#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
struct MyData {
byte j1PotX;
byte j1PotY;
byte j2PotX;
byte j2PotY;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
};
MyData data;
unsigned long lastRecvTime = 0;
RF24 radio(7, 8);
const uint64_t pipeIn = 0xE8E8F0F0E1LL;
#define channel1 2
#define channel3 4
#define channel2 3
#define channel4 5
#define channel5 6
#define channel6 9
#define channel7 10
Servo servo_z_axis;
Servo servo_x_axis;
Servo servo_y_axis;
Servo servo_clamp;
int x_axis_degree = 90;
int y_axis_degree = 90;
int z_axis_degree = 85;
int clamp_degree = 90;
void setup() {
Serial.begin(9600);
servo_x_axis.attach(3);
servo_y_axis.attach(5);
servo_z_axis.attach(6);
servo_clamp.attach(10);
resetData();
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipeIn);
radio.startListening();
}
void loop() {
recvData();
unsigned long now = millis();
if (now - lastRecvTime > 1000) {
Serial.println("Signal lost!");
resetData();
}
if (data.j1PotX < 80) x_axis_degree += 7;
else if (data.j1PotX > 160) x_axis_degree -= 7;
if (data.j1PotY < 80) y_axis_degree -= 7;
else if (data.j1PotY > 160) y_axis_degree += 7;
if (data.j2PotY < 80) z_axis_degree -= 7;
else if (data.j2PotY > 160) z_axis_degree += 7;
clamp_degree = data.tSwitch1 ==1 ? 75 : 90;
//*************************************
//You should decide the max/min angles.
z_axis_degree = min(145, max(15, z_axis_degree));
x_axis_degree = min(175, max(40, x_axis_degree));
y_axis_degree = min(150, max(5, y_axis_degree));
clamp_degree = min(90, max(75, clamp_degree));
Serial.print("x_axis_degree : ");
Serial.print(x_axis_degree);
Serial.print(", y_axis_degree : ");
Serial.print(y_axis_degree);
Serial.print(", z_axis_degree 4 : ");
Serial.print(z_axis_degree);
Serial.print(", clamp_degree : ");
Serial.println(clamp_degree);
servo_clamp.write(clamp_degree);
servo_x_axis.write(x_axis_degree);
servo_y_axis.write(y_axis_degree);
servo_z_axis.write(z_axis_degree);
}
void recvData() {
while (radio.available()) {
radio.read(&data, sizeof(MyData));
/*analogWrite(throtle_pin, data.throttle);
servo_yaw.write(map(data.yaw, 0, 255, 0, 180));
servo_pitch.write(map(data.pitch, 0, 255, 0, 180));
servo_roll.write(map(data.roll, 0, 255, 0, 180));*/
/*Serial.print(data.j1PotX);
Serial.print(", ");
Serial.print(data.j1PotY);
Serial.print(", ");
Serial.print(data.j2PotX);
Serial.print(", ");
Serial.print(data.j2PotY);
Serial.print(", ");
Serial.print(data.pot1);
Serial.print(", ");
Serial.print(data.pot2);
Serial.print(", ");
Serial.print(data.tSwitch1);
Serial.print(", ");
Serial.print(data.tSwitch2);
Serial.print(", ");
Serial.print(data.button1);
Serial.print(", ");
Serial.println(data.button2);*/
lastRecvTime = millis();
}
}
void resetData() {
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
}

View File

@@ -0,0 +1,119 @@
#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
struct MyData {
byte j1PotX;
byte j1PotY;
byte j2PotX;
byte j2PotY;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
};
MyData data;
unsigned long lastRecvTime = 0;
RF24 radio(7, 8);
const uint64_t pipeIn = 0xE8E8F0F0E1LL;
#define channel1 2
#define channel3 4
#define channel2 3
#define channel4 5
#define channel5 6
#define channel6 9
#define channel7 10
Servo servo1;
Servo servo2;
Servo servo3;
void setup() {
Serial.begin(9600);
pinMode(channel1, OUTPUT);
servo1.attach(channel2);
servo2.attach(channel4);
servo3.attach(channel5);
resetData();
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1, pipeIn);
radio.startListening();
}
void loop() {
recvData();
unsigned long now = millis();
if (now - lastRecvTime > 1000) {
Serial.println("Signal lost!");
resetData();
}
//delay(250);
digitalWrite(channel1, data.tSwitch1 == 1 ? HIGH : LOW);
servo1.write(map(data.j1PotX, 0, 255, 0, 180));
servo2.write(map(data.j1PotY, 0, 255, 0, 180));
servo3.write(map(data.pot1, 255, 0, 0, 180));
}
void recvData() {
while (radio.available()) {
radio.read(&data, sizeof(MyData));
/*analogWrite(throtle_pin, data.throttle);
servo_yaw.write(map(data.yaw, 0, 255, 0, 180));
servo_pitch.write(map(data.pitch, 0, 255, 0, 180));
servo_roll.write(map(data.roll, 0, 255, 0, 180));*/
/*Serial.print(data.j1PotX);
Serial.print(", ");
Serial.print(data.j1PotY);
Serial.print(", ");
Serial.print(data.j2PotX);
Serial.print(", ");
Serial.print(data.j2PotY);
Serial.print(", ");
Serial.print(data.pot1);
Serial.print(", ");
Serial.print(data.pot2);
Serial.print(", ");
Serial.print(data.tSwitch1);
Serial.print(", ");
Serial.print(data.tSwitch2);
Serial.print(", ");
Serial.print(data.button1);
Serial.print(", ");
Serial.println(data.button2);*/
lastRecvTime = millis();
}
}
void resetData() {
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
}

View File

@@ -0,0 +1,78 @@
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Define the digital inputs
#define t1 9 // Toggle switch 1
#define t2 8 // Toggle switch 1
#define b1 2 // Button 1
#define b2 3 // Button 2
RF24 radio(5, 6); // nRF24L01 (CE, CSN)
const uint64_t pipeOut = 0xE8E8F0F0E1LL;
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
byte j1PotX;
byte j1PotY;
byte j2PotX;
byte j2PotY;
byte pot1;
byte pot2;
byte tSwitch1;
byte tSwitch2;
byte button1;
byte button2;
};
Data_Package data; //Create a variable with the above structure
void setup() {
Serial.begin(9600);
// Define the radio communication
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openWritingPipe(pipeOut);
pinMode(t1, INPUT_PULLUP);
pinMode(t2, INPUT_PULLUP);
pinMode(b1, INPUT_PULLUP);
pinMode(b2, INPUT_PULLUP);
resetData();
}
void loop() {
data.j1PotX = map(analogRead(A2), 440, 605, 0, 255);
data.j1PotY = map(analogRead(A3), 587, 425, 0, 255);
data.j2PotX = map(analogRead(A0), 432, 601, 0, 255);
data.j2PotY = map(analogRead(A1), 436, 595, 0, 255);
data.pot1 = map(analogRead(A7), 0, 1023, 0, 255);
data.pot2 = map(analogRead(A6), 0, 1023, 0, 255);
data.tSwitch1 = digitalRead(t1);
data.tSwitch2 = digitalRead(t2);
data.button1 = digitalRead(b1);
data.button2 = digitalRead(b2);
radio.write(&data, sizeof(Data_Package));
}
void resetData(){
data.j1PotX = 127;
data.j1PotY = 127;
data.j2PotX = 127;
data.j2PotY = 127;
data.pot1 = 1;
data.pot2 = 1;
data.tSwitch1 = 1;
data.tSwitch2 = 1;
data.button1 = 1;
data.button2 = 1;
}