void charging_AC() {
SSD1306_ClearDisplay();
cursor(starting_row);
SSD1306_GotoXY(starting_col, starting_row);
SSD1306_PutC("1. ON");
delay_ms(10);
start_row = starting_row;
start_row +=1;
if (input_state(PIN_D2) == 0 && output_low(PIN_A0))
{
output_high(PIN_A0);
}
delay_ms(500);
SSD1306_GotoXY(starting_col, start_row);
SSD1306_PutC("2. OFF");
delay_ms(10);
cursor(start_row);
start_row +=1;
if (input_state(PIN_D2) == 0 && output_high(PIN_A0))
{
output_low(PIN_A0);
}
delay_ms(500);
SSD1306_GotoXY(starting_col, start_row);
I tried this changes. But i still don't got the required result. Suggest some changes or the code to make it work.
Why do you make unnecessary tests?
if (input_state(PIN_D2) == 0
&& output_high(PIN_A0))
{
output_low(PIN_A0);
}
just set the pin low if D2 == 0
if (input_state(PIN_D2) == 0)
{
output_low(PIN_A0);
}
Same story with
if (input_state(PIN_D2) == 0
&& output_low(PIN_A0))
{
output_high(PIN_A0);
}
Sometimes, when you post code to a forum, you need to pull out the stuff that is irrelevant and probably known to work. Just replace it with a comment like // code removed - displays "ON" on the screen - known to work
Your problem seems to be flow control, not driving the screen. Don't test things that don't change or aren't actually required.
void charging_AC() {
// clear display and set to show "ON" - code removed, known to work
if (input_state(PIN_D2) == 0)
{
output_high(PIN_A0);
}
delay_ms(500);
// change display to show "OFF" - code removed, known to work
if (input_state(PIN_D2) == 0)
{
output_low(PIN_A0);
}
delay_ms(500);
}
People reading through your code don't want to be overwhelmed by detail. In your case, it's not too bad because it's pretty easy to skip over blocks of irrelevant code that is presumed to work. We don't actually KNOW that it works but probably.
I would think the stripped down code would work. To test it, I would write a little program with the bare minimum logic and use the LED output. I don't know how it should work and I don't know why there is a 1 second delay.
When I look at the reduced code I notice:
- Every time I enter the function, "ON" will be displayed - unconditionally
- If D2 is low, A0 is set high
- The program stalls for 1/2 second
- "OFF" will be displayed - unconditionally
- If D2 is low (it could have changed during the delays) A0 is set low otherwise, it is left high until next time
- The program stalls for 1/2 second
If A0 is just a test indicator, I would set it low unconditionally before exit. I don't want to make an unnecessary test or abandon a signal. Do I really care if the operator released the button during the delays?
Don't do 'while' loops unless you know how to get out of them. Don't make unnecessary tests. If a thing could be done unconditionally, do it. I suspect you don't want the display to go to "ON" and then unconditionally go to "OFF" 1/2 second later.
It helps to write down the exact sequence to be follow in some kind of spoken language "If this is true, I want this to happen" or some kind of pseudo code. Get the flow correct and then worry about coding it in C.
I just don't see the point of the code: First it displays "ON" then 1/2 second later is displays "OFF" - unconditionally. First it sets A0 high but if the button is released early, it leaves it that way.
I really don't understand what you are trying to accomplish so I really can't provide any guidance other than dumb replies like "If this is true...". It may be the case that your flow control is correct but I don't think so.