I cant fix my code for my joystick

i made this code and it kept giviing me this : /usr/local/bin/arduino-cli compile --fqbn arduino:avr:uno --build-cache-path /tmp --output-dir /tmp/1358584767/build --build-path /tmp/arduino-build-2B663BB633FACCC1D1731B2FDFC04063 /tmp/1358584767/new_sketch_1726864081254

/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino: In function 'void loop()':
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:16:14: error: a function-definition is not allowed here before '{' token
void setup() {}
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino: In function 'void loop()':
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:21:6: error: redefinition of 'void loop()'
void loop() {
^~~~
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:9:6: note: 'void loop()' previously defined here
void loop() {
^~~~
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:23:5: error: 'modo' was not declared in this scope
modo = Serial.read(); //Lê o caractere e guarda na variável sentido
^~~~
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:23:5: note: suggested alternative: 'modf'
modo = Serial.read(); //Lê o caractere e guarda na variável sentido
^~~~
modf
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:28:11: error: 'modo' was not declared in this scope
switch (modo) {
^~~~
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:28:11: note: suggested alternative: 'modf'
switch (modo) {
^~~~
modf
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino: In function 'void leitura()':
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:42:31: error: 'x' was not declared in this scope
Serial.println(analogRead(x));
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:44:31: error: 'y' was not declared in this scope
Serial.println(analogRead(y));
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:46:32: error: 'botao' was not declared in this scope
Serial.println(digitalRead(botao));
^~~~~
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino: In function 'void movimento()':
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:54:21: error: 'x' was not declared in this scope
if ((analogRead(x)) == 0) {
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:57:21: error: 'x' was not declared in this scope
if ((analogRead(x)) == 1023) {
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:60:21: error: 'y' was not declared in this scope
if ((analogRead(y)) == 0) {
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:63:21: error: 'y' was not declared in this scope
if ((analogRead(y)) == 1023) {
^
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:66:22: error: 'botao' was not declared in this scope
if ((digitalRead(botao)) == 0) {
^~~~~
/tmp/1358584767/new_sketch_1726864081254/new_sketch_1726864081254.ino:69:22: error: expected primary-expression before 'int'
if ((digitalRead(int:botao)) == 1) {
^~~

Error during build: exit status 1

please help

also it kept giving me this error if i ACTUALLY USED 'int'

expected primary-expression before 'int'
if ((digitalRead(int:botao)) == 1) {

You have to show us the source code enclosed in code tags.

1 Like

Post the code.

That kind of error could be as simple as a forgotten semicolon somewhere near the line(s) it complains about.

Ask me how I know.

a7

1 Like

I believe you have copy/pasted a program INSIDE the minimal.ino sketch:

void setup() {
  // something
  }
void loop() {
    // other
}

You have something that looks like this:

void setup() {
  // part of minimal.ino
}

void loop() {
  // part of minimal.ino

  void setup() {
    // redefinition - copy/pasted program
  }
  void loop() {
    // redefinition - copy/pasted program
  }
}

Which gives you this type of error..

sketch.ino: In function 'void loop()':
sketch.ino:6:16: error: a function-definition is not allowed here before '{' token
   void setup() {
                ^
sketch.ino:9:15: error: a function-definition is not allowed here before '{' token
   void loop() {
               ^

Error during build: exit status 1

The program looks like an etch-a-sketch with a joystick and MAX7219 dot-matrix, using Serial.read(); to draw/erase (modo).

void setup() {

}

int joyX = A0; // pino analógico do eixo X
int joyY = A1; // pino analógico do eixo Y
int joyButton = 2; // pino digital do botão de pressão

void setup()
int Serial.begin(9600); // inicia comunicação serial
pinMode[(joyX, INPUT); // entrada leitura eixo X joystick
pinMode[(joyY, INPUT); // entrada leitura eixo y joystick
pinMode[(joyButton, INPUT_PULLUP) // entrada resistor interno pull up pusch button

void loop() {
int x = analogRead(joyX); // faz a leitura do eixo x (0 a 1023) - analógico
int y = analogRead(joyY); // faz a leitura do eixo y (0 a 1023) - analógico
int button = digitalRead(joyButton); // faz a leitura do eixo z (0 ou 1) - digital
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Button: ");
Serial.println(button);
delay(100);
}

this is my code

i use an arduino uno.

strange... whenever i use 'int:' it gets blue

guys im anoob

Remove all occurrences of "["

Add an opening brace "{" and close brace "}" around the code in the setup() function...

Remove int before Serial.begin();

Add semicolon ";" at the end of pinMode(joyButton, INPUT_PULLUP);

void setup() 
{ // OPEN BRACE 
  // int Serial.begin(9600); // inicia comunicação serial // REMOVE INT
  Serial.begin(9600); // inicia comunicação serial
  pinMode(joyX, INPUT); // entrada leitura eixo X joystick
  pinMode(joyY, INPUT); // entrada leitura eixo y joystick
  // pinMode(joyButton, INPUT_PULLUP) // entrada resistor interno pull up pusch button
  pinMode(joyButton, INPUT_PULLUP); // ADD ";" SEMICOLON
} // CLOSE BRACE

ok lemme try it now

/usr/local/bin/arduino-cli compile --fqbn arduino:avr:uno --build-cache-path /tmp --output-dir /tmp/3903922206/build --build-path /tmp/arduino-build-2B663BB633FACCC1D1731B2FDFC04063 /tmp/3903922206/new_sketch_1726864081254

/tmp/3903922206/new_sketch_1726864081254/new_sketch_1726864081254.ino:15:4: error: expected initializer before 'Serial'
Serial.begin(9600); // inicia comunicação serial
^~~~~~
/tmp/3903922206/new_sketch_1726864081254/new_sketch_1726864081254.ino:16:11: error: expected constructor, destructor, or type conversion before '(' token
pinMode(joyX, INPUT); // entrada leitura eixo X joystick
^
/tmp/3903922206/new_sketch_1726864081254/new_sketch_1726864081254.ino:17:11: error: expected constructor, destructor, or type conversion before '(' token
pinMode(joyY, INPUT); // entrada leitura eixo y joystick
^
/tmp/3903922206/new_sketch_1726864081254/new_sketch_1726864081254.ino:18:11: error: expected constructor, destructor, or type conversion before '(' token
pinMode(joyButton, INPUT_PULLUP); // entrada resistor interno pull up pusch button
^

Error during build: exit status 1

You did not make all the changes.

Remember to post your new code when you change the old code. When posting code, click the < CODE > button and paste your code where you see ```type or paste code here```

okay

int joyX = A0; // pino analógico do eixo X
int joyY = A1; // pino analógico do eixo Y
int joyButton = 2; // pino digital do botão de pressão

void {setup()}
 Serial.begin(9600); // inicia comunicação serial
pinMode{(joyX, INPUT)}; // entrada leitura eixo X joystick
pinMode{(joyY, INPUT)}; // entrada leitura eixo y joystick
pinMode(joyButton, INPUT_PULLUP) // entrada resistor interno pull up pusch button

void {loop()}
int x = analogRead{(joyX)}; // faz a leitura do eixo x (0 a 1023) - analógico
int y = analogRead{(joyY)}; // faz a leitura do eixo y (0 a 1023) - analógico
int button = digitalRead(joyButton); // faz a leitura do eixo z (0 ou 1) - digital
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Button: ");
Serial.println(button);
delay{(100)};

just modified it

int joyX = A0; // pino analógico do eixo X
int joyY = A1; // pino analógico do eixo Y
int joyButton = 2; // pino digital do botão de pressão

void {setup()}
 Serial.begin(9600); // inicia comunicação serial
pinMode(joyX, INPUT); // entrada leitura eixo X joystick
pinMode(joyY, INPUT); // entrada leitura eixo y joystick
pinMode(joyButton, INPUT_PULLUP) // entrada resistor interno pull up pusch button

void {loop()}
int x = analogRead(joyX); // faz a leitura do eixo x (0 a 1023) - analógico
int y = analogRead(joyY); // faz a leitura do eixo y (0 a 1023) - analógico
int button = digitalRead(joyButton); // faz a leitura do eixo z (0 ou 1) - digital
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Button: ");
Serial.println(button);
delay(100);

once again modified :

    
int joyX = A0; // pino analógico do eixo X
int joyY = A1; // pino analógico do eixo Y
int joyButton = 2; // pino digital do botão de pressão

void {setup()}
 Serial.begin(9600); // inicia comunicação serial
pinMode(joyX, INPUT); // entrada leitura eixo X joystick
pinMode(joyY, INPUT); // entrada leitura eixo y joystick
pinMode(joyButton, INPUT_PULLUP);// entrada resistor interno pull up pusch button//added the semicolon

void {loop()}
int x = analogRead(joyX); // faz a leitura do eixo x (0 a 1023) - analógico
int y = analogRead(joyY); // faz a leitura do eixo y (0 a 1023) - analógico
int button = digitalRead(joyButton); // faz a leitura do eixo z (0 ou 1) - digital
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Button: ");
Serial.println(button);
delay(100);

int joyX = A0; // pino analógico do eixo X
int joyY = A1; // pino analógico do eixo Y
int joyButton = 2; // pino digital do botão de pressão

{void setup()}
 Serial.begin(9600); // inicia comunicação serial
pinMode(joyX, INPUT); // entrada leitura eixo X joystick
pinMode(joyY, INPUT); // entrada leitura eixo y joystick
pinMode(joyButton, INPUT_PULLUP);// entrada resistor interno pull up pusch button

{void loop()}
int x = analogRead(joyX); // faz a leitura do eixo x (0 a 1023) - analógico
int y = analogRead(joyY); // faz a leitura do eixo y (0 a 1023) - analógico
int button = digitalRead(joyButton); // faz a leitura do eixo z (0 ou 1) - digital
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Button: ");
Serial.println(button);
delay(100);

again modified