-1

I have encountered a problem concerning the repetition of the program that means it executes once (since the sensor passes to 1 it calculates 5 seconds (the sensor is still at 1) after the lamp is lit). Arduino always counts after the sensor goes to 0 then in 1 but there the lamp does not light.

Ideas ?

static byte capteurPrecedent = LOW;
static unsigned long debutAppui;

void setup(){
  pinMode(4,INPUT);
  pinMode(6,OUTPUT);
}

void loop(){

  byte capteur = digitalRead(4);
  if (capteur != capteurPrecedent)
  {
    if (capteur == HIGH)
    {
      debutAppui = millis();
    }
    else
    {
      debutAppui = 0;
    }
    CapteurPrecedent = capteur;
    delay(20);             
  }
  if (debutAppui != 0 && millis() - debutAppui> = 5000)
  {
    DigitalWrite(6,HIGH);}

  }

Update from OP:

Nothing changed, and I don't want the lamp still lit after 5 ms passed and another problem - if the capteur passes in 0 the lamp still lit.

Pleas your help !

  • Can u give me a propositions ? – Mary Apr 18 at 11:54
  • Please don't edit and radically change the nature of your question. Also, the first void had a capital letter, which I've fixed... – Greenonline Apr 18 at 11:54
  • 1
    In your code if you write once lamp it remains high you are not writing it low in any case. – Vaibhav Apr 18 at 11:59
  • 1
    Your program doesn't compile. CapteurPrecedent = capteur; should be capteurPrecedent = capteur; (lower case ‘c’), millis() - debutAppui> = 5000 should be millis() - debutAppui >= 5000 (no space between ‘>’ and ‘=’), and DigitalWrite(6,HIGH); should be digitalWrite(6,HIGH); (lower case ‘d’). As a courtesy to those trying to help you, you should at least test the exact code you are posting, and make sure its behavior matches your question. – Edgar Bonet Apr 18 at 17:41
  • 1
    @Mary please use English on this SE. See help center – sa_leinad Apr 19 at 15:55
0

I just made some changes in you code i.e. add low to the lamp if capteur passes 0 and some brackets in you if condition of writing lamp high.

void loop(){
  byte capteur = digitalRead(4);
  if (capteur != capteurPrecedent)
  {
    if (capteur == HIGH)
    {
      debutAppui = millis();
    }
    else
    {
      debutAppui = 0;
      DigitalWrite(6,LOW);
    }
    CapteurPrecedent = capteur;
    delay(20);             
  }
  if (debutAppui != 0 && ((millis() - debutAppui ) >= 5000))
  {
    DigitalWrite(6,HIGH);
  }
}
  • Even i add low to the lamp it still lit !!! – Mary Apr 18 at 12:30
0

Variables are case sensitive. Your problem is that on line 22 you reference CapteurPrecedent instead of capteurPrecedent. Your code should look like this:

byte capteurPrecedent = LOW;  // <---- wrong use of static
unsigned long debutAppui;     // <---- wrong use of static

void setup(){
  pinMode(4,INPUT);
  pinMode(6,OUTPUT);
}

void loop(){

  byte capteur = digitalRead(4);
  if (capteur != capteurPrecedent)
  {
    if (capteur == HIGH)
    {
      debutAppui = millis();
    }
    else
    {
      debutAppui = 0;
    }

    capteurPrecedent = capteur;  // <---- Fixed capital
    delay(20);             
  }

  if ((debutAppui != 0) && ((millis() - debutAppui) >= 5000))  // <---- fixed ">="
  {
    digitalWrite(6,HIGH);  // <---- Fixed capital
  }
}

Your question is not clear, so if you want the sensor to be on/high for 5 seconds it would be better re-written as:

unsigned long debutAppui;

void setup(){
  pinMode(4,INPUT);
  pinMode(6,OUTPUT);
}

void loop(){

  byte capteur = digitalRead(4);
  if (capteur == HIGH)
  {
    if ((millis() - debutAppui) >= 5000)
    {
      digitalWrite(6,HIGH);
    }
    else
    {
      digitalWrite(6,LOW);
    }
  }
  else  // ie capteur == LOW
  {
    debutAppui = millis();
    digitalWrite(6,LOW);             
  }

delay(20);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.