<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>If-else chain not working properly.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/33423/if-else-chain-not-working-properly</link><description>Hi, 
 
 I am using nRF52840 DK, and I am creating my program starting from the basic UART example. I am not using threads, just plain sequential execution. I have a chain of if-else statements, like this: 
 
 The inputs fulfill condition5, but the code</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 18 Apr 2018 06:20:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/33423/if-else-chain-not-working-properly" /><item><title>RE: If-else chain not working properly.</title><link>https://devzone.nordicsemi.com/thread/128724?ContentTypeID=1</link><pubDate>Wed, 18 Apr 2018 06:20:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:63220031-ea2c-4408-a39c-6cce5b38cf2a</guid><dc:creator>adragocelestica</dc:creator><description>&lt;p&gt;You are right. I have edited the version of the code that still worked like that, replaced the &amp;amp; with &amp;amp;&amp;amp;, and it works. Thank you very much for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: If-else chain not working properly.</title><link>https://devzone.nordicsemi.com/thread/128664?ContentTypeID=1</link><pubDate>Tue, 17 Apr 2018 14:55:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:23a14773-eaba-4b56-8820-967eee5f594b</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;It looks like the c syntax is incorrect. &amp;#39;&amp;amp;&amp;#39; is a bit-wise &lt;em&gt;and,&lt;/em&gt; not the &amp;#39;&amp;amp;&amp;amp;&amp;#39; logical &lt;em&gt;and&lt;/em&gt; that this test requires. Instead of this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;if((command[0]==108)&amp;amp;(command[1]==101)&amp;amp;(command[2]==100)&amp;amp;(command_length==3))//led&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Try this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;if((command[0]==108)&amp;amp;&amp;amp;(command[1]==101)&amp;amp;&amp;amp;(command[2]==100)&amp;amp;&amp;amp;(command_length==3))//led&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then the original code may work better&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: If-else chain not working properly.</title><link>https://devzone.nordicsemi.com/thread/128556?ContentTypeID=1</link><pubDate>Tue, 17 Apr 2018 05:15:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ffad1c44-aefe-4146-86b5-652aa5a80b25</guid><dc:creator>adragocelestica</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Thanks for the quick response.&lt;/p&gt;
&lt;p&gt;The array is declared in the main loop, and passed as a pointer to this function. What is quite bothering is that, if above all that, i put this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;if(condition5){
    printf(&amp;quot;found condition5&amp;quot;);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It just works and executes the printf. And the first 4 conditions on the if-else chain work properly. That&amp;#39;s why I discarded that there may be any issue in the declaration of condition5 or the access to the array command[].&lt;/p&gt;
&lt;p&gt;Also, condition1 is the one using more elements of the array command[] (7 elements), and that one does work properly, so I discarded that there may be any issue of access to the array.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Finally, I have found a workaround by doing first the following:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint8_t index=0;
for (uint8_t i=0; i&amp;lt;command_length; i++){
	index += sprintf(&amp;amp;command_string[index], &amp;quot;%c&amp;quot;, command[i]);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And then I have the info in a string, so I re-write the if conditions like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;if(strcmp(command_string, &amp;quot;led&amp;quot;)==0){
    //do stuff
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Now it works properly with all conditions.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know why it &amp;quot;likes&amp;quot; the string compare better, but for now I will go with it. If you want, I can try to declare the array as volatile and see what happens in the code that doesn&amp;#39;t work properly, perhaps just to figure out why it was not working and to prevent similar things from happening in the future. One of my colleagues has the theory that it is an optimizer thing, and during compilation it &amp;quot;decides&amp;quot; that the fifth if will never happen, so it just blows it away. It makes sense, because when debugging that if statement is not implemented in the assembly code.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Alin O. Dragomir&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: If-else chain not working properly.</title><link>https://devzone.nordicsemi.com/thread/128392?ContentTypeID=1</link><pubDate>Mon, 16 Apr 2018 07:42:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc7c413b-d610-405e-9b7e-5715ce6493a4</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Is your array written from an interrupt context?&lt;/p&gt;
&lt;p&gt;I suspect that you need to declare the command[] array as volatile.&lt;/p&gt;
&lt;p&gt;Could you try this and report back?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>