Go Down

### Topic: help with boolean (Read 2509 times)previous topic - next topic

#### jonisonvespaa

##### Apr 22, 2012, 03:17 pmLast Edit: Apr 22, 2012, 04:01 pm by jonisonvespa Reason: 1
hi
im really stuck im trying to make a mute button to so when alarm sounds i can mute it with a button, i just cant make it work, any help greatly appreciated thanks
Code: [Select]
`           //    input and alatm circuit, if any inputs are on for more > 30 seconds alarm sounds, mute button to turn off alarm.             int inputA = 2;                    // input aint inputB = 3;                    // input bint mute = 4;                      // mute low inputint led = 5;                       // LED on pin 3int alarm = 6;                     // alarm soundsint abgood = 7;                    // healthy a and b inputs made.int abfail = 8;                    // FAIL a or b failed.int inputaval;                     // val for reading input aint inputbval;                     // val for reading input bboolean running = false;void setup(){  pinMode(inputA, INPUT);         //input  digitalWrite(inputB, HIGH);     //turn on pullup r  pinMode(inputB, INPUT);         //input  digitalWrite(inputB, HIGH);     //turn on pullup r  pinMode(mute, INPUT);           //mute push switch input  digitalWrite(mute, HIGH);       //turn on pull up r  pinMode(led, OUTPUT);           //led to show  pinMode(alarm, OUTPUT);         //alarm circuit.  pinMode(abgood, OUTPUT);        //lights up green led for healthy status.  pinMode(abfail, OUTPUT);        //lights up red led for a fail. }void loop(){  inputaval = digitalRead(inputA);            // read input value and store it in val1  inputbval = digitalRead(inputB);            // read input value and store it in val2    if ((inputaval == LOW) && (inputbval == LOW))  {    delay(1000);    digitalWrite(abgood, HIGH);               // a and B inputs are healthy.    digitalWrite(alarm, LOW);                 // no alarm    digitalWrite(abfail, LOW);                // no fail led lit                          } else{      delay (1000);                                  digitalWrite(alarm, HIGH);                // Alarm sounds   ****want to MUTE/toggle this output****          digitalWrite(abfail, HIGH);               // fail led lit.  if (digitalRead(mute) == LOW)              // switch is pressed - pullup keeps pin high normally      delay(100);                              // delay to debounce switch    running = !running;                      // toggle running variable    digitalWrite(alarm, running);              // indicate via LED  }}`

#### PaulS

#1
##### Apr 22, 2012, 03:54 pm
Code: [Select]
`  inputaval = digitalRead(inputa);            // read input value and store it in fan1  inputbval = digitalRead(inputb);            // read input value and store it in fan2`
The code does not do what the comments say. If you are not going to maintain the comments, get rid of them. Incorrect comments are worse than no comments.

You'll notice that there are functions like digitalRead, analogWrite(), etc., rather than digitalread and analogwrite. Ever wonder why? It's simple, really. The mixed case makes it much easier to see the words, and to see that you are using the correct function. You should really consider adopting a similar scheme for naming your variables. inputA and inputB are much easier to distinguish than inputa and inputb.

But, really, you don't have something connected to the Arduino called an input, do you. Use names that reflect what it attached. Use names for the state of what it attached that reflect what is attached. inputaval is way too vague.

When I see code with multiple } on one line, I give up trying to read it.
Code: [Select]
`if(thisOrThat){   if(someOtherCondition)   {   }   else   {   }}`

is much easier to see the logic of than
Code: [Select]
`if(thisOrThat){  if(someOtherCondition){  }else{}}`

If you had some names that allowed me to see which pin the mute button was connected to, and some visible logic in the structure of the code, it would be much easier to see where you are going wrong, and to explain what you need to change.

#### wildbill

#2
##### Apr 22, 2012, 04:04 pm
What is the mute button supposed to do? Mute the alarm while pressed or mute it forever or mute it until the error condition goes away & allow a subsequent alarm to be sounded?

#### jonisonvespaa

#3
##### Apr 22, 2012, 04:06 pm
ok points taken paul

#### jonisonvespaa

#4
##### Apr 22, 2012, 04:56 pm
hi bill,
sorry i wasnt very clear im trying to make it mute until the error goes away, and to allow subsequent alarms to be sounded if the conditions occur
thank you

#### jonisonvespaa

#5
##### Apr 22, 2012, 05:06 pmLast Edit: Apr 22, 2012, 05:09 pm by jonisonvespa Reason: 1
Code: [Select]
`             //    input and alatm circuit, if any inputs are on for more > 30 seconds alarm sounds, mute button to turn off alarm.             int InputA = 2;                    // input aint InputB = 3;                    // input bint Mute = 4;                      // mute button, low inputint Led = 5;                       // LED on pin 3int Alarm = 6;                     // alarm sounds int ABgood = 7;                    // healthy a and b inputs made.int ABfail = 8;                    // FAIL a or b failed.int InputAval;                     // val for reading input aint InputBval;                     // val for reading input bboolean running = false;void setup(){  pinMode(InputA, INPUT);         //input a input  digitalWrite(InputA, HIGH);     //turn on pullup r  pinMode(InputB, INPUT);         //input b input  digitalWrite(InputB, HIGH);     //turn on pullup r  pinMode(Mute, INPUT);           //mute push switch input  digitalWrite(Mute, HIGH);       //turn on pull up r  pinMode(Led, OUTPUT);           //led to show  pinMode(Alarm, OUTPUT);         //alarm circuit.  pinMode(ABgood, OUTPUT);        //lights up green led for healthy status.  pinMode(ABfail, OUTPUT); }      //lights up red led for a fail.  void loop(){    InputAval = digitalRead(InputA);            // read input value and store it in fan1  InputBval = digitalRead(InputB);            // read input value and store it in fan2    if ((InputAval == LOW) && (InputBval == LOW))     {delay(1000);    digitalWrite(ABgood, HIGH);               // a and b inputs are healthy.    digitalWrite(Alarm, LOW);                 // no alarm    digitalWrite(ABfail, LOW);                // no fail led lit                            }else{        delay (1000);                                  digitalWrite(Alarm, HIGH);                 // Alarm sounds   ****want to MUTE/toggle this output****            digitalWrite(ABfail, HIGH);}               // fail led lit.      if (digitalRead(Mute) == LOW)                 // switch is pressed - pullup keeps pin high normally      delay(100);                                 // delay to debounce switch    running = !running;                         // toggle running variable    digitalWrite(Alarm, running); }             // indicate via LED `

yes paul i see what you mean, looks more logical will take on board your advice thank you

#### PaulS

#6
##### Apr 22, 2012, 05:12 pm
Quote
looks more logical

Really? What does this } match?
Code: [Select]
`    digitalWrite(Alarm, running); }             // indicate via LED`

Why are you delay()ing only if the switch is pressed?
Code: [Select]
`  if (digitalRead(Mute) == LOW)                 // switch is pressed - pullup keeps pin high normally      delay(100);                                 // delay to debounce switch`

#### jonisonvespaa

#7
##### Apr 24, 2012, 12:08 pm
Code: [Select]
`* *  2 switches monitoring two fans, healthly if both fan1 and fan2 switches are low output switches a relay, if any input high an alarm sounds no output to relay. */                              //int muteoutput = 2;          // mute buzzer (removes the low in the buz circuit to mute) XXXXXint Buz = 10;                // buzzer sounds if fault int Mute = A2;               // mut switch input    int Service = 9;             // service switch                           int Supfanled = 4;           // supply fan ledint Extfanled = 5;           // ext fan led. int Esled = 8;               // emergency stop switch led                                        int Relay = 3;               // green led lights if fan1 and fan2 are low connected to pin 9. healthey fan1 and fan2 int Gasled = 12;             // led to indicate gas is on.int Fanfault = 6;            // red led lights if fan 1 or fan2 are high connected to pin 8.  (sounds an alarm)int Es = A3;                 // emergency stop switch input.                               int Fan2 = A5;               // fan switch is connected to pin 3  int Fan1 = A4;               // fan switch is connected to pin 2  int Fan1val;                 // var for reading the pin statusint Fan2val;                 // var for reading the pin statusint Esval;                   // val for reading emergency stop statusint Srviceval;               // val for service status.boolean running = false;     // *****void setup(){                                                   Serial.begin(9600);  pinMode(Service, INPUT);              // service switch on just gas operates nothing else  digitalWrite(Service, HIGH);          //turn on pulup r  pinMode(Supfanled, OUTPUT);           // supply fan led o/p   pinMode(Extfanled, OUTPUT);           // extfan led o/p  pinMode(Gasled, OUTPUT);               // Greenlled as op.  pinMode(Fanfault, OUTPUT);            // redled as op.  pinMode(Fan1, INPUT);                // Set the switch pin as ip.  digitalWrite(Fan1, HIGH);             // turn on internal r  pinMode(Fan2, INPUT);                 // Set the switch pin as ip.  digitalWrite(Fan2, HIGH);             // turn on internal r  pinMode(Relay, OUTPUT);               // Set the switch pin as op.  pinMode(Buz, OUTPUT);                 // Set the switch pin as op.  pinMode(Mute, OUTPUT);                // Set the switch pin as 1p.   pinMode(Es, INPUT);                   // Set the switch pin as 1p. *****  digitalWrite(Es, HIGH);               // turn on pullup r  pinMode(Esled, OUTPUT);               // Set the switch pin as 1p    pinMode(Mute, INPUT);                 // set the switch pin as ip  digitalWrite(Mute, HIGH);             // turn on pullup resistor } void loop(){  Fan1val = digitalRead(Fan1);            // read input value and store it in fan1  Fan2val = digitalRead(Fan2);            // read input value and store it in fan2  Esval = digitalRead(Es);                // read estop value and store value    if ((Fan1val == LOW) && (Fan2val == LOW) && (Esval == LOW))   {             delay (1000);                             // check if both inputs show fan OK (Switch is pressed)     digitalWrite(Gasled, HIGH);               // turn green led on.    digitalWrite(Relay, HIGH);                // turn on gas relay.    digitalWrite(Buz,LOW);                    // buzzer off, no error    digitalWrite(Fanfault,LOW);               // red led off no fault                                             } else{        delay (1000);                                   digitalWrite(Gasled, LOW);                // turn green OFF          digitalWrite(Relay, LOW);                 // turn off relay     **** Turn off Relay if fault has been greater than 30 seconds*****    digitalWrite(Fanfault, HIGH);             // red led on    digitalWrite(Buz, HIGH);                  // Alarm on            ****want to toggle this on/off only when fault, with the mute button *******                        digitalWrite(Supfanled, Fan1val);          //fan1 status led    digitalWrite(Extfanled, Fan2val);          //fan2 status led    digitalWrite(Esled, Esval);                //estop status   }}  `

hi,
im really struggling with my program and im stuck, all i need to finish my project is to add a mute to my alarm,  and a 30 sec delay to my relay.

finding it really difficuly to understand mills() to add a delay and boolean to make a mute button, hope someone could help me maby if i see the code it will help understand how these work and help me go faward
thank you

#### PaulS

#8
##### Apr 24, 2012, 12:14 pm
Why was it necessary to start a new thread on the same topic?

Intelligent use of white space is good.
Code: [Select]
`  digitalWrite(Mute, HIGH);             // turn on pullup resistor } void loop(){  Fan1val = digitalRead(Fan1);            // read input value and store it in fan1  Fan2val = digitalRead(Fan2);            // read input value and store it in fan2  Esval = digitalRead(Es);                // read estop value and store value    if ((Fan1val == LOW) && (Fan2val == LOW) && (Esval == LOW))   {             delay (1000);                             // check if both inputs show fan OK (Switch is pressed) `
isn't it.

Where are you triggering an alarm? Where are you reading the mute switch? Where are you acting on that switch?

#### AWOL

#9
##### Apr 24, 2012, 12:17 pmLast Edit: Apr 24, 2012, 12:19 pm by AWOL Reason: 1
I thought I was (am?) having a deja vu.
They looked to be on the same topic, so I merged them.

Code: [Select]
`  pinMode(Mute, OUTPUT);                // Set the switch pin as 1p. `
What is a "1p"? (apart from 1/100th of a pound sterling)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### jonisonvespaa

#10
##### Apr 24, 2012, 12:22 pmLast Edit: Apr 24, 2012, 12:28 pm by jonisonvespa Reason: 1
hi

my alarm is here, is a high output that switches on a buzzer circuit.

digitalWrite(Buz, HIGH);                  // Alarm on

my mute switch is here, a low in to mute the alarm.

int Mute = A2;               // mute switch input

to be honest i really dont know how to act upon the input (mute)

(pinMode(Mute, OUTPUT);                // Set the switch pin as 1p.) sorry corrected should be INPUT

#### PaulS

#11
##### Apr 24, 2012, 12:28 pm
Quote
my mute switch is here, a low in to mute the alarm.

That defines a pin name. There is a lot more to reading the state of the switch than that. You have not defined what the mute switch is to do, when it is to be read, or when the mute effect starts and ends.

For instance, on the rare occasions when I watch TV, I mute it when the commercials come on. I unmute it when the commercials end.

What do you want to mute? When do you want to mute it? When do you want to stop muting it?

#### jonisonvespaa

#12
##### Apr 24, 2012, 12:34 pmLast Edit: Apr 24, 2012, 12:42 pm by jonisonvespa Reason: 1
hi paul

the mute needs to mute (Buz, HIGH); alarm,  and finish when, if ((Fan1val == LOW) && (Fan2val == LOW) && (Esval == LOW))

#### PaulS

#13
##### Apr 24, 2012, 12:39 pm
You need a flag (boolean muted = false;). You need to set the value of the flag in appropriate places. Set it to false when the need for muting ends. Set it to true when the mute switch reads LOW.

Then, use the muted flag to decide whether to make the annoying noise, or not.