code loopt na 3 keer vast.

Arduino specifieke Software
Berichten: 68
Geregistreerd: 28 Aug 2012, 15:14

code loopt na 3 keer vast.

Berichtdoor Nald » 25 Sep 2012, 22:09

hallo als ik onderstaande code draai, dan loopt hij na 3 keer vast.

De bedoeling is dat hij de teller na 30 sec reset naar 1, dit gaat de eerste 3 keer goed, maar wanneer ik dit voor de 4e keer doet, dan loopt hij vast bij 2. Weet iemand mischien wat ik over het hoofd zie?

ik weet dat het in het tijdgedeelte zit, want zodra ik dit gedeelte eruit haal, dan is er niks aan de hand.

Code: Alles selecteren
 int buttonState = HIGH;
 int buttonPushCounter = 1;
 int lastbuttonState=LOW;
 
 boolean lastButton =LOW;
 boolean currentButton =LOW;

 int wachttijdC = 10000;


void setup()
  {
  Serial.begin(9600);
  }

    boolean debounce(boolean last)
  { boolean current = digitalRead(buttonPin);
 if (last != current){
   delay(5);
   current = digitalRead(buttonPin); 
  }
    return current;
  }

void loop(){

 currentButton = debounce(lastButton);
     
 buttonState = currentButton;
 
 if (buttonState != lastbuttonState ) {
 if (buttonState == HIGH) {
   buttonPushCounter++;
   Serial.println("on");
   Serial.print("number of button pushes:  ");
   Serial.println(buttonPushCounter);
   
      }else{
       
    Serial.println("off");}}
    lastbuttonState= buttonState;
   
  if (buttonPushCounter % 4 == 0 || millis()-lastMillis > wachttijdC){
    lastMillis = millis();   
    buttonPushCounter =1;}


   
   
  if (buttonPushCounter == 2 && currentButton == HIGH){
    delay(250);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);
    digitalWrite(Melody,HIGH);}
     
      else
     
  if (buttonPushCounter == 3 && currentButton == HIGH){
    delay(250);
    digitalWrite(Melody,HIGH);
    onthoudtstate= HIGH;
    digitalWrite(Sound, onthoudtstate);}
   
     else
     
  if (buttonPushCounter == 3 && currentButton == LOW){
    digitalWrite(Melody,LOW);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);}
   
     else
     
  if (buttonPushCounter == 1 && currentButton == HIGH){
    delay(250);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);
    digitalWrite(Melody,HIGH);
    digitalWrite(KnipR,HIGH);
    digitalWrite(KnipL,LOW);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(Melody,HIGH);
    delay(50);
    digitalWrite(KnipR,HIGH);
    digitalWrite(Melody,HIGH);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(KopR,HIGH);
    digitalWrite(Melody,HIGH);
    delay(150);
    digitalWrite(KopR,LOW);
    digitalWrite(MistR,HIGH);
    digitalWrite(Melody,HIGH);
    delay(150);
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,HIGH);
    digitalWrite(Melody,HIGH);
    digitalWrite(Sound,LOW);
    delay(150);
    digitalWrite(MistL,LOW);
    digitalWrite(KopL,HIGH);
    digitalWrite(Melody,HIGH);
    delay(150);
    digitalWrite(KopL,LOW);
    digitalWrite(KnipL,HIGH);
    digitalWrite(Melody,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
    digitalWrite(Melody,HIGH);
    digitalWrite(Sound,LOW);
    delay(50);
    digitalWrite(KnipL,HIGH);
    digitalWrite(Melody,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
   
       }else{
         
    digitalWrite(Melody,LOW);
    digitalWrite(Sound,LOW);
    digitalWrite(KopL,LOW);
    digitalWrite(KopR,LOW);
    digitalWrite(KnipL,LOW);
    digitalWrite(KnipR,LOW);
    digitalWrite(MistL,LOW);
    digitalWrite(MistR,LOW);
     

   }}

Advertisement

Gebruikers-avatar
Berichten: 700
Geregistreerd: 05 Mrt 2012, 21:56
Woonplaats: Appingedam

Re: code loopt na 3 keer vast.

Berichtdoor pjh » 26 Sep 2012, 00:07

volgens mij is je code niet compleet. Is niet handig.

Gebruikers-avatar
Berichten: 188
Geregistreerd: 23 Dec 2011, 00:12

Re: code loopt na 3 keer vast.

Berichtdoor Duality » 27 Sep 2012, 00:02

het spijt me maar je code ziet er niet heel erg duidelijk/overzichtelijk uit.
EDIT: heb er even een autoformat over heen gehaald als je het niet erg vind :)

boven dien vald me iets vreemds op aan je code, bijv bij: if (buttonPushCounter == 1 && currentButton == HIGH)
die else daar boven, wat doet die? het lijkt of die {} (brackets) mist .. ik weet niet zo goed wat ik er van moet maken :)
ow ik zie het all je hebt die if die bij die else heel raar geindent ... (geplaatst)

Code: Alles selecteren
int buttonState = HIGH;
int buttonPushCounter = 1;
int lastbuttonState=LOW;

boolean lastButton =LOW;
boolean currentButton =LOW;

int wachttijdC = 10000;


void setup()
{
  Serial.begin(9600);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current){
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}

void loop(){

  currentButton = debounce(lastButton);

  buttonState = currentButton;

  if (buttonState != lastbuttonState ) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);

    }
    else{

      Serial.println("off");
    }
  }
  lastbuttonState= buttonState;

  if (buttonPushCounter % 4 == 0 || millis()-lastMillis > wachttijdC){
    lastMillis = millis();   
    buttonPushCounter =1;
  }




  if (buttonPushCounter == 2 && currentButton == HIGH){
    delay(250);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);
    digitalWrite(Melody,HIGH);
  }

  else

      if (buttonPushCounter == 3 && currentButton == HIGH){
      delay(250);
      digitalWrite(Melody,HIGH);
      onthoudtstate= HIGH;
      digitalWrite(Sound, onthoudtstate);
    }

    else

        if (buttonPushCounter == 3 && currentButton == LOW){
        digitalWrite(Melody,LOW);
        onthoudtstate= LOW;
        digitalWrite(Sound, onthoudtstate);
      }

      else

          if (buttonPushCounter == 1 && currentButton == HIGH){
          delay(250);
          onthoudtstate= LOW;
          digitalWrite(Sound, onthoudtstate);
          digitalWrite(Melody,HIGH);
          digitalWrite(KnipR,HIGH);
          digitalWrite(KnipL,LOW);
          delay(75);
          digitalWrite(KnipR,LOW);
          digitalWrite(Melody,HIGH);
          delay(50);
          digitalWrite(KnipR,HIGH);
          digitalWrite(Melody,HIGH);
          delay(75);
          digitalWrite(KnipR,LOW);
          digitalWrite(KopR,HIGH);
          digitalWrite(Melody,HIGH);
          delay(150);
          digitalWrite(KopR,LOW);
          digitalWrite(MistR,HIGH);
          digitalWrite(Melody,HIGH);
          delay(150);
          digitalWrite(MistR,LOW);
          digitalWrite(MistL,HIGH);
          digitalWrite(Melody,HIGH);
          digitalWrite(Sound,LOW);
          delay(150);
          digitalWrite(MistL,LOW);
          digitalWrite(KopL,HIGH);
          digitalWrite(Melody,HIGH);
          delay(150);
          digitalWrite(KopL,LOW);
          digitalWrite(KnipL,HIGH);
          digitalWrite(Melody,HIGH);
          delay(75);
          digitalWrite(KnipL,LOW);
          digitalWrite(Melody,HIGH);
          digitalWrite(Sound,LOW);
          delay(50);
          digitalWrite(KnipL,HIGH);
          digitalWrite(Melody,HIGH);
          delay(75);
          digitalWrite(KnipL,LOW);

        }
      else{

        digitalWrite(Melody,LOW);
        digitalWrite(Sound,LOW);
        digitalWrite(KopL,LOW);
        digitalWrite(KopR,LOW);
        digitalWrite(KnipL,LOW);
        digitalWrite(KnipR,LOW);
        digitalWrite(MistL,LOW);
        digitalWrite(MistR,LOW);


      }
}

Berichten: 68
Geregistreerd: 28 Aug 2012, 15:14

Re: code loopt na 3 keer vast.

Berichtdoor Nald » 27 Sep 2012, 21:37

Ik wou de code wat beknopter weergeven.

Hierbij de volledige code. Om heel eerlijk te zijn, snap ik niet de gehele werking van de pushbuttoncounter, deze is "geleend"

Wat ik graag wil is dat de counter gereset wordt elke 60? seconden.

Code: Alles selecteren
 int MistR=2;
 int buttonPin=3;
 int MistL=4;
 int KopL=5;
 int MistA=6;
 int KopR=7;
 int Sound=8;
 int KnipL=9;
 int KnipR=10;
 int Melody=11;
 int Switch=12;
 int Trek=13;

 int licht= A1;
 int potPin= A2;
 int onthoudt=A5;
 
 int lichtstate=0;
 int Trekstate = 0;
 int afstandbediening=0;
 
 int lastMillis = 0;
 int interval = 30000;
 long time =0;
 long bounce = 200;
 
 int reading=0;
 int calibrate = 511;
 int firstreading = 511;
 int wachttijdA = 15000;
 int wachttijdB = 14000;
 int wachttijdC = 60000;
 int Marge = 6;
 int Stuuruitslag = 30;
 long VorigeMillis = 0;

 int onthoudtstate = LOW;
 int onthouden;
 int lastonthoudtstate = HIGH;

 int buttonState = HIGH;
 int buttonPushCounter = 1;
 int lastbuttonState=LOW;
 
 boolean lastButton =LOW;
 boolean currentButton =LOW;

void setup()
  {
  pinMode (KopL, OUTPUT);
  pinMode (KopR, OUTPUT);
  pinMode (MistL, OUTPUT);
  pinMode (MistR, OUTPUT);
  pinMode (KnipL, OUTPUT);
  pinMode (KnipR, OUTPUT);
  pinMode (Melody, OUTPUT);
  pinMode (Sound, OUTPUT);
  pinMode (MistA, OUTPUT);
  pinMode (Trek, INPUT);
  pinMode (buttonPin, INPUT);
  pinMode (onthoudt, INPUT);
  Serial.begin(9600);
  }

    boolean debounce(boolean last)
  { boolean current = digitalRead(buttonPin);
 if (last != current){
   delay(5);
   current = digitalRead(buttonPin); 
  }
    return current;
  }


void loop(){
 
  onthouden = digitalRead(onthoudt);
 
  if (onthouden == HIGH && lastonthoudtstate == LOW && millis() - time > bounce){
  if (onthoudtstate == HIGH)
   onthoudtstate = LOW;
   
      else
   
   onthoudtstate = HIGH;
   time= millis(); }
   digitalWrite(Sound, onthoudtstate);
 
  lastonthoudtstate = onthouden;
 
   lichtstate=analogRead(licht);
   
  if (lichtstate > 500){
    digitalWrite(KopL,HIGH);
    digitalWrite(KopR,HIGH);   
     }
       
  reading = analogRead(potPin);
   
  unsigned long MillisNu = millis();
  if (MillisNu - VorigeMillis > wachttijdA){
    VorigeMillis = MillisNu;
    firstreading = analogRead(potPin);}
   
  if (MillisNu - VorigeMillis > wachttijdB){
    if (0 < (firstreading - reading) < Marge){
      calibrate = firstreading; }else
     
    if (0 < (reading - firstreading) < Marge){
      calibrate = firstreading; }else{
       
        calibrate = calibrate; }}
       
  if (reading - calibrate > Stuuruitslag && lichtstate > 500){
    digitalWrite(MistL,HIGH);
    digitalWrite(MistR,LOW);
    delay(1000);}
   
    else
 
  if (calibrate - reading > Stuuruitslag && lichtstate > 500){
    digitalWrite(MistR,HIGH);
    digitalWrite(MistL,LOW);
    delay(1000);}
   
    else{
     
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,LOW);
    }
 
  Trekstate = digitalRead(Trek);
 
  if (Trekstate ==HIGH && lichtstate < 500){
    delay(250);
    digitalWrite(KnipR,HIGH);
    digitalWrite(KnipL,LOW);
    delay(75);
    digitalWrite(KnipR,LOW);
    delay(50);
    digitalWrite(KnipR,HIGH);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(KopR,HIGH);
    delay(150);
    digitalWrite(KopR,LOW);
    digitalWrite(MistR,HIGH);
    delay(150);
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,HIGH);
    delay(150);
    digitalWrite(MistL,LOW);
    digitalWrite(KopL,HIGH);
    delay(150);
    digitalWrite(KopL,LOW);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
    delay(50);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
       }
     
     if (Trekstate ==HIGH && lichtstate > 500){
    delay(250);
    digitalWrite(KnipR,HIGH);
    digitalWrite(KnipL,LOW);
    delay(75);
    digitalWrite(KnipR,LOW);
    delay(50);
    digitalWrite(KnipR,HIGH);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(KopR,HIGH);
    delay(150);
    digitalWrite(MistR,HIGH);
    delay(150);
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,HIGH);
    delay(150);
    digitalWrite(MistL,LOW);
    digitalWrite(KopL,HIGH);
    delay(150);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
    delay(50);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
       } 
     
 
   
 currentButton = debounce(lastButton);
     
 buttonState = currentButton;
 
 if (buttonState != lastbuttonState ) {
 if (buttonState == HIGH) {
   buttonPushCounter++;
   Serial.println("on");
   Serial.print("number of button pushes:  ");
   Serial.println(buttonPushCounter);
   
      }else{
       
    Serial.println("off");}}
    lastbuttonState= buttonState;
   
  if (buttonPushCounter % 4 == 0){ 
    buttonPushCounter =1;}

  if (MillisNu-lastMillis > wachttijdC){
    lastMillis = MillisNu;   
    buttonPushCounter =1;}
   
   
  if (buttonPushCounter == 2 && currentButton == HIGH){
    delay(250);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);
    digitalWrite(Melody,HIGH);}
     
      else
     
  if (buttonPushCounter == 3 && currentButton == HIGH){
    delay(250);
    digitalWrite(Melody,HIGH);
    onthoudtstate= HIGH;
    digitalWrite(Sound, onthoudtstate);}
   
     else
     
  if (buttonPushCounter == 3 && currentButton == LOW){
    digitalWrite(Melody,LOW);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);}
   
     else
     
  if (buttonPushCounter == 1 && currentButton == HIGH){
    delay(250);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);
    digitalWrite(Melody,HIGH);
    digitalWrite(KnipR,HIGH);
    digitalWrite(KnipL,LOW);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(Melody,HIGH);
    delay(50);
    digitalWrite(KnipR,HIGH);
    digitalWrite(Melody,HIGH);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(KopR,HIGH);
    digitalWrite(Melody,HIGH);
    delay(150);
    digitalWrite(KopR,LOW);
    digitalWrite(MistR,HIGH);
    digitalWrite(Melody,HIGH);
    delay(150);
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,HIGH);
    digitalWrite(Melody,HIGH);
    digitalWrite(Sound,LOW);
    delay(150);
    digitalWrite(MistL,LOW);
    digitalWrite(KopL,HIGH);
    digitalWrite(Melody,HIGH);
    delay(150);
    digitalWrite(KopL,LOW);
    digitalWrite(KnipL,HIGH);
    digitalWrite(Melody,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
    digitalWrite(Melody,HIGH);
    digitalWrite(Sound,LOW);
    delay(50);
    digitalWrite(KnipL,HIGH);
    digitalWrite(Melody,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
   
       }else{
         
    digitalWrite(Melody,LOW);
    digitalWrite(Sound,LOW);
    digitalWrite(KopL,LOW);
    digitalWrite(KopR,LOW);
    digitalWrite(KnipL,LOW);
    digitalWrite(KnipR,LOW);
    digitalWrite(MistL,LOW);
    digitalWrite(MistR,LOW);
     

   }}

Gebruikers-avatar
Berichten: 188
Geregistreerd: 23 Dec 2011, 00:12

Re: code loopt na 3 keer vast.

Berichtdoor Duality » 28 Sep 2012, 00:36

jou code is still confusing me :), selecteer je code in de arduino ide en zoek in het menu naar autoformat, dat maakt het wat beter lees baar voor mensen :)

Berichten: 68
Geregistreerd: 28 Aug 2012, 15:14

Re: code loopt na 3 keer vast.

Berichtdoor Nald » 28 Sep 2012, 18:02

Bij deze. Maar het gaat dus om het gedeelte bij de pushbutton counter

Code: Alles selecteren
int MistR=2;
int buttonPin=3;
int MistL=4;
int KopL=5;
int MistA=6;
int KopR=7;
int Sound=8;
int KnipL=9;
int KnipR=10;
int Melody=11;
int Switch=12;
int Trek=13;

int licht= A1;
int potPin= A2;
int onthoudt=A5;

int lichtstate=0;
int Trekstate = 0;
int afstandbediening=0;

int lastMillis = 0;
int interval = 30000;
long time =0;
long bounce = 200;

int reading=0;
int calibrate = 511;
int firstreading = 511;
int wachttijdA = 15000;
int wachttijdB = 14000;
int wachttijdC = 60000;
int Marge = 6;
int Stuuruitslag = 30;
long VorigeMillis = 0;

int onthoudtstate = LOW;
int onthouden;
int lastonthoudtstate = HIGH;

int buttonState = HIGH;
int buttonPushCounter = 1;
int lastbuttonState=LOW;

boolean lastButton =LOW;
boolean currentButton =LOW;

void setup()
{
  pinMode (KopL, OUTPUT);
  pinMode (KopR, OUTPUT);
  pinMode (MistL, OUTPUT);
  pinMode (MistR, OUTPUT);
  pinMode (KnipL, OUTPUT);
  pinMode (KnipR, OUTPUT);
  pinMode (Melody, OUTPUT);
  pinMode (Sound, OUTPUT);
  pinMode (MistA, OUTPUT);
  pinMode (Trek, INPUT);
  pinMode (buttonPin, INPUT);
  pinMode (onthoudt, INPUT);
  Serial.begin(9600);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current){
    delay(5);
    current = digitalRead(buttonPin); 
  }
  return current;
}


void loop(){

  onthouden = digitalRead(onthoudt);

  if (onthouden == HIGH && lastonthoudtstate == LOW && millis() - time > bounce){
    if (onthoudtstate == HIGH)
      onthoudtstate = LOW;

    else

      onthoudtstate = HIGH;
    time= millis();
  }
  digitalWrite(Sound, onthoudtstate);

  lastonthoudtstate = onthouden;

  lichtstate=analogRead(licht);

  if (lichtstate > 500){
    digitalWrite(KopL,HIGH);
    digitalWrite(KopR,HIGH);   
  }

  reading = analogRead(potPin);

  unsigned long MillisNu = millis();
  if (MillisNu - VorigeMillis > wachttijdA){
    VorigeMillis = MillisNu;
    firstreading = analogRead(potPin);
  }

  if (MillisNu - VorigeMillis > wachttijdB){
    if (0 < (firstreading - reading) < Marge){
      calibrate = firstreading;
    }
    else

        if (0 < (reading - firstreading) < Marge){
        calibrate = firstreading;
      }
    else{

      calibrate = calibrate;
    }
  }

  if (reading - calibrate > Stuuruitslag && lichtstate > 500){
    digitalWrite(MistL,HIGH);
    digitalWrite(MistR,LOW);
    delay(1000);
  }

  else

      if (calibrate - reading > Stuuruitslag && lichtstate > 500){
      digitalWrite(MistR,HIGH);
      digitalWrite(MistL,LOW);
      delay(1000);
    }

  else{

    digitalWrite(MistR,LOW);
    digitalWrite(MistL,LOW);
  }

  Trekstate = digitalRead(Trek);

  if (Trekstate ==HIGH && lichtstate < 500){
    delay(250);
    digitalWrite(KnipR,HIGH);
    digitalWrite(KnipL,LOW);
    delay(75);
    digitalWrite(KnipR,LOW);
    delay(50);
    digitalWrite(KnipR,HIGH);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(KopR,HIGH);
    delay(150);
    digitalWrite(KopR,LOW);
    digitalWrite(MistR,HIGH);
    delay(150);
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,HIGH);
    delay(150);
    digitalWrite(MistL,LOW);
    digitalWrite(KopL,HIGH);
    delay(150);
    digitalWrite(KopL,LOW);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
    delay(50);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
  }

  if (Trekstate ==HIGH && lichtstate > 500){
    delay(250);
    digitalWrite(KnipR,HIGH);
    digitalWrite(KnipL,LOW);
    delay(75);
    digitalWrite(KnipR,LOW);
    delay(50);
    digitalWrite(KnipR,HIGH);
    delay(75);
    digitalWrite(KnipR,LOW);
    digitalWrite(KopR,HIGH);
    delay(150);
    digitalWrite(MistR,HIGH);
    delay(150);
    digitalWrite(MistR,LOW);
    digitalWrite(MistL,HIGH);
    delay(150);
    digitalWrite(MistL,LOW);
    digitalWrite(KopL,HIGH);
    delay(150);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
    delay(50);
    digitalWrite(KnipL,HIGH);
    delay(75);
    digitalWrite(KnipL,LOW);
  } 



  currentButton = debounce(lastButton);

  buttonState = currentButton;

  if (buttonState != lastbuttonState ) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);

    }
    else{

      Serial.println("off");
    }
  }
  lastbuttonState= buttonState;

  if (buttonPushCounter % 4 == 0){ 
    buttonPushCounter =1;
  }

  if (MillisNu-lastMillis > wachttijdC){
    lastMillis = MillisNu;   
    buttonPushCounter =1;
  }


  if (buttonPushCounter == 2 && currentButton == HIGH){
    delay(250);
    onthoudtstate= LOW;
    digitalWrite(Sound, onthoudtstate);
    digitalWrite(Melody,HIGH);
  }

  else

      if (buttonPushCounter == 3 && currentButton == HIGH){
      delay(250);
      digitalWrite(Melody,HIGH);
      onthoudtstate= HIGH;
      digitalWrite(Sound, onthoudtstate);
    }

    else

        if (buttonPushCounter == 3 && currentButton == LOW){
        digitalWrite(Melody,LOW);
        onthoudtstate= LOW;
        digitalWrite(Sound, onthoudtstate);
      }

      else

          if (buttonPushCounter == 1 && currentButton == HIGH){
          delay(250);
          onthoudtstate= LOW;
          digitalWrite(Sound, onthoudtstate);
          digitalWrite(Melody,HIGH);
          digitalWrite(KnipR,HIGH);
          digitalWrite(KnipL,LOW);
          delay(75);
          digitalWrite(KnipR,LOW);
          digitalWrite(Melody,HIGH);
          delay(50);
          digitalWrite(KnipR,HIGH);
          digitalWrite(Melody,HIGH);
          delay(75);
          digitalWrite(KnipR,LOW);
          digitalWrite(KopR,HIGH);
          digitalWrite(Melody,HIGH);
          delay(150);
          digitalWrite(KopR,LOW);
          digitalWrite(MistR,HIGH);
          digitalWrite(Melody,HIGH);
          delay(150);
          digitalWrite(MistR,LOW);
          digitalWrite(MistL,HIGH);
          digitalWrite(Melody,HIGH);
          digitalWrite(Sound,LOW);
          delay(150);
          digitalWrite(MistL,LOW);
          digitalWrite(KopL,HIGH);
          digitalWrite(Melody,HIGH);
          delay(150);
          digitalWrite(KopL,LOW);
          digitalWrite(KnipL,HIGH);
          digitalWrite(Melody,HIGH);
          delay(75);
          digitalWrite(KnipL,LOW);
          digitalWrite(Melody,HIGH);
          digitalWrite(Sound,LOW);
          delay(50);
          digitalWrite(KnipL,HIGH);
          digitalWrite(Melody,HIGH);
          delay(75);
          digitalWrite(KnipL,LOW);

        }
      else{

        digitalWrite(Melody,LOW);
        digitalWrite(Sound,LOW);
        digitalWrite(KopL,LOW);
        digitalWrite(KopR,LOW);
        digitalWrite(KnipL,LOW);
        digitalWrite(KnipR,LOW);
        digitalWrite(MistL,LOW);
        digitalWrite(MistR,LOW);


      }
}

Berichten: 68
Geregistreerd: 28 Aug 2012, 15:14

Re: code loopt na 3 keer vast.

Berichtdoor Nald » 02 Okt 2012, 21:47

ik heb de code zo geordened zoalser gevraagd werd.

Nu heeft er niemand een idee?

In dit gedeelte zit toch geen fout?
Code: Alles selecteren
  if (MillisNu-lastMillis > wachttijdC){
    lastMillis = MillisNu;   
    buttonPushCounter =1;}


Want zodra ik dit toevoeg loopt de code na 3 keer resetten vast.

Gebruikers-avatar
Berichten: 700
Geregistreerd: 05 Mrt 2012, 21:56
Woonplaats: Appingedam

Re: code loopt na 3 keer vast.

Berichtdoor pjh » 02 Okt 2012, 23:22

Ik vind het lastig je code te lezen. Die hele trein if else ... En al die variabelen om te kijken of er tijd en knopjes gedrukt zijn. Ik mis de heldere structuur, ondanks auto-opmaak. Wil best kijken of ik het zelfde kan bereiken op een andere manier. Je code doorlopen gaat niet lukken.

Berichten: 68
Geregistreerd: 28 Aug 2012, 15:14

Re: code loopt na 3 keer vast.

Berichtdoor Nald » 03 Okt 2012, 13:06

Eenvoudiger kan ik het niet maken. De fout zit in dit specifieke stuk van de code. Want als ik dit stuk apart upload, loopt hij ook na 3 keer vast.
Dit vastlopen gebeurt alleen als ik dit stukje toevoegt. zonder dit stukje doet hij het goed.

Code: Alles selecteren
 if (MillisNu-lastMillis > wachttijdC){
    lastMillis = MillisNu;   
    buttonPushCounter =1;}


Versimpelde code:
Code: Alles selecteren
int buttonPin=3;

int lastMillis = 0;

int wachttijdC = 15000;

int buttonState = HIGH;
int buttonPushCounter = 1;
int lastbuttonState=LOW;

boolean lastButton =LOW;
boolean currentButton =LOW;

void setup()
{

  pinMode (buttonPin, INPUT);
  Serial.begin(9600);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current){
    delay(5);
    current = digitalRead(buttonPin); 
  }
  return current;
}
void loop(){
  unsigned long MillisNu = millis();
  unsigned long LastMillis;
  currentButton = debounce(lastButton);

  buttonState = currentButton;

  if (buttonState != lastbuttonState ) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);

    }
    else{

      Serial.println("off");
    }
  }
  lastbuttonState= buttonState;

  if (buttonPushCounter % 4 == 0){ 
    buttonPushCounter =1;
  }

  if (MillisNu-lastMillis > wachttijdC){
    lastMillis = MillisNu;   
    buttonPushCounter =1;
  }


  if (buttonPushCounter == 2 && currentButton == HIGH){
    //actie 1
  }

  else

      if (buttonPushCounter == 3 && currentButton == HIGH){
      //actie 2
    }

    else

        if (buttonPushCounter == 3 && currentButton == LOW){
        //actie 3
      }

      else

          if (buttonPushCounter == 1 && currentButton == HIGH){
          //actie 4

        }
      else{
        //alles LOW
      }
}

Gebruikers-avatar
Berichten: 700
Geregistreerd: 05 Mrt 2012, 21:56
Woonplaats: Appingedam

Re: code loopt na 3 keer vast.

Berichtdoor pjh » 03 Okt 2012, 13:50

misschien kun je proberen deze eerste twee regels uit de loop, naar VOOR de void setup te plaatsen
unsigned long MillisNu;
unsigned long LastMillis;

en dan aan het begin van je loop alleen deze:
MillisNu = millis();

dus:
Code: Alles selecteren
  unsigned long MillisNu;
  unsigned long LastMillis;

int buttonPin=3;

int lastMillis = 0;

int wachttijdC = 15000;

int buttonState = HIGH;
int buttonPushCounter = 1;
int lastbuttonState=LOW;

boolean lastButton =LOW;
boolean currentButton =LOW;

void setup()
{

  pinMode (buttonPin, INPUT);
  Serial.begin(9600);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current){
    delay(5);
    current = digitalRead(buttonPin); 
  }
  return current;
}
void loop(){
  MillisNu = millis();
 
currentButton = debounce(lastButton);

  buttonState = currentButton;

  if (buttonState != lastbuttonState ) {
............

Volgende

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 19 gasten