//Libraries for NRF24L01+ module. #include<SPI.h> #include<nRF24L01.h> #include<RF24.h> //RF24 object with two pins defined with arguments. CE: 4, CSN: 5 RF24 radio(4, 5); //Address of the pipe. 40 bit long, you can choose this freely. //Remember to use different address in different projects. longlong address = 0x1234ABCDEFLL;
int count = 0; char stext[32] = ""; int spos = 0; char rtext[32] = ""; int rpos = 0;
voidsendText(char * text, int tlen) { radio.stopListening(); radio.openWritingPipe(address); radio.write(stext, tlen);
//Libraries for NRF24L01+ module. #include<SPI.h> #include<nRF24L01.h> #include<RF24.h> //RF24 object with two pins defined with arguments. CE: 4, CSN: 5 RF24 radio(4, 5); //Address of the pipe. 40 bit long, you can choose this freely. //Remember to use different address in different projects. longlong address = 0x1234ABCDEFLL;
voidsetup(){ //Start the radio Serial.begin(115200); delay(2000); Serial.println("Enter username: "); radio.begin(); radio.setRetries(3, 5); //Open reading pipe with given address and start listening for incoming data radio.openWritingPipe(address); radio.openReadingPipe(0, address); radio.setPALevel(RF24_PA_MIN); }
//Libraries for NRF24L01+ module. #include<SPI.h> #include<RF24.h> //RF24 object with two pins defined with arguments. CE: 4, CSN: 5 RF24 radio(4, 5); //Address of the pipe. 40 bit long, you can choose this freely. //Remember to use different address in different projects. longlong address = 0x1234ABCDEFLL;
// for temperature, humidity sensor #include"DHT.h" #define DHTPIN 13 // data pin #define DHTTYPE DHT22 // change to DHT11 if you're using the DHT11 //float temphumi[2]; // AM2301(DHT21) -> DHT21, AM2302(DHT22) -> DHT22 uint16_t temphumi[2]; DHT dht(DHTPIN, DHTTYPE);
voidsetup(){ //Start the radio Serial.begin(115200);
radio.begin(); radio.openWritingPipe(address); //RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX //NRF24L01: -18dBm, -12dBm,-6dBM, and 0dBm radio.setPALevel(RF24_PA_LOW); radio.stopListening(); dht.begin(); } voidloop(){ //Get temperature from the sensor uint16_t t = dht.readTemperature(); uint16_t h = dht.readHumidity();
temphumi[0] = t; temphumi[1] = h;
//Send the temperature wirelessly, print error if failed if (!radio.write(&temphumi, sizeof(temphumi))) { Serial.println(F("Sending temperature failed")); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println(F("Failed to read from DHT sensor!")); return; } delay(2000); } }
// for no interference, two joysticks must be used with each other ADC! // one joystick use ADC1: GPIO 36,39,32,33,34,35 // the other must use ADC2: GPIO 4,0,2,15,13,12,14,27,25,26 constint Jstick_x_pin = 25; // Left_Right GPIO25 constint Jstick_y_pin = 32; // Forward_Back GPIO32
// for no interference, two joysticks must be used with each other ADC! // one joystick use ADC1: GPIO 36,39,32,33,34,35 // the other must use ADC2: GPIO 4,0,2,15,13,12,14,27,25,26 constint Jstick_x_pin = 25; // Left_Right GPIO25 constint Jstick_y_pin = 32; // Forward_Back GPIO32
2개의 joystick을 동시에 사용할때 동일한 ADC의 GPIO를 사용하면, 한쪽 joystick을 움직여 값을 변화시킬 경우 다른 한쪽 joystick을 움직이지 않았음에도 동시에 값이 일부 (소폭) 변화하는 현상이 생긴다.
이 문제를 해결하기 위해 각각의 joystick을 서로 다른 ADC에 연결한다.
ADC1: GPIO 36,39,32,33,34,35 중 1개 사용 (예제에서는 GPIO 32를 y축 joystick으로 사용)
ADC2: GPIO 4,(0),(2),15,13,(12),14,27,25,26 중 1개 사용 (()안의 핀은 사용시 주의 필요) (예제에서는 GPIO 25를 x축 joystick으로 사용)
단, ESP32의 wifi 기능을 이용하면 ADC1핀 중에서 wifi 기능에 공유되는 핀의 사용에 제한을 받게 되므로 주의가 필요하다.
그밖에도 사용하는 ADC의 noise를 제거하기 위해 다음과 같은 방법을 쓰는 것이 좋다.
voidsetup(){ Serial.begin(115200); radio.begin(); radio.openReadingPipe(1, address); radio.setPALevel(RF24_PA_LOW); radio.startListening(); // Allow allocation of all timers ESP32PWM::allocateTimer(0); ESP32PWM::allocateTimer(1); ESP32PWM::allocateTimer(2); ESP32PWM::allocateTimer(3); servo_x.setPeriodHertz(50); // standard 50 hz servo servo_y.setPeriodHertz(50); servo_x.attach(servo_xPin, 500, 2500); // attaches the servo on pin 25 & 32 to the servo object servo_y.attach(servo_yPin, 500, 2500); // using default min/max of 1000us and 2000us // different servos may require different min/max settings // for an accurate 0 to 180 sweep }
voidloop(){ recvData(); unsignedlong now = millis(); if( now - lastRecvTime > 1000){ //ResetData(); } rotate_xy(); }
//Servo motor library for ESP32 #include<ESP32Servo.h>
Servo servo_x; // create servo object to control a servo Servo servo_y; // 16 servo objects can be created on the ESP32
int angle_x = 90; int angle_y = 90; int i = 0; int center_x = 0; int center_y = 0; int ref_xl, ref_xr, ref_ya, ref_yb; // Servo rotation reference value
// for no interference, each joysticks must be used with other ADC! (* not recommended) // one joystick use ADC1: GPIO 36,39,32,33,34,35 // the other must use ADC2: GPIO 4,0*,2,15,13,12,14,27,25,26 int servo_xPin = 15; int servo_yPin = 2;
voidsetup(){ Serial.begin(115200); radio.begin(); radio.openReadingPipe(1, address); radio.setPALevel(RF24_PA_LOW); radio.startListening(); // Allow allocation of all timers ESP32PWM::allocateTimer(0); ESP32PWM::allocateTimer(1); ESP32PWM::allocateTimer(2); ESP32PWM::allocateTimer(3); servo_x.setPeriodHertz(50); // standard 50 hz servo servo_y.setPeriodHertz(50); servo_x.attach(servo_xPin, 500, 2500); // attaches the servo on pin 25 & 32 to the servo object servo_y.attach(servo_yPin, 500, 2500); // using default min/max of 1000us and 2000us // different servos may require different min/max settings // for an accurate 0 to 180 sweep
// center value centerData(); }
voidloop(){ unsignedlong now = millis(); if( now - lastRecvTime > 5){ // 5ms 마다 서보출력 recvData(); rotate_xy(); } }