Problem with code

Hello,

Can anyone spot the error in this code? The buzzer on D7 don't work, and I need to hold the button down for 5 seconds for it to change levels... I shold change levels with only a push... If you hold the button for 5 seconds it should turn D7 true untill you push the button again..

 int buttonSensor = analogRead(A1);
 int countbuttonsec = 0;
 int level = 0;
 bool levelon = false;
 const int ledPin =  3;
 const int relaisPin =  7; 

#define NOTE_G3  196

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(relaisPin, OUTPUT);
Serial.begin(9600);
}


void loop() {
  // put your main code here, to run repeatedly:

  // Convert the analog reading (which goes from 0 - 1023 er 40) to a voltage (0 - 5V):
  buttonSensor = analogRead(A1);
  if (buttonSensor > 961) {
    countbuttonsec++;
    if (countbuttonsec == 1){
    if (level == 0){
      digitalWrite(ledPin, HIGH);
      buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      level1();
      level++;
    }
    else  if (level == 1){
      digitalWrite(ledPin, HIGH);
       buzz();
      delay(10000);
      digitalWrite(ledPin, LOW);
      delay(500);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      level2();
      level++;
    }
    else  if (level == 2){
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(500);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(500);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      
      level3();
      level++;
    }
    else  if (level == 3){
        digitalWrite(ledPin, HIGH);
         buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(500);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(500);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(500);
      digitalWrite(ledPin, HIGH);
       buzz();
      delay(1000);
      digitalWrite(ledPin, LOW);
      level4();
      level++;
    }
      else  if (level == 4){
        digitalWrite(ledPin, HIGH);
         buzz();
      delay(300);
      digitalWrite(ledPin, LOW);
      delay(300);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(300);
      digitalWrite(ledPin, LOW);
      delay(300);
       digitalWrite(ledPin, HIGH);
        buzz();
      delay(300);
      digitalWrite(ledPin, LOW);
      level = 0;
    }
    }
    else if(countbuttonsec > 5000){
       buzz();
        buzz();
         buzz();
      if (level != 5){
     digitalWrite(relaisPin, 250); 
     digitalWrite(ledPin, HIGH);
     delay(1000);
     digitalWrite(ledPin, LOW);
     level = 5;
      }
      else{
        digitalWrite(relaisPin, 0);
             digitalWrite(ledPin, HIGH);
     delay(200);
     digitalWrite(ledPin, LOW);
        level = 0;
      }
    }
   
  }
   else{
     countbuttonsec = 0;
     if (level > 0){
       if (level == 1){      
      level1();
    }
    else  if (level == 2){
      level2();
    }
    else  if (level == 3){
      level3();
    }
    else  if (level == 4){
      level4();
    }
     }
    }
  
  // print out the value you read:
  
}

void level1(){
  //levelon = true;
 digitalWrite(relaisPin, 250);
  delay(1500);
  digitalWrite(relaisPin, 0);
  delay(5000);
  //levelon = false
}
void level2(){
  //levelon = true;
 digitalWrite(relaisPin, 250);
  delay(1500);
  digitalWrite(relaisPin, 0);
  delay(10000);
  //levelon = false
}
void level3(){
  //levelon = true;
 digitalWrite(relaisPin, 250);
  delay(1500);
  digitalWrite(relaisPin, 0);
  delay(150000);
  //levelon = false
}
void level4(){
  //levelon = true;
 digitalWrite(relaisPin, 250);
  delay(1500);
  digitalWrite(relaisPin, 0);
  delay(200000);
  //levelon = false
}
void buzz(){
 tone(8, NOTE_G3, 0.2); 
}

The buzzer on D7 don't work

If you have a buzzer connected to the pin, why the hell is it named relaisPin? Is relais french for buzzer?

 const int relaisPin =  7;

You REALLY need to put every { on its own line AND use Tools + Auto Format AND add comments to your code. It is very hard to guess what that code is supposed to be doing.

and I need to hold the button down for 5 seconds for it to change levels.

Then quit using delay()!

Mark

PaulS: If you have a buzzer connected to the pin, why the hell is it named relaisPin? Is relais french for buzzer?

 const int relaisPin =  7;

It looks like your French is weak, Paul. Relais means relay and a relay may well be operating a buzzer.

Make sure you are on firm ground before launching smart remarks. :)

...R

kjetilhansen:
I need to hold the button down for 5 seconds for it to change levels.

That is because you are using delay() to manage your timing. The Arduino can do nothing until a delay() completes.

You need to use millis() to manage your timing as illustrated in several things at a time

…R

kjetilhansen: Can anyone spot the error in this code?

The mistake is using the "delay()" function to block the execution of the program.

I found delays of up to:

delay(10000);

The meaning is: "At this point of the code, block program execution for 10 seconds and DO NOTHING until time has passed."

If you don't want to halt your program every now and then, but want to be responsive for human interaction at every time, you strictly have to avoid every "delay()". DO NOT USE!

From your description you want to implement a "state machine". In your program the different states of your machine are named "level" and in each state there are certain sub-states with buzzing and switching pins HIGH or LOW.

So your state machine can either be working like that with 3 states (levels) : - level1 state ==> handled by level1 function - level2 state ==> handled by level2 function - level3 state ==> handled by level3 function And each of the level functions is using a technique like the Arduino example "BlinkWithoutDelay" to handle the fancy stuff.

Or your state machine can have much more states with much smaller state functions, each of them treated seperately.