-1
\$\begingroup\$

enter image description here

Could someone help me out and see if my code is correct please and thank you.

#include "mbed.h"

DigitalIn pushswt1(p19);
DigitalIn pushswt2(p20);

PwmOut myled(LED1);
PwmOut red(p21); 
PwmOut piezo(p22);

int main() 
{
        
        red.period(0.01f);
        red=0.0f;
        while (1){
        
         while (pushswt1==0 && pushswt2==0)
            {  
              piezo=0.0;
              red =0.0;
              myled=0.0;
             } 
             while (pushswt2==1)
             {
                  
                piezo.period(1.0/1140);
                piezo=0.7;
                red=1.0;
                myled=0.0;
                wait(0.7);
                
                piezo.period(1.0/960);
                piezo=0.7;
                red=0.0;
                myled=1.0;
                wait(0.7);
               }
        while(pushswt1==1)
        {
           
            for (int i=1200;i<1400;i+=20){
                piezo.period(1.0/i);
                piezo=0.25;
                
                red=(i-1200.0)/(1400-1200);
                
                
                wait(0.25);
              }
                 
            for (int i=1400;i>1200;i-=20){
                piezo.period(1.0/i);
                piezo=0.25;
                 
                red=(i-1200.0)/(1400-1200);
               wait(0.25);
                
               }}
           }}
\$\endgroup\$
7
  • \$\begingroup\$ I fixed the formatting for you but didn't fix the indentation which looks wrong to me. Pay very good attention to it. It will help you debug your code. \$\endgroup\$ – Transistor Oct 1 '20 at 17:29
  • \$\begingroup\$ thank you, should i fix the indentation and upload it here again? \$\endgroup\$ – koko1deep Oct 1 '20 at 17:31
  • \$\begingroup\$ if the code does what you want it to do, then it is correct ... otherwise, it is not correct \$\endgroup\$ – jsotola Oct 1 '20 at 17:45
  • 1
    \$\begingroup\$ Debugging questions here must be specific about how the observed behavior differs from that intended. If this is not a debugging question, then you are going to have to state an actual, specific and specifically answerable question. \$\endgroup\$ – Chris Stratton Oct 1 '20 at 17:51
  • 1
    \$\begingroup\$ Hit the edit link below your question and fix the indentation there. The editor won't take tabs (I think) so indent with spaces. \$\endgroup\$ – Transistor Oct 1 '20 at 19:22
1
\$\begingroup\$

If you're using the online mbed.com compiler, fix the indentation by using the shortcut CTRL+SHIFT+F (or use the right-click menu Format Code). Otherwise if you're using the mbed cli there are linters such as uncrustify that enforce uniform indentation rules for you. Programmer's equivalent to spellcheck, it helps catch typos and other errors.

This software would be easier to validate if the effect generation code was contained in functions such as effect_silence(), effect_siren_warble(), effect_siren_twoTone():

// Output effect of two-tone siren with flashing lights
void effect_siren_twoTone()
{
    // frequency ramp up from 1200Hz to 1400Hz step 20Hz
    for (int i = 1200; i < 1400; i += 20) {
        piezo.period(1.0/i);
        piezo = 0.25;
        red = (i-1200.0)/(1400-1200);
        wait(0.25);
    }
    // frequency ramp down from 1400Hz to 1200Hz step 20Hz
    for (int i = 1400; i > 1200; i -= 20) {
        piezo.period(1.0/i);
        piezo = 0.25;
        red = (i-1200.0)/(1400-1200);
        wait(0.25);
    }
}

Then in the state machine logic, add comments that tie the logic back to the stated requirements:

int main()
{
    // setup: put your setup code here, to run once
    red.period(0.01f);
    red=0.0f;

    while (1) {
        // loop: put your main code here, to run repeatedly

        // wait until p19 and p20 switches are both not pressed
        while (pushswt1==0 && pushswt2==0) {
            // idle state: no effects
            effect_silence();
        }
        while (pushswt2==1) {
            // if p20 switch is pressed
            // effect: warbling siren effect until p20 switch is released
            effect_siren_warble();
        }
        while (pushswt1==1) {
            // if p19 switch is pressed
            // effect: two tone siren effect repeat until p19 switch is released
            effect_siren_twoTone();
        }
    } // end while(1) forever loop
} // end main()

With the software structured this way, it's easy to test/validate/debug the effects separately from testing the state machine logic. If for example the p20 switch press was causing the two-tone siren effect, or if the p19 switch only worked immediately after the p20 switch, then you would know the issue was in the state machine control logic and not the siren effect. If on the other hand the siren effects don't sound right, then you know the problem is in the corresponding effect_siren_twoTone or effect_siren_warble function. That could be debugged by temporarily replacing main() with a simpler main() { while(1) { effect_siren_twoTone(); } } to test a single effect isolated from the buttons/state-machine logic. Naturally keep a backup of the sources! mbed-cli uses mercurial (hg) for source control, you can use hg commit to save a backup copy and hg update to restore previous versions...

One last remark, when building the code, if there are errors reported by the compiler, examine the first compiler error message, and use that as a search term. Stackexchange is a good resource if you search on the type of error message, but not a good resource for "see if my code is correct"...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.