I'm failing Arduino 101!

I want to close switch contacts on an analog pin to sound an audible alarm. (currently an LED, I couldn't take the alarm any more)

In the first example the alarm is high, in the second the alarm is low. Pushing the button has no effect on either.

If I move the switch to a digital pin I can make it work. What am I missing?

Full disclosure: I am trying to add this to another sketch which is inserted at the bottom.

const int powderLevelPin = A1;
const int  audibleAlarm = 9;

void setup() {
  pinMode(powderLevelPin, INPUT);
  pinMode(audibleAlarm, OUTPUT);
}

void loop() {
  if (powderLevelPin, HIGH) {
    digitalWrite(audibleAlarm, HIGH);
  }
}
const int powderLevelPin = A1;
const int  audibleAlarm = 9;

void setup() {
  pinMode(powderLevelPin, INPUT);
  pinMode(audibleAlarm, OUTPUT);
}

void loop() {
  if (powderLevelPin, LOW) {
    digitalWrite(audibleAlarm, HIGH);

This is how I have it connected:

#include <Bounce2.h>
#include <SoftwareSerial.h>
const int  caseFeederRelay = 2;
const int  bulletFeederRelay = 3;
const int  caseFeederLowerSensor = 7;
const int  caseFeederUpperSensor = 8;
const int  audibleAlarm = 9;
SoftwareSerial Genotronex(10, 11); //Rx,Tx
const int  bluetoothStatePin = 12;
const int finisedRoundCounterPin = A0;
const int powderLevelPin = A1;
const int powderChargePin = A2;
const int primerLevelPin = A3;
const int primerSlidePin = A4;
int bluetoothState = 0;
int bluetoothData;
int stateCaseFeederRelay;
int previousStateCaseFeederRelay;
int pauseCaseFeeder = LOW;
int pauseBulletFeeder = HIGH;
Bounce deBounceUpperSensor = Bounce();
Bounce deBounceLowerSensor = Bounce();
Bounce deBouncePowderLevel = Bounce();
Bounce deBouncePowderCharge = Bounce();
Bounce deBouncePrimerLevel = Bounce();
Bounce deBouncePrimerSlide = Bounce();

void setup() {
  pinMode(caseFeederRelay, OUTPUT);
  pinMode(bulletFeederRelay, OUTPUT);
  pinMode(bluetoothStatePin, INPUT);
  pinMode(caseFeederLowerSensor, INPUT);
  pinMode(caseFeederUpperSensor, INPUT);
  pinMode(audibleAlarm, OUTPUT);
  pinMode(finisedRoundCounterPin, OUTPUT);
  pinMode(powderLevelPin, INPUT);
  pinMode(powderChargePin, INPUT);
  pinMode(primerLevelPin, INPUT);
  pinMode(primerSlidePin, INPUT);
  Genotronex.begin(9600);
  Genotronex.println("Bluetooth Connect");
  deBounceUpperSensor.attach(caseFeederUpperSensor);
  deBounceUpperSensor.interval(500);
  deBounceLowerSensor.attach(caseFeederLowerSensor);
  deBounceLowerSensor.interval(25);
  //deBouncePowderLevel.attach(powderLevelPin);
  //deBouncePowderLevel.interval(25);
  deBouncePowderCharge.attach(powderChargePin);
  deBouncePowderCharge.interval(25);
  deBouncePrimerLevel.attach(primerLevelPin);
  deBouncePrimerLevel.interval(25);
  deBouncePrimerSlide.attach(primerSlidePin);
  deBouncePrimerSlide.interval(25);
  Genotronex.println("Bluetooth Connect");
}
void loop() {
  bluetoothState = digitalRead(bluetoothStatePin);
  if (bluetoothState == LOW) {
    bluetoothNotConnected();
  }

  if (Genotronex.available()) {
    bluetoothData = Genotronex.read();

    if (bluetoothData == '0') {
      (pauseBulletFeeder = 0);
      Genotronex.println("Bullet Feeder On");
    }

    if (bluetoothData == '1') {
      (pauseBulletFeeder = 1);
      Genotronex.println("Bullet Feeder Off");
    }

    if (bluetoothData == '6') {
      (pauseCaseFeeder = 1);
      Genotronex.println("Case Feeder On");
    }

    if (bluetoothData == '7') {
      (pauseCaseFeeder = 0);
      Genotronex.println("Case Feeder Off");
    }
  }

  if (pauseBulletFeeder == 0) {
    digitalWrite(bulletFeederRelay, LOW);
  } else {
    digitalWrite(bulletFeederRelay, HIGH);
  }

  if (pauseCaseFeeder == 1) {
    caseSensorNormalBehavior();
  } else {
    digitalWrite(caseFeederRelay, HIGH);
  }
  //////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////
  // deBouncePowderLevel.update();
  // int deBouncePowderLevelRead = deBouncePowderLevel.read();

  // if (deBouncePowderLevelRead != LOW) {
  //    digitalWrite(audibleAlarm, LOW);
  //  }
  //  else if (deBouncePowderLevelRead != HIGH) {
  //    analogWrite(audibleAlarm, HIGH);
  //  }

  //  deBouncePowderCharge.update();
  //  int deBouncePowderChargeRead = deBouncePowderCharge.read();
  //  deBouncePrimerLevel.update();
  //  int deBouncePrimerLevelRead = deBouncePrimerLevel.read();
  //  deBouncePrimerSlide.update();
  //  int deBouncePrimerSlideRead = deBouncePrimerSlide.read();

  if (powderLevelPin, HIGH) {
    digitalWrite(audibleAlarm, HIGH);
  }
  //////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////



}
void caseSensorNormalBehavior() {

  deBounceUpperSensor.update();
  deBounceLowerSensor.update();
  int deBounceUpperSensorRead = deBounceUpperSensor.read();
  int deBounceLowerSensorRead = deBounceLowerSensor.read();

  if ( deBounceUpperSensorRead != LOW ) {
    digitalWrite(caseFeederRelay, HIGH);
  }
  else if  (deBounceLowerSensorRead != HIGH ) {
    digitalWrite(caseFeederRelay, LOW);
  }

  stateCaseFeederRelay = digitalRead(caseFeederRelay);
  if (previousStateCaseFeederRelay != stateCaseFeederRelay) {
    if (stateCaseFeederRelay == HIGH) {
      Genotronex.println("caseFeederRelay Off");
    } else {
      Genotronex.println("caseFeederRelay On");
    };
    previousStateCaseFeederRelay = stateCaseFeederRelay;
  };
}
void bluetoothNotConnected() {
  digitalWrite(caseFeederRelay, LOW);
  digitalWrite(bulletFeederRelay, LOW);
}

if (powderLevelPin, HIGH) { You forgot to read the pin

if (powderLevelPin, LOW) {

That is equivalent to if(A1, LOW) which is not really valid syntax. Maybe you mean

if(digitalRead(powderLevelPin) == LOW) {

In addition to what AWOL said, you need to either add a pull down resistor to your powderLevelPin or connect the switch between powderLevelPin and ground, turn on the internal pull-up resistor, and modify your code to reflect the changed switch logic. Currently the pin is floating, which means it could be HIGH or LOW at any time. The pull-up or pull-down resistor puts it in a known state when the switch is open.

For information on how to turn on the internal pull-up resistors see:

groundFungus:

if (powderLevelPin, LOW) {

That is equivalent to if(A1, LOW) which is not really valid syntax. Maybe you mean

if(digitalRead(powderLevelPin) == LOW) {

It's perfectly valid syntax (the compiler would've bitched about it otherwise) - it's the semantics which are questionable.

Thank you all.

This bit does exactly what it is supposed to do. Push a button and light comes on:

const int  audibleAlarm = 9;
const int powderLevelPin = 15;
const int powderChargePin = 16;
const int primerLevelPin = 17;
const int primerSlidePin = 18;
int powderLevelState;
int powderChargeState;
int primerLevelState;
int primerSlideState;

void setup() {
  pinMode(powderLevelPin, INPUT_PULLUP);
  pinMode(powderChargePin, INPUT_PULLUP);
  pinMode(primerLevelPin, INPUT_PULLUP);
  pinMode(primerSlidePin, INPUT_PULLUP);
  pinMode(audibleAlarm, OUTPUT);
}

void loop() {
  powderLevelState = digitalRead(powderLevelPin);
  powderChargeState = digitalRead(powderChargePin);
  primerLevelState = digitalRead(primerLevelPin);
  primerSlideState = digitalRead(primerSlidePin);
  if (powderLevelState == HIGH)  {
    digitalWrite(audibleAlarm, LOW);
  }
  else {
    digitalWrite(audibleAlarm, HIGH);
  }
}

This is what I wish it would do. It will compile but none of the buttons will take audibleAlarm High:

const int  audibleAlarm = 9;
const int powderLevelPin = 15;
const int powderChargePin = 16;
const int primerLevelPin = 17;
const int primerSlidePin = 18;
int powderLevelState;
int powderChargeState;
int primerLevelState;
int primerSlideState;

void setup() {
  pinMode(powderLevelPin, INPUT_PULLUP);
  pinMode(powderChargePin, INPUT_PULLUP);
  pinMode(primerLevelPin, INPUT_PULLUP);
  pinMode(primerSlidePin, INPUT_PULLUP);
  pinMode(audibleAlarm, OUTPUT);
}

void loop() {
  powderLevelState = digitalRead(powderLevelPin);
  powderChargeState = digitalRead(powderChargePin);
  primerLevelState = digitalRead(primerLevelPin);
  primerSlideState = digitalRead(primerSlidePin);
  if (powderLevelState == HIGH|| powderChargeState == HIGH || primerLevelState == HIGH || primerSlideState == HIGH)  {
    digitalWrite(audibleAlarm, LOW);
  }
  else {
    digitalWrite(audibleAlarm, HIGH);
  }
}

Is this doable? An array? Multiple arrays?

Your if logic is wrong. That will only turn your alarm on when all buttons are pushed.

Grumpy_Mike:
Your if logic is wrong. That will only turn your alarm on when all buttons are pushed.

I thought || was OR and && wa AND. Is there a way to write the if statement such that pressing any button will turn the light on and the light stays on as long as any button is pushed?

I thought || was OR and && wa AND. Is there a way to write the if statement such that pressing any button will turn the light on and the light stays on as long as any button is pushed?

 pinMode(powderLevelPin, INPUT_PULLUP);
  pinMode(powderChargePin, INPUT_PULLUP);
  pinMode(primerLevelPin, INPUT_PULLUP);
  pinMode(primerSlidePin, INPUT_PULLUP);

All the inputs are default HIGH when they are not pressed. The OR is correct, but the logic level needs to inverted

 if (powderLevelState == LOW|| powderChargeState == LOW || primerLevelState == LOW || primerSlideState == LOW)  {
    digitalWrite(audibleAlarm, HIGH);// on
  }
  else {
    digitalWrite(audibleAlarm, LOW);// off
  }
}

Are you certain that you want to be working with input pin states, and not state changes in your code?

cattledog:

 pinMode(powderLevelPin, INPUT_PULLUP);

pinMode(powderChargePin, INPUT_PULLUP);
 pinMode(primerLevelPin, INPUT_PULLUP);
 pinMode(primerSlidePin, INPUT_PULLUP);




All the inputs are default HIGH when they are not pressed. The OR is correct, but the logic level needs to inverted



if (powderLevelState == LOW|| powderChargeState == LOW || primerLevelState == LOW || primerSlideState == LOW)  {
   digitalWrite(audibleAlarm, HIGH);// on
 }
 else {
   digitalWrite(audibleAlarm, LOW);// off
 }
}


Are you certain that you want to be working with input pin states, and not state changes in your code?

Your code example accomplished my immediate goal. Thank you.

The only thing I'm certain of is if I push any of the four buttons, (or any combination of the buttons,) I want the light to come on. Beyond that I'm interested in learning and if possible, learning best practices.

By state changes you are suggesting reading the pins and setting variables? Write the code to react to changes in the variables rather than reading the pins in real time and reacting to that?

By state changes you are suggesting reading the pins and setting variables? Write the code to react to changes in the variables rather than reading the pins in real time and reacting to that?

I think you understand. It lets you do things when a button becomes pressed or released and does not have to be held down constantly for something to happen. It also lets you time events from button inputs. It''s a matter of program design.

Take a look at the two of the examples in the IDE examples 02 Digital . "Button" demonstrates response depending on the state of the button. "StateChangeDetection" demonstrates response on change of the state of the button.

warnmar10:
The only thing I'm certain of is if I push any of the four buttons, (or any combination of the buttons,) I want the light to come on.

You could wire all the buttons to the INPUT_PULLUP mode pin and ground. The pin gets a weak 5V because of the mode, anything that grounds that will cause the pin to go LOW.

Wire the buttons in parallel and press any one causes the LOW. That's logic written in metal.

cattledog:
I think you understand. It lets you do things when a button becomes pressed or released and does not have to be held down constantly for something to happen. It also lets you time events from button inputs. It''s a matter of program design.

Take a look at the two of the examples in the IDE examples 02 Digital . "Button" demonstrates response depending on the state of the button. "StateChangeDetection" demonstrates response on change of the state of the button.

Now I'm confused again. I was about to rewrite to use variables but... isn't that what I did, read each pin and save its state to a variable?

 powderLevelState = digitalRead(powderLevelPin);
  powderChargeState = digitalRead(powderChargePin);
  primerLevelState = digitalRead(primerLevelPin);
  primerSlideState = digitalRead(primerSlidePin);
  if ((powderLevelState == LOW|| powderChargeState == LOW || primerLevelState == LOW || primerSlideState == LOW))  {
    digitalWrite(audibleAlarm, HIGH);
  }
  else {
    digitalWrite(audibleAlarm, LOW);
  }

GoForSmoke:
You could wire all the buttons to the INPUT_PULLUP mode pin and ground. The pin gets a weak 5V because of the mode, anything that grounds that will cause the pin to go LOW.

Wire the buttons in parallel and press any one causes the LOW. That's logic written in metal.

I'm working through this problem outside the actual sketch I'm writing. Once I get it nailed down I'll insert it into the big one. In the real sketch I want to make an audible alarm sound but also update a display with which of the switches is in alarm state.

Now I'm confused again. I was about to rewrite to use variables but... isn't that what I did, read each pin and save its state to a variable?

The issue is not whether or not the state is assigned to a variable, but rather if you monitor the change of that state. Typically there will be two variables-- state and lastState where lastState takes on the value of state each pass through a loop. Then there is a conditonal test

if(state != lastState)
{//do something because state has changed}

See this loop from the state change example in the IDE

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

//more code
}//end loop()

Reading those pins should happen in a debounce routine that sets a button status.

My own button library does that, if the status is 2 then the button was just pressed.
Status is held in 3 bits; bit 0 = current pin state, bit 1 = previous pin state, bit 2 = bounce state.
If bit 2 is set (bits value >= 4) then the button is bouncing. 0 is held down, 1 is just released, 2 is just pressed, 3 is up.
Change detect is built in, the user only needs to look for the desired button status.
My button library also allows button arrays.

Your sketch should check the button status to decide anything, not the pin state.
You should be able to get that with the bounce library you have too.

GoForSmoke:
Reading those pins should happen in a debounce routine that sets a button status.

My own button library does that, if the status is 2 then the button was just pressed.
Status is held in 3 bits; bit 0 = current pin state, bit 1 = previous pin state, bit 2 = bounce state.
If bit 2 is set (bits value >= 4) then the button is bouncing. 0 is held down, 1 is just released, 2 is just pressed, 3 is up.
Change detect is built in, the user only needs to look for the desired button status.
My button library also allows button arrays.

Your sketch should check the button status to decide anything, not the pin state.
You should be able to get that with the bounce library you have too.

Something like this?

void caseSensorNormalBehavior() {

  deBounceUpperSensor.update();
  deBounceLowerSensor.update();
  int deBounceUpperSensorRead = deBounceUpperSensor.read();
  int deBounceLowerSensorRead = deBounceLowerSensor.read();

  if ( deBounceUpperSensorRead != LOW ) {
    digitalWrite(caseFeederRelay, HIGH);
  }
  else if  (deBounceLowerSensorRead != HIGH ) {
    digitalWrite(caseFeederRelay, LOW);
  }

  stateCaseFeederRelay = digitalRead(caseFeederRelay);
  if (previousStateCaseFeederRelay != stateCaseFeederRelay) {
    if (stateCaseFeederRelay == HIGH) {
      Genotronex.println("Case Feeder Off");
    } else {
      Genotronex.println("Case Feeder On");
    };
    previousStateCaseFeederRelay = stateCaseFeederRelay;
  };
}

Now that I have the logic of the HIGH vs LOW worked out I'll apply debounce as needed.

GoForSmoke:
Reading those pins should happen in a debounce routine that sets a button status.

My own button library does that, if the status is 2 then the button was just pressed.
Status is held in 3 bits; bit 0 = current pin state, bit 1 = previous pin state, bit 2 = bounce state.
If bit 2 is set (bits value >= 4) then the button is bouncing. 0 is held down, 1 is just released, 2 is just pressed, 3 is up.
Change detect is built in, the user only needs to look for the desired button status.
My button library also allows button arrays.

Your sketch should check the button status to decide anything, not the pin state.
You should be able to get that with the bounce library you have too.

Wait. What?

I'm not married to my bounce library. If there is a better way I'm interested. Right now I'm struggling with is the pin high or low, I may not be ready for bit value decisions yet.

I'm using the bounce library on two optical switches and really I think it is only needed on one. I'm not using it because I recognized a bounce problem. I used it to keep the switch from turning my machine off until I was really sure there was a case blocking the sensor and not just blinking the sensor on its way past.

These four switches I've been working on today are actual SPST microswitches. If the level of powder in my dispenser gets too low a switch will close and stay that way until I get up an tend to it etc.

I intend to add a fifth switch that will count finished rounds as they go by. For that one I assumed I would use debounce. Are there other reasons to debounce if there are no counting or timing events connected to the switch events?

Using Bounce2 to read the inputs is a good idea. I'm still not clear about the architecture of your code, and if you want the alarms to sound when the input is triggered and then stay that way until something else happens(e.g an operator clears a jam and presses a resume button), or if you want the alarm to sound only when the input is in a certain state and the alarm will sound and stop solely dependent upon that input.

I remember from a previous thread where we discussed a choice between using the Bounce2 functions .rose() and .fell() to look at state changes, or if you wanted to use the .read() function

EDIT: Sorry, this discussion wasn't in a previous thread with you, but the point is still relevant.