Programme simple qui fonctionne pas HELP ME PLEASE

Bonjour mes ami(e)s Arduinoiste !
J'essaye de faire une "lampe" avec une DEL qui s'allume seulement quand la température descend au dessous d'un certain seuil, et que dont on peut régler l'intensité avec un petit potentiomètre de 10Kohms.
Le problème c'est que mon programme ne fonctionne pas, en effet, je l'examine depuis tout a l'heure et j'ai l'impression que ca va ... Mais forcement que non !
Alors je fait appel a vous ? HELP ME PLEASE !!!! :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

const int onOffSwitchPin = 7;
const int LED = 6; //sortit PWM
const int tempPin = A4;
const int potPin = A0;
int potVal;
float limitTemp = 30.00;
int onOffSwitchState = 0;
int LEDOn = 0;
int temperature;

void setup() {
pinMode(onOffSwitchPin,INPUT);
pinMode(LED,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
potVal = analogRead(potPin)/4;
int sensorVal = analogRead(tempPin);
float voltage = (sensorVal/1024.0)*5.0;
float temperature = (voltage-0.5)*100;
if(onOffSwitchState == HIGH){
nivRes == 1;
}
else{
onOffSwitchState == 0;
}
if(onOffSwitchState == 1){
if(temperature <= limitTemp){
LEDOn == 1;
}
}
else{
LEDOn == 0;
}

if(LEDOn == 1){
analogWrite(LED, potVal);
}
else{
analogWrite(LED, LOW);
}
// put your main code here, to run repeatedly:

}

pardon je me suis tromper de nom j'ai mis "nivRes" au lieu de "onOffSwitchState",
voilà le code bon mais qui ne fonctionne toujours pas !

const int onOffSwitchPin = 7;
const int LED = 6; //sortit PWM
const int tempPin = A4;
const int potPin = A0;
int potVal;
float limitTemp = 30.00;
int onOffSwitchState = 0;
int LEDOn = 0;
int temperature;

void setup() {
pinMode(onOffSwitchPin,INPUT);
pinMode(LED,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
potVal = analogRead(potPin)/4;
int sensorVal = analogRead(tempPin);
float voltage = (sensorVal/1024.0)*5.0;
float temperature = (voltage-0.5)*100;
if(onOffSwitchState == HIGH){
onOffSwitchState == 1;
}
else{
onOffSwitchState == 0;
}
if(onOffSwitchState== 1){
if(temperature <= limitTemp){
LEDOn == 1;
}
}
else{
LEDOn == 0;
}

if(LEDOn == 1){
analogWrite(LED, potVal);
}
else{
analogWrite(LED, LOW);
}
// put your main code here, to run repeatedly:

}

Salut,
Je ne suis pas sur que cela puisse fonctionner avec tout les if en cascade, il est préférable de faire :

if (......)
{
...
}
else if (.....)
{
..
}
else if (.....)
{
....
}
else if.....
et pour terminer
else
{
....
}

Voila

Merci beaucoup de cette réponse si rapide !
Mais il faut que les deux conditions soit réunis donc je suis obligé d'apposer ces différentes conditions en cascade, ya t'il un autre moyen ?
Ca ne fonctionne toujours sa m'affiche même plein d'erreur, je doit me tromper quelque part...

hello

j'ai modifié ton prg pout qu'il compile et se lance.

je ne sais pas s'il fait ce que tu voulais

autrement pour les deux conditions il y a :

[codeif((onOffSwitchState== 1)&&(temperature <= limitTemp)){
LEDOn == 1;
}
else{
LEDOn == 0;
}][/code]

voici ton code d'origine"corrigé"
lances le avec le moniteur en 115200 bauds

const int onOffSwitchPin = 7;
      int onOffSwitchState = 0;
const int LED = 6; //sortit PWM
const int tempPin = A4;
const int potPin = A0;
int potVal;
float limitTemp = 30.00;
//int onOffSwitchState = 0;
int LEDOn = 0;
int temperature;

void setup() {Serial.begin(115200);
pinMode(tempPin,INPUT);
  pinMode(potPin,INPUT);
   pinMode(onOffSwitchPin,INPUT);
   pinMode(LED,OUTPUT);
  // put your setup code here, to run once:

}

void loop() {
  potVal = analogRead(potPin)/4;
 int sensorVal = analogRead(tempPin);
 float voltage = (sensorVal/1024.0)*5.0;
 float temperature = (voltage-0.5)*100;
  if(digitalRead(onOffSwitchPin)){
       onOffSwitchState = 1;
       Serial.print(F(" onOffSwitchState ="));Serial.println( onOffSwitchState );
  }
  
  else{
    onOffSwitchState = 0;
    Serial.print(F(" onOffSwitchState ="));Serial.println( onOffSwitchState );
  }

delay(3000);
  
   if(onOffSwitchState== 1){Serial.print(F("  if..........onOffSwitchState ="));Serial.println( onOffSwitchState );
   if(temperature <= limitTemp){Serial.print(F(" if.......temperature <= limitTemp ="));Serial.println( temperature );
      LEDOn = 1;Serial.print(F(" LEDOn ="));Serial.println( LEDOn );
      }
     }
     else{
      LEDOn = 0;Serial.print(F(" LEDOn ="));Serial.println( LEDOn );
     }
 
     if(LEDOn == 1){Serial.print(F(" if..........LEDOn =  1  "));Serial.println( LEDOn );
      analogWrite(LED, potVal);Serial.print(F(" LED =    "));Serial.println(potVal );
     }
     else{
      analogWrite(LED, LOW);Serial.print(F(" LED =    "));Serial.println(LOW );
     }
   

}

C'est bon ca marche merci beaucoup vraiment ! C'est vraiment que vous m'ayez aider !
Mais j'ai juste une question : Le moniteur série est-il obligatoire ?
Merci encore!

ben oui pour le debugage..

content pour toi

tu ne lisais pas l'inter on/off avec digitalRead
tu mélangeais les "=" et les "==".

les "Serial.print" sont le moyen pour toi de suivre ce qui se passe lorsque ton prg se déroule.
cela te permet d'afficher la valeur d'une variable, ou de voir si le prg entre dans un "if", etc...

une fois débugé, tu mets des " // "devant ces "Serial.print", pour faire qu'ils soient considérés comme des commentaires. donc plus compilés, donc prg moins lourd en mémoire

Ah ok, merci beaucoup pour l'aide et pour l'explication !
C'est vraiment cool qui reste des gens comme cela de nos jour, ca se fait rare maintenant !

JP33:
C'est vraiment cool qui reste des gens comme cela de nos jour, ca se fait rare maintenant !

il y en a plein sur le forum et on compte sur vous pour faire pareil quand vous serez devenu fort !

je plussoye avec JML, ce forum d'entre aide est très actif :slight_smile:

Rebonjours mes sauveurs ! :slight_smile:
J'aimerais améliorer mon programme en ajoutant une photorésistance dans mon circuits.
J'aimerais que quand la luminosité augmentent le PWM qui contrôle la luminosité de ma DEL augmentent avec, j'ai essayer comme ca mais ca marche pas...
Pouvez vous m'aidé encore une fois ?
Merci par avance ! :slight_smile: :slight_smile: :slight_smile:

const int onOffSwitchPin = 7;
int onOffSwitchState = 0;
const int LED = 6; //sortit PWM
const int tempPin = A4;
const int potPin = A0;
const int luminositeSensorPin = A1;
int potVal;
float limitTemp = 30.00;
//int onOffSwitchState = 0;
int LEDOn = 0;
int temperature;
int luminositeVal = 0;
int potVal2;

void setup() {Serial.begin(115200);
pinMode(tempPin,INPUT);
pinMode(potPin,INPUT);
pinMode(onOffSwitchPin,INPUT);
pinMode(LED,OUTPUT);
// put your setup code here, to run once:

}

void loop() {
potVal = analogRead(potPin)/4;
int sensorVal = analogRead(tempPin);
float voltage = (sensorVal/1024.0)*5.0;
float temperature = (voltage-0.5)*100;
potVal2 = analogRead(potPin)/16;
luminositeVal = analogRead(luminositeSensorPin)/16; Serial.print(F("Valeur sensor : "));Serial.println(luminositeVal);
if(digitalRead(onOffSwitchPin)){
onOffSwitchState = 1;
Serial.print(F(" onOffSwitchState ="));Serial.println( onOffSwitchState );
}

else{
onOffSwitchState = 0;
Serial.print(F(" onOffSwitchState ="));Serial.println( onOffSwitchState );
}

delay(3000);

if(onOffSwitchState== 1){Serial.print(F(" if..........onOffSwitchState ="));Serial.println( onOffSwitchState );
if(temperature <= limitTemp){Serial.print(F(" if.......temperature <= limitTemp ="));Serial.println( temperature );
LEDOn = 1;Serial.print(F(" LEDOn ="));Serial.println( LEDOn );
}
}
else{
LEDOn = 0;Serial.print(F(" LEDOn ="));Serial.println( LEDOn );
}

if(LEDOn == 1){Serial.print(F(" if..........LEDOn = 1 "));Serial.println( LEDOn );
analogWrite(LED, potVal);Serial.print(F(" LED = "));Serial.println(potVal );
}
else{
analogWrite(LED, LOW);Serial.print(F(" LED = "));Serial.println(LOW );
}
if(luminositeVal < potVal){
potVal2 +1; Serial.print(F("Valeur PotVal2 "));Serial.println(potVal2 );
}
else if(luminositeVal > potVal){
potVal2 -1;Serial.print(F("Valeur PotVal2 "));Serial.println(potVal2 );
}
else if(luminositeVal == potVal){
potVal2 +0;Serial.print(F("Valeur PotVal2 "));Serial.println(potVal2 );
}

}

les "sauveurs" apprécieraient que vous utilisassiez les tags de code :slight_smile:

corrigez votre post ci dessus et rajoutez les code tags autour du code:
[code]`` [color=blue]// votre code ici[/color] ``[/code].

ça doit ressembler à cela:// votre code ici
(faites aussi ctrl-T (PC) or cmd-T (Mac) dans l'IDE avant de copier le code pour qu'il soit indenté correctement)


luminositeVal = analogRead(luminositeSensorPin)/16; c'est 1023 / 4 qui donne un nombre entre 0 et 255 (à la louche) pas en divisant par 16

Rebonjours mes sauveurs ! :slight_smile:
J'aimerais améliorer mon programme en ajoutant une photorésistance dans mon circuits.
J'aimerais que quand la luminosité augmentent le PWM qui contrôle la luminosité de ma DEL augmentent avec, j'ai essayer comme ca mais ca marche pas...
Pouvez vous m'aidé encore une fois ?
Merci par avance ! :slight_smile: :slight_smile: :slight_smile:

const int onOffSwitchPin = 7;
int onOffSwitchState = 0;
const int LED = 6; //sortit PWM
const int tempPin = A4;
const int potPin = A0;
const int luminositeSensorPin = A1;
int potVal;
float limitTemp = 30.00;
//int onOffSwitchState = 0;
int LEDOn = 0;
int temperature;
int luminositeVal = 0;
int potVal2;

void setup() {
  Serial.begin(115200);
  pinMode(tempPin, INPUT);
  pinMode(potPin, INPUT);
  pinMode(onOffSwitchPin, INPUT);
  pinMode(LED, OUTPUT);
  // put your setup code here, to run once:

}

void loop() {
  potVal = analogRead(potPin) / 4;
  int sensorVal = analogRead(tempPin);
  float voltage = (sensorVal / 1024.0) * 5.0;
  float temperature = (voltage - 0.5) * 100;
  potVal2 = analogRead(potPin) / 16;
  luminositeVal = analogRead(luminositeSensorPin) / 16; Serial.print(F("Valeur sensor :   ")); Serial.println(luminositeVal);
  if (digitalRead(onOffSwitchPin)) {
    onOffSwitchState = 1;
    Serial.print(F(" onOffSwitchState =")); Serial.println( onOffSwitchState );
  }

  else {
    onOffSwitchState = 0;
    Serial.print(F(" onOffSwitchState =")); Serial.println( onOffSwitchState );
  }

  delay(1500);

  if (onOffSwitchState == 1) {
    Serial.print(F("  if..........onOffSwitchState =")); Serial.println( onOffSwitchState );
    if (temperature <= limitTemp) {
      Serial.print(F(" if.......temperature <= limitTemp =")); Serial.println( temperature );
      LEDOn = 1; Serial.print(F(" LEDOn =")); Serial.println( LEDOn );
    }
  }
  else {
    LEDOn = 0; Serial.print(F(" LEDOn =")); Serial.println( LEDOn );
  }

  if (LEDOn == 1) {
    Serial.print(F(" if..........LEDOn =  1  ")); Serial.println( LEDOn );
    analogWrite(LED, potVal); Serial.print(F(" LED =    ")); Serial.println(potVal );
  }
  else {
    analogWrite(LED, LOW); Serial.print(F(" LED =    ")); Serial.println(LOW );
  }
  if (luminositeVal < potVal) {
    potVal2 + 1; Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }
  else if (luminositeVal > potVal) {
    potVal2 - 1; Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }
  else if (luminositeVal == potVal) {
    potVal2 + 0; Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }


}

Bonsoir J-M-L,

c'est 1023 / 4 qui donne un nombre entre 0 et 255 (à la louche) pas en divisant par 16

Oui c'est vrai mais c'est pour que ca soit plus cohérent avec la valeur de mon potentiométre, et même en ne divisant que par 4 ca ne fonctionne pas le PWM n'augmente pas pour atteindre le seuil que j'ai fixer avec le potentiomètre ...

Merci de votre réponse si rapide !

qu'est-ce qui ne marche pas ? vous affichez plein de valeurs donc vous devez avoir une idée de ce qu'il se passe (ou pas)

J'ai fait une erreur dans le code, voilà le programme correct mais qui ne fonctionne pas.
Ce qui ne fonctionne pas c'est le fait que je choisi une certaine luminosité avec le potentiométre, et je souhaiterais qu'elle soit respecter (que la luminosité de la LED et de l'extérieur respecte le seuil fixer par le potentiométre).
Je récupère la luminosité réelle par la photorésistance, la valeur de mon potentiométre, et de mon bouton marche arrêt.
L'idée c'est que si la luminosité naturelle augmentent je baisse automatiquement l'intensité de la DEL et inversement.
C'est ca qui ne fonctionne pas ...

const int onOffSwitchPin = 7;
int onOffSwitchState = 0;
const int LED = 6; //sortit PWM
const int tempPin = A4;
const int potPin = A0;
const int luminositeSensorPin = A1;
int potVal;
float limitTemp = 30.00;

//int onOffSwitchState = 0;
int LEDOn = 0;
int temperature;
int luminositeVal = 0;
int potVal2;

void setup() {
  Serial.begin(115200);
  pinMode(tempPin, INPUT);
  pinMode(potPin, INPUT);
  pinMode(onOffSwitchPin, INPUT);
  pinMode(LED, OUTPUT);
  // put your setup code here, to run once:

}

void loop() {
  potVal = analogRead(potPin) / 16;
  int sensorVal = analogRead(tempPin);
  float voltage = (sensorVal / 1024.0) * 5.0;
  float temperature = (voltage - 0.5) * 100;
  potVal2 = analogRead(potPin) / 16;
  luminositeVal = analogRead(luminositeSensorPin) / 16; Serial.print(F("Valeur sensor :   ")); Serial.println(luminositeVal);
  if (digitalRead(onOffSwitchPin)) {
    onOffSwitchState = 1;
    Serial.print(F(" onOffSwitchState =")); Serial.println( onOffSwitchState );
  }

  else {
    onOffSwitchState = 0;
    Serial.print(F(" onOffSwitchState =")); Serial.println( onOffSwitchState );
  }

  delay(1500);

  if (onOffSwitchState == 1) {
    Serial.print(F("  if..........onOffSwitchState =")); Serial.println( onOffSwitchState );
    if (temperature <= limitTemp) {
      Serial.print(F(" if.......temperature <= limitTemp =")); Serial.println( temperature );
      LEDOn = 1; Serial.print(F(" LEDOn =")); Serial.println( LEDOn );
    }
  }
  else {
    LEDOn = 0; Serial.print(F(" LEDOn =")); Serial.println( LEDOn );
  }

  if (LEDOn == 1) {
    Serial.print(F(" if..........LEDOn =  1  ")); Serial.println( LEDOn );
    analogWrite(LED, potVal); Serial.print(F(" LED =    ")); Serial.println(potVal );
  }
  else {
    analogWrite(LED, LOW); Serial.print(F(" LED =    ")); Serial.println(LOW );
  }
  if (luminositeVal < potVal) {
    potVal2 + 1; Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }
  else if (luminositeVal > potVal) {
    potVal2 - 1; Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }
  else if (luminositeVal == potVal) {
    potVal2 + 0; Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }


}

les bouts de code en rouge sont supposés faire quoi? un petit calcul comme cela, pour faire beau? vous ne stockez le résultat de l'addition nulle part...

if (luminositeVal < potVal) {
    [color=red]potVal2 + 1;[/color] Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }
  else if (luminositeVal > potVal) {
    [color=red]potVal2 - 1;[/color] Serial.print(F("Valeur PotVal2   ")); Serial.println(potVal2 );
  }

et bien je pense que c'est cela qui ne fonctionne pas ...
Comment puis-je faire ?

ben je ne sais pas ce que vous voulez faire... pourquoi + ou - 1 ? c'est pas la valeur du potentiomètre qui est intéressante ?

je crois qu'il faut redéfinir un peu votre cahier des charges:

  • un bouton qui permet d'allumer ou éteindre la "lampe" (LED)
  • une photorésistance qui pilote la valeur de l'éclairage: plus il fait jour, moins la lampe brille quand elle est allumée
  • un potentiomètre qui définit la valeur maximum de la luminosité de la "lampe"

est-ce bien cela?