Problem with PWM output and sensor input

Hi.

I'm doing a school project, where I need some help with some input. The idea is to make an alarm, and when it's turned on, there will be sent PWM signals to four LED's. Beside the LED's there will be four buttons. Each button should turn off the corresponding LED.

So far, I can make the PWM start and the buttons turns off the LED they're supposed to as long as the button is pushed.
But my problem is, that I want to make the LED stay turned off when the corresponding button is pushed. The PWM signal to the other LED's shall continue until their button is pushed.

I have been trying to work with some kind of state variable on each button without succes.

I hope someone can help me!
Thanks.

No code, no help :slight_smile:

Please put the code in code tags by using the hash button (#)

Pin setup:

#define Player1 6
#define Player2 9
#define Player3 10
#define Player4 11
#define Player1But A1
#define Player2But A2
#define Player3But A3
#define Player4But A4
#define Sound 13
boolean state1 = false;
boolean state2 = false;
boolean state3 = false;
boolean state4 = false;
void setup(){
  Serial.begin(9600);
  pinMode(Player1, OUTPUT);
  pinMode(Player2, OUTPUT);
  pinMode(Player3, OUTPUT);
  pinMode(Player4, OUTPUT);
  pinMode(Sound, OUTPUT);
}
void loop(){
    int alarmCount = 0;
    while(alarmCount < 1000){
     // Some other code running
      alarmCount++;
    }
    while(alarmCount >= 1000){
      pwmAll();
    }
}

Makes the LED’s go from LOW to HIGH and vice versa.

void pwmAll(){
  for(int i = 0; i < 255; i+=10) {
    if(checkState(Player1But, state1) == false){analogWrite(Player1,i);}
    if(checkState(Player2But, state2) == false){analogWrite(Player2,i);}
    if(checkState(Player3But, state3) == false){analogWrite(Player3,i);}
    if(checkState(Player4But, state4) == false){analogWrite(Player4,i);}
    lastPushed();
    delay(5);
  }
  alarmSound(true);
  for (int i = 255;i > 0; i-=10){
    if(checkState(Player1But, state1) == false){analogWrite(Player1, i);}
    if(checkState(Player2But, state2) == false){analogWrite(Player2, i);}
    if(checkState(Player3But, state3) == false){analogWrite(Player3, i);}
    if(checkState(Player4But, state4) == false){analogWrite(Player4, i);}
    lastPushed();
    delay(5);
  }
  analogWrite(Player1, 0);
  analogWrite(Player2, 0);
  analogWrite(Player3, 0);
  analogWrite(Player4, 0);
  alarmSound(true);
}
void alarmSound(boolean state){
  if(state == true){
  tone(Sound, 200);
  delay(175);
  noTone(Sound);
  }
}
boolean checkState(int pin, boolean state){
    if(analogRead(pin) > 5){
      state = true;
      analogWrite(pin,LOW);
      return true;
    }
    
    else {
      state = false;
      return false;
    }
}
boolean lastPushed(){
  if(state1 == true && state2 == true && state3 == true){
        analogWrite(Player1, LOW);
        analogWrite(Player2, LOW);
        analogWrite(Player3, LOW);
        analogWrite(Player4, HIGH);
        delay(5000);
  }
  if(state1 == true && state2 == true && state4 == true){
        analogWrite(Player1, LOW);
        analogWrite(Player2, LOW);
        analogWrite(Player4, LOW);
        analogWrite(Player3, HIGH);
        delay(5000);
  }
  if(state1 == true && state3 == true && state4 == true){
        analogWrite(Player1, LOW);
        analogWrite(Player3, LOW);
        analogWrite(Player4, LOW);
        analogWrite(Player2, HIGH);
        delay(5000);
  }
  if(state2 == true && state3 == true && state4 == true){
        analogWrite(Player2, LOW);
        analogWrite(Player3, LOW);
        analogWrite(Player4, LOW);
        analogWrite(Player1, HIGH);
        delay(5000);
  }
}

This is all the necessary code. And this is how I thought it could work, but it doesn’t work as I want to.
By the way, I am usinge Arduino Leonardo Atmega32u4.

if(state1 == true && state2 == true && state3 == true){
        analogWrite(Player1, LOW);
        analogWrite(Player2, LOW);
        analogWrite(Player3, LOW);
        analogWrite(Player4, HIGH);
        delay(5000);
  }
  if(state1 == true && state2 == true && state4 == true){
        analogWrite(Player1, LOW);
        analogWrite(Player2, LOW);
        analogWrite(Player4, LOW);
        analogWrite(Player3, HIGH);
        delay(5000);
  }

If the second set of lines is executed then so is the first you need to include the

&& state3 == false

conditions as well to make the combination unique.
This is the first thing wrong I spotted so there may be other errors as well.

Thanks for your reply!

I tried to make the change with the state, but it didn't change anything. When the button is pushed, the corresponding LED turns off, and the others continue with receiving the PWM signal. But it should turn off three of them, and let the last one be turned on.

I'm wondering why the states are changed in my checkState()-method, but when the program reaches lastPushed() it seems like they're back to defaults?

Why are you using analogue write with HIGH and LOW, you should have a number in here. Zero or 255 if you want it max or min.

I don't know why I'd use analogWrite() instead of digitalWrite(), but it doesn't change the problem.

I have been trying different things now.. and the only thing I want this do to is to turn off the LED once the corresponding button has been pushed.

the only thing I want this do to is to turn off the LED once the corresponding button has been pushed.

So how about writing code to do that instead of all the sweeping through PWM output values.

What you have not described is how you have things wired up, it seems from the code you have wired the buttons to the analogue inputs for some reason, do you have a pull up or down resistor?

Your checkState routine does not change the value of the state variable passed to it, are you expecting it to? the lines in it setting state to a value have no effect outside that function and as you do nothing with them are meaningless.

Strip your code down to the minimum and include the changes you have made, post your code again, this time in all one piece. If it is too big then attach the file using the additional options triangle at the bottom of the page.

Thanks for your replies.

I've solved the problem with my state variables. My function checkState() didn't overwrite the variables, only used them as input to the function.
I made small changes to the function by checking which pin that had input, and changing the attached state variable.