Machine Control POE

I am newer to Robot C. I am teaching the PLtW: POE course. We have a Machine Control Project and the students have decided to try and create a security system.

Goals: Use a bump to arm the alarm, wait 10 s for person to leave, notice an intruder, after 3 seconds sound the alarm unless the button is pressed.

Here is what I have programmed to try and make it work, but I am running into issues:

task main()
{
     bool enter = false;      //enter = intruder
     untilBump(dgtl1);       //arm the system
     wait(10);
     while(enter==false)
         {
             if(SensorValue[dgtl10]>20)      //no intruder detected using ultrasonic
                 {
                     enter = false;       //no intruder detected
                 }
             else             //intruder detected
                {
                   clearTimer(T1);
                   while(time1[T1]<=3001)
                     {
                        if(SensorValue[dgtl1]==0 && time1[T1]>3000)  //button not pressed within 3 s 
                              {
                                    enter = true; //intruder detected
                              }
                         else       //button is pressed or more than 3 seconds have passed
                             {
                                    enter = false; /*no intruder detected, I want this line of code to kick back out to the initial while statement to continue looking for intruders*/
                             }
                      }

                }
      while(enter) //while an intruder is detected
          {
             while(1==1) //continuously run this code
                 {
                     turnFlashlightOn(port1, 127); //flash lights as alarm
                     wait(0.5);
                     turnFlashlightOff(port1);
                     wait(0.5);
                 }
           }
}

Help! Any and all advice is welcome. Thanks!

What issues are you currently having with it?

1 Like

Here is an example of how such logic could be implemented with a state machine:

task main()
{
    int systemState = 0; // 0 = system is not activated

    while(1)
    {
        bool controlButtonPressed = (SensorValue[dgtl1]==1); 
        bool intruderIsDetected   = (SensorValue[dgtl10]<20); // ultrasonic detected an intruder nearby

        if( systemState==0 && // system is not activated yet
            controlButtonPressed==false )
        {
            delay(1); // sleep for 1 msec 
            continue; // jump back to the top of the loop
        }

        if( systemState==0 ) // system not activated and control button must have been pressed
        {
            delay(10000); // sleep for 10 sec
            systemState=1; // move system into armed state
            continue; // jump back to the top of the loop
        }
        
        if( systemState==1 && // system is armed
            intruderIsDetected == false )
        {
            delay(1); // sleep for 1 msec 
            continue; // jump back to the top of the loop
        }


        if( systemState==1 ) // system is armed and intruder must have been detected
        {
            clearTimer(T1); // reset timer
            systemState=2;  // move system into pre-alarm state
            continue; // jump back to the top of the loop
        }

        if( systemState==2 && // system is in pre-alarm state
            time1[T1]<3001 ) // and 3 sec wait time had not elapsed yet
        {
            if( controlButtonPressed==true )
            {
                systemState=1; // move system back into armed state
            }
            else
            {
                delay(1); // sleep for 1 msec 
            }
            continue; // jump back to the top of the loop
        }
        
        if( systemState==2 ) // system must have been in pre-alarm state for over 3 sec and button wasn't pressed
        {
             // activate the alarm and continuously run this code
             // there is no coming back from here except for power down
             while(1)
             {
                 turnFlashlightOn(port1, 127); //flash lights as alarm
                 delay(500);
                 turnFlashlightOff(port1);
                 delay(500);
             }
        }
    }
}

It could be somewhat simplified, but I made it more verbose for better readability.

I wasn’t sure what you want to do when the button is pressed within 3 sec of detecting an intruder and intruder is still being detected, so you can modify this state machine accordingly.

1 Like

My main problem is that when the bump is pressed within 3 seconds of an intruder being detected, the alarm still goes off. It doesn’t seem to kick back to the original state.

As programs are mostly linear, it looks like once the alarm goes off. It leaves the detection loop and the alarm just runs forever. The easiest way I can think of making the system reset is by using another function. One function for the detection loop and one for the alarm going off. And calling upon one another.