My Global variable automatically sets to zero?

Here this is a simple code

#include <PPMReader.h> // library to read data from my flysky i6
#include <LED.h> // custom class to turn on off led
bool led_on; // whether led shall be on or off
int channel;
void setup(){
    ....
    led_on = false;
}
void loop{
channel = ppm.read()// this is a function that reads channel  from receiver and it gets value between 1000 and 1998
if( channel <1050){
led_on = false;
}
else if (channel > 1985){
led_on = true;
}
if (led_on){
led.on();
}
else{
led.off();
}

Now the problem is that, I expect once led_is turned on it remains turned on unless I turn if off myself by decreasing the value of channel below 1050.But what happnes is that for whatever time my channel value is more than 1985 led turns on else it remains off.

Now I think I got what is causing the problem .

When my channel4 from flysky is maximum .. for some time it gets zero as shown in the graph.
:sweat_smile: I could not figure it out yesterday.
Still I have not solved the issue.
I do not want to use delay in my main thread.

If i missed something please do tell me, so that i can correct it.
Thanking you

I'd start with what's the value of channel by using a Serial.println( channel ); thingy to see what the do is.

what happens when channel is >1050 and < 1985?

I'd put a print statement to see if the code is ever getting there.

looks like it should work as expected, unless read value is unstable and could change value even if for a split second

@killzone_kid I have edited my question. You were right.

@anchalshivank the installation and troubleshooting section is for getting the Arduino system working. It is specifically NOT fir your project. Please take more care when choosing a section to post in. Please read the getting the best from this forum post and the forum section description before posting again.

I have moved your post to this more appropriate place.

I have solved the issue.. As it can be seen from the graph that for a tiny amout of time the value drops to zero and value droping to zero for that tiny amout of time shall not either turn on or off the led.

so simple solution is to change the condition of the if statement.

if(channel4>1985){
led_on = true;
}
else if(channel5>=1000 && channel4 <1017){//as receiver value ranges from 1000 to 1998 , zero shall be excluded in any case
led_on = false;
}

Thank you everyone :grinning:

And the opening post did disappear for a while :frowning: Back now :slight_smile:

Yes, and it is real because after I had moved it and the opening post, the only post made by the OP, when I used the @ to address the OP no names at all appeared. Where as now all the names of every contributor appeared in a drop down list as normal.

As the iPad I am using can’t display the posts and reply at the same time, unlike my laptop, I had to close the reply remember four letters of the op’s name, open up the reply and type them in. So it was quite a performance getting his name in.

@anchalshivank, sorry for derailing your topic a little.

@Grumpy_Mike, I've learned to first post the reply and next move the topic.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.