Need a bit of help with code

Hey everyone …I have a bit of code below and would like to be able to read two separate I/O states in order to enable the next set of commands

int buttonState9 = digitalRead(digitalOUT);
  sensorValue9 = buttonState9;
  if (sensorValue9 ==  HIGH)  {
  Serial.println("HIGH");
  Particle.publish("System Armed","1");
  }
  else if (sensorValue == LOW) {
  Serial.println("LOW");
  } 
  int buttonState10 = digitalRead(digitalOUT1);
  sensorValue10 = buttonState10;
  if (sensorValue10 ==  HIGH)  {
  Serial.println("HIGH");
  Particle.publish("KeyPad OK","1");
  }
  else if (sensorValue == LOW) {
  Serial.println("LOW");
  } 
  //
  int buttonState2 = digitalRead(digitalin1);//Bed Motion
  sensorValue1 = buttonState2;
  if (sensorValue1 == LOW) {  
  Particle.publish("BedMot","0",PRIVATE);
  }

So what Id like to be able to do is if digitalOUT & digitalOUT1 are high then the Bed Motion PIR is enabled …A little push in a direction would be good …Thanks

You may want to start thinking about specific states (AKA state machine) and progress through the states using the inputs from the various sensors. Something like this pseudo code:

enum MyState{
  SYSTEM_STANDBY,
  SYSTEM_ARMED,
  KEYPAD_OK,
  SENSING_MOTION,
  ALARM_STATE,
  STATES_MAX // just an empty state at the end...
} state = SYSTEM_STANDBY;

MyState lastState = STATES_MAX;

// other globals stuff...

void setup() 
{
  // Serial stuff
  // Particle stuff
  // pinMode stuff
}

void loop() 
{
  if(lastState != state) . // state change detection.  Put here things to do when the states are changed
  {
    switch (state)
    {
      case SYSTEM_STANDBY:
        Serial.println("Standby Mode");
        break;
      case SYSTEM_ARMED:
        Serial.println("ARMED");
        break;
      case KEYPAD_OK:
        Serial.println("Keypad OK");
        break;
      case SENSING_MOTION:
        Serial.println("Sensing Motion");
        break;
      case ALARM_STATE:
        Serial.println("Sensing Motion");
        break;
    }
    lastState = state;
  }

  
  switch (state) . // state management here . Put in each case what events trigger state changes
  {
    case SYSTEM_STANDBY:  // wait here for output on digitalOUT
      if(digitalRead(digitalOUT)
      {
        state = SYSTEM_ARMED;
      }
      break;
    case SYSTEM_ARMED:  // wait here for output on digitalOUT1
      if(digitalRead(digitalOUT1))
      {
        state = KEYPAD_OK;
      }
      break;
    case KEYPAD_OK:
      // do keypadOK stuff here which may bring you to previous or next states
      break;
    case SENSING_MOTION:
      // . do motion sensing stuff here
      break;
    case ALARM_STATE:
      // do Alarm things here 
      break;
  }
}
1 Like

Thanks I’ll give this a shot

1 Like