using an if statement that detect 3 button pushes to open a solonoid

I’m using an arduino uno to make a fixture where a part cant be removed until 3 magnet sensors go off. i have the switch working for when the item is placed in the nest. But I’m struggling to code the sensors so it takes all 3 then it tells the solenoid to release. any ideas?

sketch_apr18a.ino (1.67 KB)

welcome to the forums. If you post your code inside code tags, you get this

// constant variables

const int magsensAPin = 2;                   // Magnet sensor A receiving pin
const int magsensBPin = 3;                   // Magnet Sensor B receiving Pin
const int magsensCPin = 4;                   // Magnet Sensor C receiving Pin
const int pswitchPin = 8;                    // Pressure switch on and off pin
const int solenoidPin = 6;                   // solenoid on and off pin
int PowerLEDPin = 7;                         // Powerd LED

//changing variables


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(magsensAPin, INPUT_PULLUP);          // magsens pin 2 A is receiving
  pinMode(magsensBPin, INPUT_PULLUP);          // magsens pin 3 B is receiving
  pinMode(magsensCPin, INPUT_PULLUP);          // magsens pin 4 C is receiving
  pinMode(pswitchPin, INPUT_PULLUP);           // Lets pin receive signals

  // outputs

  pinMode(solenoidPin, OUTPUT);         // solonoid is outputting
  pinMode(PowerLEDPin, OUTPUT);

}


void loop() {
  // Lock in

  // if pressed, button state is high:
  if (digitalRead(pswitchPin) == HIGH) {
    delay(3000);
    // turn solenoid on
    digitalWrite(solenoidPin, HIGH);
  } else {
    // turn solonoid off:
    digitalWrite(solenoidPin, LOW);

  }


  if (digitalRead(magsensAPin) == LOW and digitalRead(magsensBPin) == LOW and digitalRead(magsensCPin) == LOW) {
    // withdraws arm
    digitalWrite(solenoidPin, LOW);
    delay(3000);
  } else {
  }
}



//Flashing LED LOOP

//digitalWrite(PowerLEDPin, HIGH);
//delay(1000);
//digitalWrite(PowerLEDPin, LOW);
//delay(1000);

With your inputs wires as INPUT_PULLUP, pressing == LOW, not pressing == HIGH

blh64:
welcome to the forums. If you post your code inside code tags, you get this

// constant variables

const int magsensAPin = 2;                  // Magnet sensor A receiving pin
const int magsensBPin = 3;                  // Magnet Sensor B receiving Pin
const int magsensCPin = 4;                  // Magnet Sensor C receiving Pin
const int pswitchPin = 8;                    // Pressure switch on and off pin
const int solenoidPin = 6;                  // solenoid on and off pin
int PowerLEDPin = 7;                        // Powerd LED

//changing variables

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(magsensAPin, INPUT_PULLUP);          // magsens pin 2 A is receiving
  pinMode(magsensBPin, INPUT_PULLUP);          // magsens pin 3 B is receiving
  pinMode(magsensCPin, INPUT_PULLUP);          // magsens pin 4 C is receiving
  pinMode(pswitchPin, INPUT_PULLUP);          // Lets pin receive signals

// outputs

pinMode(solenoidPin, OUTPUT);        // solonoid is outputting
  pinMode(PowerLEDPin, OUTPUT);

}

void loop() {
  // Lock in

// if pressed, button state is high:
  if (digitalRead(pswitchPin) == HIGH) {
    delay(3000);
    // turn solenoid on
    digitalWrite(solenoidPin, HIGH);
  } else {
    // turn solonoid off:
    digitalWrite(solenoidPin, LOW);

}

if (digitalRead(magsensAPin) == LOW and digitalRead(magsensBPin) == LOW and digitalRead(magsensCPin) == LOW) {
    // withdraws arm
    digitalWrite(solenoidPin, LOW);
    delay(3000);
  } else {
  }
}

//Flashing LED LOOP

//digitalWrite(PowerLEDPin, HIGH);
//delay(1000);
//digitalWrite(PowerLEDPin, LOW);
//delay(1000);




With your inputs wires as INPUT_PULLUP, pressing == LOW, not pressing == HIGH

AHH ok, sorry about that. and what do you mean by "With your inputs wires as INPUT_PULLUP, pressing == LOW, not pressing == HIGH
[/quote]

jefhod:
AHH ok, sorry about that. and what do you mean by "With your inputs wires as INPUT_PULLUP, pressing == LOW, not pressing == HIGH

What he means is, when using INPUT_PULLUP you have enabled the internal pull-up resistor for the digital input. To then ensure the input is triggered you need to have your magnetic sensor connect to GND / LOW when triggered.

When you have code like this

pinMode(magsensAPin, INPUT_PULLUP);

The proper wiring is to have one side of the sensor/switch button connected to your input pin and the other side connected to ground. When the switch is open, the internal pullup resistor makes sure the pin reads HIGH. When you press the switch, it is connected to ground and will read LOW. You never want the input to not be connected (e.g. "floating input")

the sensors are all wired in like you said. still running into the issue where their not retracting the pneumatic arm once all three are picking up a magnet. (they each have indicator lights thats why i know theyre picking it up)

Perhaps it’s time for you to post a schematic, and the current version of the sketch just to ensure everyone understands what you have.

So I dont have a schematic to send you but heres a picture of the setup, hopefully this helps.

jefhod:
So I dont have a schematic to send you but heres a picture of the setup, hopefully this helps.

How to inline an image

You could just draw a schematic with pencil or biro and post a photo thereof.

this is it besides the fact that im using digital in and out not analog

The INPUT_PULLUP applies to your power switch as well so that logic seems reversed. Are you sure your sensors are reporting HIGH/LOW. Try this code and see what shows up on the Serial Monitor

// constant variables

const int magsensAPin = 2;                   // Magnet sensor A receiving pin
const int magsensBPin = 3;                   // Magnet Sensor B receiving Pin
const int magsensCPin = 4;                   // Magnet Sensor C receiving Pin
const int pswitchPin = 8;                    // Pressure switch on and off pin
const int solenoidPin = 6;                   // solenoid on and off pin
const int PowerLEDPin = 7;                         // Powerd LED

//changing variables

int prevPowerSwitchState;
bool powerOn = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(magsensAPin, INPUT_PULLUP);          // magsens pin 2 A is receiving
  pinMode(magsensBPin, INPUT_PULLUP);          // magsens pin 3 B is receiving
  pinMode(magsensCPin, INPUT_PULLUP);          // magsens pin 4 C is receiving
  pinMode(pswitchPin, INPUT_PULLUP);           // Lets pin receive signals

  // outputs

  pinMode(solenoidPin, OUTPUT);         // solonoid is outputting
  pinMode(PowerLEDPin, OUTPUT);

  prevPowerSwitchState = digitalRead(pswitchPin);
}


void loop() {
  // Lock in

  int magnetAState = digitalRead(magsensAPin);
  int magnetBState = digitalRead(magsensBPin);
  int magnetCState = digitalRead(magsensCPin);
  int powerSwitchState = digitalRead(pswitchPin);

  // if pressed, button state is high:
  if (powerSwitchState != prevPowerSwitchState ) {
    // power switch has changed
    if (powerSwitchState == LOW) {
      // switch just pressed, turn solenoid on
      Serial.println( "Power pressed: solenoid ON");
      delay(3000);
      digitalWrite(solenoidPin, HIGH);
      powerOn = true;
    } else {
      // turn solonoid off:
      Serial.println( "Power released, solenoid OFF" );
      digitalWrite(solenoidPin, LOW);
      powerOn = false;
    }
    delay(25);  // debounce
  }
  prevPowerSwitchState = powerSwitchState;

  // only check sensors if powerOn is true
  if (powerOn == true ) {
    if (magnetAState == LOW and magnetBState == LOW and magnetCState == LOW) {
      // withdraws arm
      Serial.println( "Solenoid OFF:" );
      digitalWrite(solenoidPin, LOW);
      delay(3000);
    }
    else {
      Serial.print( "A: " ); Serial.print( magnetAState );
      Serial.print( ", B: " ); Serial.print( magnetBState );
      Serial.print( ", C: " ); Serial.print( magnetCState );
      Serial.println();
    }
  }
}



//Flashing LED LOOP

//digitalWrite(PowerLEDPin, HIGH);
//delay(1000);
//digitalWrite(PowerLEDPin, LOW);
//delay(1000);

It doesnt compile, im gonna work to fix it now

That compiled as-is for me. IDE 1.8.9 and Uno selected.

sorry had to open a new window. it said

13:54:13.669 -> Power released, solenoid OFF
13:54:24.591 -> Power pressed: solenoid ON
13:54:27.605 -> A: 1, B: 0, C: 0
13:54:27.639 -> A: 1, B: 0, C: 0
13:54:27.639 -> A: 1, B: 0, C: 0
13:54:27.673 -> A: 1, B: 0, C: 0

the code you sent made the sensors work, but now the solenoid arm is stuck out at the beginning of the code.

It would appear that sensor A is stuck HIGH. Have you verified they go HIGH/LOW as they should?

yes, it is, I just changed the value in the code and now it's all working! Thank you Everyone!!