Can anyone help me fix this error?
#include <PS2X_lib.h>
#define PS2_DAT 13
#define PS2_CMD 11
#define PS2_SEL 10
#define PS2_CLK 12
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
int A;
//Chân điều khiển trên mạch
#define pwm2 2
#define pwm3 3
#define pwm4 4
#define pwm5 5
#define pwm6 6
#define pwm7 7
#define pwm8 8
#define pwm9 9
void setup() {
Serial.begin(57600);
delay(300);
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
}
void loop()
{
chuong_trinhPS2()
if (A == 1) {
chuong_trinh_tien();
}
if (A == 2) {
chuong_trinh_lui();
}
if (A == 3) {
chuong_trinh_re_phai();
}
if (A == 4) {
chuong_trinh_re_trai();
}
if (A == 0) {
reset();
}
void chuong_trinh_taycam()
{
ps2x.read_gamepad(false, vibrate);
// TIến
if (ps2x.Button(PSB_PAD_UP)
{ A = 1; Serial.print("tiến": );
}
// Lùi
else if (ps2x.Button(PSB_PAD_DOWN)
{ A = 2; Serial.print("lùi": );
}
else if (ps2x.Button(PSB_PAD_RIGHT)
{ A = 3; Serial.print("phải": );
}
else if (ps2x.Button(PSB_PAD_LEFT)
{ A = 4; Serial, print("trái": );
}
delay (100);
}
void chuong_trinh_tien()
{
analogWrite(pwm2, 0); analogWrite(pwm4, 255);
analogWrite(pwm6, 255); analogWrite(pwm8, 0);
analogWrite(pwm3, 255); analogWrite(pwm5, 0);
analogWrite(pwm7, 0); analogWrite(pwm9, 255);
}
void chuong_trinh_lui()
analogWrite(pwm2, 255); analogWrite(pwm4, 0);
analogWrite(pwm6, 0); analogWrite(pwm8, 255);
analogWrite(pwm3, 0); analogWrite(pwm5, 255);
analogWrite(pwm7, 255); analogWrite(pwm9, 0);
void reset()
{
analogWrite(pwm2, 0); analogWrite(pwm4, 0);
analogWrite(pwm6, 0); analogWrite(pwm8, 0);
analogWrite(pwm3, 0); analogWrite(pwm5, 0);
analogWrite(pwm7, 0); analogWrite(pwm9, 0);
}
delay(50);
}`Use code tags to format code for the forum`
Welcome
Please edit your post, select all code and click the <CODE> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and the forum software will display it correctly.
If you haven't done so yet, please use tools → autoformat to properly indent the code.
After that, you will find this in your code
void loop()
{
chuong_trinhPS2()
if (A == 1) {
chuong_trinh_tien();
}
if (A == 2) {
chuong_trinh_lui();
}
if (A == 3) {
chuong_trinh_re_phai();
}
if (A == 4) {
chuong_trinh_re_trai();
}
if (A == 0) {
reset();
}
void chuong_trinh_taycam()
The fact that void chuong_trinh_taycam() is indented means that you defined this function inside loop().
Add a } so that last part looks like
if (A == 0) {
reset();
}
}
void chuong_trinh_taycam()
After that and a new autoformat, the end of your code looks like
void reset()
{
analogWrite(pwm2, 0); analogWrite(pwm4, 0);
analogWrite(pwm6, 0); analogWrite(pwm8, 0);
analogWrite(pwm3, 0); analogWrite(pwm5, 0);
analogWrite(pwm7, 0); analogWrite(pwm9, 0);
}
delay(50);
}
Those last two lines will result in an error. I suspect that the delay(50) is supposed to be at the end of loop() so move it there and remove the last }.
PS
I do not have the library that you use so I can't compile to check if those are all the problems.
I can send you the library file, can you help me? I did as you said but the same error still occurs, maybe it comes from the void loop part but I can't fix it.
This is the PS2x folder, can you help me check for bugs?
Tôi đã sửa mã của bạn, nhưng quá nhiều lỗi để mô tả. Hiển thị liên kết đến mã gốc.
I fixed your code, but too many errors to describe. Show a link to the original code.
#include <PS2X_lib.h>
#define PS2_DAT 13
#define PS2_CMD 11
#define PS2_SEL 10
#define PS2_CLK 12
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
int A;
//Chân điều khiển trên mạch
#define pwm2 2
#define pwm3 3
#define pwm4 4
#define pwm5 5
#define pwm6 6
#define pwm7 7
#define pwm8 8
#define pwm9 9
void setup() {
Serial.begin(57600);
delay(300);
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
}
void loop() {
chuong_trinhPS2();
}
void chuong_trinhPS2() {
if (A == 1) {
chuong_trinh_tien();
}
if (A == 2) {
chuong_trinh_lui();
}
if (A == 3) {
chuong_trinh_re_phai();
}
if (A == 4) {
chuong_trinh_re_trai();
}
if (A == 0) {
reset();
}
}
void chuong_trinh_taycam() {
if (ps2x.read_gamepad(false, vibrate)) {
// TIến
if (ps2x.Button(PSB_PAD_UP)) {
A = 1;
Serial.print("tiến:");
}
// Lùi
else if (ps2x.Button(PSB_PAD_DOWN)) {
A = 2;
Serial.print("lùi:");
}
else if (ps2x.Button(PSB_PAD_RIGHT)) {
A = 3;
Serial.print("phải:");
}
else if (ps2x.Button(PSB_PAD_LEFT)) {
A = 4;
Serial.print("trái:");
}
delay (100);
}
}
void chuong_trinh_tien() {
analogWrite(pwm2, 0); analogWrite(pwm4, 255);
analogWrite(pwm6, 255); analogWrite(pwm8, 0);
analogWrite(pwm3, 255); analogWrite(pwm5, 0);
analogWrite(pwm7, 0); analogWrite(pwm9, 255);
}
void chuong_trinh_lui() {
analogWrite(pwm2, 255); analogWrite(pwm4, 0);
analogWrite(pwm6, 0); analogWrite(pwm8, 255);
analogWrite(pwm3, 0); analogWrite(pwm5, 255);
analogWrite(pwm7, 255); analogWrite(pwm9, 0);
}
void chuong_trinh_re_trai() {
Serial.println("chuong_trinh_re_trai - Điều này còn thiếu");
}
void chuong_trinh_re_phai() {
Serial.println("chuong_trinh_re_phai - Điều này còn thiếu");
}
void reset() {
analogWrite(pwm2, 0); analogWrite(pwm4, 0);
analogWrite(pwm6, 0); analogWrite(pwm8, 0);
analogWrite(pwm3, 0); analogWrite(pwm5, 0);
analogWrite(pwm7, 0); analogWrite(pwm9, 0);
delay(50);
}
Bạn có thể chỉ tôi một số lỗi đó được không ?
Xin lỗi. Quá nhiều lỗi nhỏ, tôi mất dấu. Mã trong Post # 6 có tất cả các chỉnh sửa. So sánh điều này với mã của bạn trong Post # 1. Nếu bạn có câu hỏi cụ thể, xin vui lòng hỏi.
Sorry. So many small errors, I lost track. The code in Post #6 has all the edits. Compare this to your code in Post #1. If you have specific questions, please feel free to ask.
People, please keep it in English.
Please edit your posts and at least add the English translation.
Sorry. Added xlat.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.