Erreur Arduino

Fermé
maxpothier Messages postés 37 Date d'inscription samedi 21 juillet 2018 Statut Membre Dernière intervention 28 décembre 2019 - Modifié le 21 oct. 2018 à 11:22
[Dal] Messages postés 6203 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 29 janvier 2025 - 23 oct. 2018 à 10:19
Bonjour,
je voudrais faire un programme, avec mon Arduino Mega 2560, qui allume une LED lorsqu'on fait le bon code sur un DIP à 10 interrupteurs. Voici le programme:
void setup(){
  pinMode(22, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);
  pinMode(24, INPUT_PULLUP);
  pinMode(25, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);
  pinMode(27, INPUT_PULLUP);
  pinMode(28, INPUT_PULLUP);
  pinMode(29, INPUT_PULLUP);
  pinMode(30, INPUT_PULLUP);
  pinMode(31, INPUT_PULLUP);
  pinMode(32, OUTPUT);
}
int code[] = {0, 0, 1, 1, 0, 1, 0, 1, 1, 0};
int pins[10] = {};
void loop(){
  pins = {};
  for (int x = 0; x++; x<11){
    if (digitalRead(x + 21) = 1){
      pins[x] = 0;
    }else{
      pins[x] = 1;
    }
  }
  if (code == pins){
    digitalWrite(32, HIGH);
  }else{
    digitalWrite(33, HIGH);
  }
}

Mais j'obtiens l'erreur suivante:
sketch_oct20a.ino: In function ‘void loop()’:
sketch_oct20a.ino:17:8: error: assigning to an array from an initializer list
sketch_oct20a.ino:19:29: error: lvalue required as left operand of assignment

Merci d'avance...

4 réponses

NHenry Messages postés 15194 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 23 février 2025 353
21 oct. 2018 à 13:10
Pour l'erreur d'assignement :
digitalRead(x + 21) = 1
Il faut doubler le = pour faire un test d'égalité.

Et pour l'autre erreur, je pense que tu peux retirer la ligne :
pins = {};
0
[Dal] Messages postés 6203 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 29 janvier 2025 1 099
22 oct. 2018 à 12:13
Et pour l'autre erreur, je pense que tu peux retirer la ligne :
pins = {};


Sauf si ce qu'il veut est de remettre à zéro le tableau
int pins[10];
à chaque itération de la boucle...

si tel est le cas, il peut le faire ainsi :

memset(pins, 0, sizeof pins);
0
NHenry Messages postés 15194 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 23 février 2025 353
22 oct. 2018 à 18:57
Comme la boucle suivante redéfinit la valeur de chacun des éléments du tableau, je ne pense pas que ça soit utile.

Et en plus, je n'avais pas vu, mais :
if (code == pins)
renverra toujours faux, car cela compare les pointeurs et pas le contenu des tableaux.
0
maxpothier Messages postés 37 Date d'inscription samedi 21 juillet 2018 Statut Membre Dernière intervention 28 décembre 2019 1
Modifié le 22 oct. 2018 à 20:01
Merci beaucoup,
Le programme ne me fait plus d'erreurs. Le voici:
void setup(){
  pinMode(22, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);
  pinMode(24, INPUT_PULLUP);
  pinMode(25, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);
  pinMode(27, INPUT_PULLUP);
  pinMode(28, INPUT_PULLUP);
  pinMode(29, INPUT_PULLUP);
  pinMode(30, INPUT_PULLUP);
  pinMode(31, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}
int code[] = {0, 0, 1, 1, 0, 1, 0, 1, 1, 0};
int pins[10] = {};
void loop(){
  delay(15);
  for (int x = 0; x++; x<11){
    if (digitalRead(x + 21) == 1){
      pins[x] = 0;
    }else{
      pins[x] = 1;
    }
  }
  int codeBon = 1;
  for (int x = 0; x++; x<11){
    if (code[x] != pins[x]){
      codeBon = 0;
    }
  }
  if (codeBon == 1){
    digitalWrite(13, HIGH);
  }else{
    digitalWrite(13, LOW);
  }
}

mais j'ai beau saisir ou non le code sur le DIP, la led incorporée reste allumée... Pour comprendre le problème, j'ai fait un petit ajout de deux lignes:
void setup(){
  pinMode(22, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP);
  pinMode(24, INPUT_PULLUP);
  pinMode(25, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);
  pinMode(27, INPUT_PULLUP);
  pinMode(28, INPUT_PULLUP);
  pinMode(29, INPUT_PULLUP);
  pinMode(30, INPUT_PULLUP);
  pinMode(31, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  Serial.begin(9600);//                                                    Là
}
int code[] = {0, 0, 1, 1, 0, 1, 0, 1, 1, 0};
int pins[10] = {};
void loop(){
  delay(15);
  for (int x = 0; x++; x<11){
    if (digitalRead(x + 21) == 1){
      pins[x] = 0;
    }else{
      pins[x] = 1;
    }
  }
  Serial.println(pins);//                                                           et Là
  int codeBon = 1;
  for (int x = 0; x++; x<11){
    if (code[x] != pins[x]){
      codeBon = 0;
    }
  }
  if (codeBon == 1){
    digitalWrite(13, HIGH);
  }else{
    digitalWrite(13, LOW);
  }
}

mais l'erreur des mille et une lignes s'affiche:
sketch_oct20a.ino: In function ‘void loop()’:
sketch_oct20a.ino:26:22: error: no matching function for call to ‘println(int [10])’
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_oct20a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:70:12: note: candidate: size_t Print::println(char) <near match>
size_t println(char);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:70:12: note: conversion of argument 1 would be ill-formed:
sketch_oct20a.ino:26:22: error: invalid conversion from ‘int*’ to ‘char’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_oct20a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:71:12: note: candidate: size_t Print::println(unsigned char, int) <near match>
size_t println(unsigned char, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:71:12: note: conversion of argument 1 would be ill-formed:
sketch_oct20a.ino:26:22: error: invalid conversion from ‘int*’ to ‘unsigned char’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_oct20a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:72:12: note: candidate: size_t Print::println(int, int) <near match>
size_t println(int, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:72:12: note: conversion of argument 1 would be ill-formed:
sketch_oct20a.ino:26:22: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_oct20a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:73:12: note: candidate: size_t Print::println(unsigned int, int) <near match>
size_t println(unsigned int, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:73:12: note: conversion of argument 1 would be ill-formed:
sketch_oct20a.ino:26:22: error: invalid conversion from ‘int*’ to ‘unsigned int’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_oct20a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:74:12: note: candidate: size_t Print::println(long int, int) <near match>
size_t println(long, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:74:12: note: conversion of argument 1 would be ill-formed:
sketch_oct20a.ino:26:22: error: invalid conversion from ‘int*’ to ‘long int’ [-fpermissive]
In file included from /usr/share/arduino/hardware/arduino/cores/arduino/Stream.h:26:0,
from /usr/share/arduino/hardware/arduino/cores/arduino/HardwareSerial.h:28,
from /usr/share/arduino/hardware/arduino/cores/arduino/Arduino.h:193,
from sketch_oct20a.ino:1:
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:75:12: note: candidate: size_t Print::println(long unsigned int, int) <near match>
size_t println(unsigned long, int = DEC);
^
/usr/share/arduino/hardware/arduino/cores/arduino/Print.h:75:12: note: conversion of argument 1 would be ill-formed:
sketch_oct20a.ino:26:22: error: invalid conversion from ‘int*’ to ‘long unsigned int’ [-fpermissive]
0
NHenry Messages postés 15194 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 23 février 2025 353
22 oct. 2018 à 21:41
Le message est clair :
error: no matching function for call to ‘println(int [10])’
println n'accepte pas un int[] en paramètre.
0
maxpothier Messages postés 37 Date d'inscription samedi 21 juillet 2018 Statut Membre Dernière intervention 28 décembre 2019 1
22 oct. 2018 à 20:02
rien que de lire les 3 premières lignes de l'erreur, j'ai mal à la tête...
0
[Dal] Messages postés 6203 Date d'inscription mercredi 15 septembre 2004 Statut Contributeur Dernière intervention 29 janvier 2025 1 099
23 oct. 2018 à 10:19
Tu ne peux pas passer un tableau d'entiers à println()

https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

Sinon, les arguments de ta boucle for inversent le test de sortie et l'incrémentation

au lieu de :
for (int x = 0; x++; x<11){
essaye
for (int x = 0; x<11; x++){
...


Dal
0