อาการนี้น่าจะมาจากแหล่งจ่ายไฟ
สามารถเช็คทีละส่วน แนะนำให้เช็คโดยส่งข้อมูล NRF24L01 อย่างเดียว
เปิดทิ้งไว้ 1 วัน ถ้าส่งได้ปกติแสดงว่ามีจากจุดอื่นครับ
โค๊ดนี้ใช้ได้ปกติ
#include
#include
#include
/*----- Declare all constant pin to be used ----*/
#define CE_PIN 8 //2.4GHz CE_pin of NRF24L01 module
#define CSN_PIN 6 //2.4GHz CSN_pin of NRF24L01 module
const uint64_t pipe = 0xE8E8F0F0E1LL; // This is the transmit pipe to communicate the two module
RF24 radio(CE_PIN, CSN_PIN); // Activate the Radio
int joystick[6]; // 7 element array holding the Joystick readings
int motorspeed = 255;
const int motor1pin1 = 9; // left motors 2
const int motor1pin2 = 10; // left motor 1
const int motor2pin1 = 2; // right motor 1
const int motor2pin2 = 4; // right motor 2
const int EnableA = 3; // enable pin for motor 1
const int EnableB = 5; // enable pin for motor 2
int buttonstatusA;
int buttonstatusB;
int buttonstatusC;
int buttonstatusD;
int buzzer = 7;
int Freq = 800;
void setup() {
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(EnableA, OUTPUT);
pinMode(EnableB, OUTPUT);
//pinMode(A,INPUT_PULLUP);
pinMode(buzzer,OUTPUT);
Serial.begin(9600); /* Opening the Serial Communication */
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
//radio.setPALevel(RF24_PA_MIN);
//radio.setAutoAck(true);
radio.openReadingPipe(1,pipe);
radio.startListening();
//radio.powerUp();
}
void loop() /****** MAIN LOOP: RUNS CONSTANTLY ******/
{
if (radio.available())
{
// radio.powerUp();
radio.read( joystick, sizeof(joystick) );
delay(100);
int X = joystick[0];
int Y = joystick[1];
int A = joystick[2];
int B = joystick[3];
int C = joystick[4];
int D = joystick[5];
if( Y < 300 && X > 0 ) {
digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,HIGH);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,HIGH);
analogWrite(EnableA,motorspeed);
analogWrite(EnableB,motorspeed);
}
else if( Y > 550 && X > 300 ) {
digitalWrite(motor1pin1,HIGH);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,HIGH);
digitalWrite(motor2pin2,LOW);
analogWrite(EnableA,motorspeed);
analogWrite(EnableB,motorspeed);
}
else if( X > 500 && Y > 0 ) {
digitalWrite(motor1pin1,HIGH);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,HIGH);
analogWrite(EnableA,motorspeed);
analogWrite(EnableB,0);
}
else if( X == 0 && Y > 0 ) {
digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,HIGH);
digitalWrite(motor2pin1,HIGH);
digitalWrite(motor2pin2,LOW);
analogWrite(EnableA,0);
analogWrite(EnableB,motorspeed);
}
else {
digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,LOW);
analogWrite(EnableA,0);
analogWrite(EnableB,0);
}
/* if(buttonstatusA == 1){
}
if(buttonstatusA==2){
digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,LOW);
analogWrite(EnableA,0);
analogWrite(EnableB,0);
}
if(A == 0){
buttonstatusA++;
//tone(buzzer,Freq);
}
if(buttonstatusA>2){
buttonstatusA = 1;
//noTone(buzzer);
}*/
Serial.print("X =");
Serial.print(X);
Serial.print(" Y = ");
Serial.print(Y);
Serial.print(" Up = ");
Serial.print(A);
Serial.print(" Right = ");
Serial.print(B);
Serial.print(" Down = ");
Serial.print(C);
Serial.print(" Left = ");
Serial.println(D);
}
}//--(end main loop )---
โค๊ดนี้ รับ NRFได้ปกติ แต่พอจ่าย ไฟ มัน กลับ ให้ค่า X Y เป้น 0 หมดเลย
//-----( Import needed libraries )-----
#include
#include "nRF24L01.h"
#include
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 8
#define CSN_PIN 6
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
#define ENA_m1 3 // Enable/speed motor Right
#define ENB_m1 5 // Enable/speed motor Left
#define IN_11 9 // L298N #1 in 1 motor Right
#define IN_12 10 // L298N #1 in 2 motor Right
#define IN_13 2 // L298N #1 in 3 motor Left
#define IN_14 4 // L298N #1 in 4 motor Left
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[6]; // 6 element array holding Joystick readings
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup()
{
pinMode(ENA_m1, OUTPUT);
pinMode(ENB_m1, OUTPUT);
pinMode(IN_11, OUTPUT);
pinMode(IN_12, OUTPUT);
pinMode(IN_13, OUTPUT);
pinMode(IN_14, OUTPUT);
Serial.begin(9600);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop()
{
if (radio.available())
{
radio.read( joystick, sizeof(joystick) );
int xAxis = joystick[0];
int yAxis = joystick[1];
// the four button variables from joystick array
int buttonUp = joystick[2];
int buttonRight = joystick[3];
int buttonDown = joystick[4];
int buttonLeft = joystick[5];
// Y-axis used for forward and backward control
if (yAxis < 470) {
digitalWrite(IN_11, HIGH);
digitalWrite(IN_12, LOW);
digitalWrite(IN_13, HIGH);
digitalWrite(IN_14, LOW);
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
digitalWrite(IN_11, LOW);
digitalWrite(IN_12, HIGH);
digitalWrite(IN_13, LOW);
digitalWrite(IN_14, HIGH);
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (xAxis < 470) {
int xMapped = map(xAxis, 470, 0, 0, 255);
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
int xMapped = map(xAxis, 550, 1023, 0, 255);
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(ENA_m1, motorSpeedB); // Send PWM signal to motor A
analogWrite(ENB_m1, motorSpeedB); // Send PWM signal to motor B
Serial.print("X = ");
Serial.print(xAxis);
Serial.print(" Y = ");
Serial.print(yAxis);
Serial.print(" Up = ");
Serial.print(joystick[2]);
Serial.print(" Right = ");
Serial.print(joystick[3]);
Serial.print(" Down = ");
Serial.print(joystick[4]);
Serial.print(" Left = ");
Serial.println(joystick[5]);
}
}
ตรงนี้ต้องเช็คที่หน้างาน แยกเช็คทีละโมดูล ลองโค้ดง่าย ๆ ก่อน อีกจุดเช็คที่แหล่งจ่ายไฟ nrf24L01 ครับ